-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_webp_lossless.bat
138 lines (117 loc) · 3.85 KB
/
convert_webp_lossless.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@echo off
setlocal enabledelayedexpansion
rem Check for dry run argument
set "dry_run=0"
if /I "%~1"=="/dry-run" (
set "dry_run=1"
echo Dry run mode activated. No changes will be made.
)
rem Set the target resolution heading
set "target_resolution=convert_webp_lossless"
rem Read the paths from the paths.txt file
set "path_file=%~dp0paths.txt"
rem Initialize variables
set "input_folder="
set "output_folder="
rem Read the paths from the file
set "reading_paths=0"
for /f "delims=" %%A in ('type "%path_file%"') do (
rem Check if we found the target resolution heading
if "%%A"=="# !target_resolution!" (
set "reading_paths=1"
set "input_folder="
set "output_folder="
set "line_count=0"
) else if !reading_paths! == 1 (
if !line_count! lss 2 (
set /a line_count+=1
if !line_count! equ 1 (
set "input_folder=%%A"
) else (
set "output_folder=%%A"
)
) else (
set "reading_paths=0"
)
)
)
rem Check if both paths were read successfully
if not defined input_folder (
echo Input folder not specified for resolution !target_resolution! in paths.txt.
pause
exit /b
)
if not defined output_folder (
echo Output folder not specified for resolution !target_resolution! in paths.txt.
pause
exit /b
)
rem Define the ImageMagick path
set "imagemagick_path=%~dp0bin\magick.exe"
rem Create the output folder if it doesn't exist (but skip if dry run)
if !dry_run! == 0 (
mkdir "%output_folder%"
) else (
echo [Dry Run] Would create output folder: "%output_folder%"
)
set "non_webp_found=0"
set "paused=0"
rem Function to handle pause/resume/cancel
:check_controls
choice /c PRC /n /t 0 /d Y >nul
if errorlevel 3 (
echo Cancelling operations...
exit /b
) else if errorlevel 2 (
if !paused! == 0 (
echo Pausing operations... Press 'R' to resume.
set "paused=1"
)
) else if errorlevel 1 (
if !paused! == 1 (
echo Resuming operations...
set "paused=0"
)
)
exit /b
rem Recursively scan for non-WebP files
for /R "%input_folder%" %%f in (*.*) do (
call :check_controls
if !paused! == 1 (
choice /c R /n /m "Paused. Press 'R' to resume." >nul
set "paused=0"
)
if /I not "%%~xf"==".webp" (
rem Set the flag to indicate that a non-WebP file was found
set "non_webp_found=1"
rem Get the relative path by removing the input folder prefix from the file path
set "relative_path=%%f"
set "relative_path=!relative_path:%input_folder%=!"
rem Extract the directory portion of the relative path
set "relative_dir=%%~dpf"
set "relative_dir=!relative_dir:%input_folder%\=converted\!"
rem Create the corresponding output subdirectory in the converted folder if it doesn't exist (but skip if dry run)
if !dry_run! == 0 (
mkdir "%output_folder%\!relative_dir!" 2>nul
) else (
echo [Dry Run] Would create directory: "%output_folder%\!relative_dir!"
)
rem Convert the non-WebP file to WebP (but skip if dry run)
if !dry_run! == 0 (
%imagemagick_path% "%%f" "%output_folder%\!relative_dir!%%~nf.webp" -define webp:lossless=true
) else (
echo [Dry Run] Would convert file: "%%f" to Lossless WebP, saving to "%output_folder%\!relative_dir!%%~nf.webp" -define webp:lossless=true
)
)
)
rem Create the converted folder only if non-WebP files were found (but skip if dry run)
if !non_png_found! == 1 if !dry_run! == 0 (
mkdir "%output_folder%\converted" 2>nul
) else if !non_webp_found! == 1 (
echo [Dry Run] Would create folder: "%output_folder%\converted"
)
echo Batch resize for !target_resolution! completed!
if !dry_run! == 1 (
echo This was a dry run. No changes were made.
)
pause