-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch encoder.bat
More file actions
516 lines (457 loc) · 18.1 KB
/
batch encoder.bat
File metadata and controls
516 lines (457 loc) · 18.1 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
@echo off
setlocal enabledelayedexpansion
set CurrentVersion=v1.7.3
cls
set "icongray=[7;90m"
set "iconyellow=[7;33m"
set "icongreen=[7;32m"
set "iconred=[7;31m"
set "textgray=[90m"
set "textgreen=[32m"
set "textred=[31m"
set "textcyan=[36m"
set "formatend=[0m"
(set eline=^
%=this line is empty=%
)
set "space= "
for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser))
:ArgParser
set "FLAG=0"
for %%G in (%*) DO (
set ARG=%%G
rem if FLAG, record the flag name
echo !ARG! | findstr "\--" > nul && (
if not ["!FLAG!"]==["0"] ( rem Check if FLAG is set - if it is, then previous was a boolean.
set "par_!FLAG!=true"
if "%par_debug%"=="true" (echo %iconyellow%par_!FLAG!=TRUE%formatend%)
)
set ARG_NAME=!ARG:~2!
set "FLAG=!ARG_NAME!"
if "%par_debug%"=="true" (echo %iconyellow%FLAG=!ARG_NAME!%formatend%)
) || (
set "par_!FLAG!=!ARG!"
if "%par_debug%"=="true" (echo %iconyellow%par_!FLAG!=!ARG!%formatend%)
set "FLAG=0"
)
)
if not ["!FLAG!"]==["0"] ( rem Final boolean catch
set "par_!FLAG!=true"
if "%par_debug%"=="true" (echo %iconyellow%par_!FLAG!=TRUE%formatend%)
)
if "%par_debug%"=="true" (pause)
cls
if defined par_help (
rem list help for args, then exit
echo %formatend%Help for Batch Encoder %CurrentVersion%
echo Program usage:
echo.
echo "batch encoder v%textgray%x%formatend%.%textgray%x%formatend%.%textgray%x%formatend%.bat"
echo "batch encoder v%textgray%x%formatend%.%textgray%x%formatend%.%textgray%x%formatend%.bat" [options]
echo "batch encoder v%textgray%x%formatend%.%textgray%x%formatend%.%textgray%x%formatend%.bat" --silent --update ^(true^|false^|force^) --ffmpegloc ^<path^> [options]
echo.
echo %textcyan%--silent%formatend% Allows the program to operate without any prompts.
echo Requires --ffmpegloc and --update to be set.
echo.
echo %textcyan%--update%formatend% ^(true^|false^|force^) Sets if the program should check for an update or not.
echo 'force' will update regardless of version.
echo.
echo %textcyan%--ffmpegloc%formatend% ^<path^> Sets the path of FFmpeg. Will accept a relative path.
echo.
echo %textcyan%--verbose%formatend% Restores FFmpeg display behavior to pre-v1.6.4 ^(expanded details^).
echo.
echo %textcyan%--speed%formatend% ^(1-9^) Sets speed value. 1 is slowest, 9 is fastest. See prompt for more info.
echo Currently only works when --silent flag is used.
echo.
echo %textcyan%--quality%formatend% ^(1-9^) Sets quality value. 1 is lowest, 9 is highest. See prompt for more info.
echo Currently only works when --silent flag is used.
echo.
echo %textcyan%--waste%formatend% ^(keep^|recycle^|delete^) Controls what happens with old files. 'keep' will retain,
echo 'recycle' will send to bin, and 'delete' will permanently remove the file.
echo.
goto EndPause
)
if defined par_updated-from (
echo %icongray% ^^! %formatend% Just updated^^! Running cleanup...
timeout /nobreak 2 > nul
rem ↓ special format to remove " from string
if exist "%par_updated-from:"=%" (del "%par_updated-from:"=%")
)
:AskProceed
call:ClearAndTitle
if "%par_silent%"=="true" (goto AskUpdate)
echo %icongray% i %formatend% This program will aim to encode all .mp4 files in the folder it's placed in and recycle the originals.
set /p "startconfirmation=Do you want to proceed? %textgray%[Y/N]%formatend%: "
if /i "%startconfirmation%"=="n" exit
if /i "%startconfirmation%"=="y" (goto AskUpdate)
goto AskProceed
:AskUpdate
call:ClearAndTitle
if /i "%par_silent%"=="true" (
if not defined par_update (echo Error: --silent switch used but --update ^(true^|false^|force^) not provided. & exit /b 1)
set "par_update=%par_update:"=%"
if /i "%par_update%"=="false" (goto FFMPEGLocation)
if /i "%par_update%"=="true" (goto AutoUpdate)
if /i "%par_update%"=="force" (goto AutoUpdate)
echo Error: --update argument invalid ^(should be ^(true^|false^|force^)^). & exit /b 1
)
if /i "%par_update%"=="force" (goto AutoUpdate)
set /p "updateconfirmation=%icongray% ^ %formatend% Would you like to check for an update? %textgray%[Y/N]%formatend%: "
if /i "%updateconfirmation%"=="n" (goto FFMPEGLocation)
if /i "%updateconfirmation%"=="y" (goto AutoUpdate)
goto AskUpdate
:AutoUpdate
call:ClearAndTitle
if exist "batch encoder %UpdateVersion%%append%-u.bat" (del "batch encoder %UpdateVersion%%append%-u.bat")
echo %icongray% i %formatend% Downloading information...
set "updateFileName=batch_update.json"
curl --silent -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version:2022-11-28" -o %updateFileName% https://api.github.com/repos/Adam-Kay/Batch-Encoder/releases/latest
if not exist "%updateFileName%" (goto AutoUpdateError)
>%TEMP%\batch_update.tmp findstr "tag_name" %updateFileName%
<%TEMP%\batch_update.tmp set /p "entry_ver="
set "ver=%entry_ver:~15,-2%"
set "UpdateVersion=v%ver:~1%"
>%TEMP%\batch_update.tmp findstr "body" %updateFileName%
set "pwsh_replace=-replace '^!' -replace '^<\/\S*?^>', '#[FORMEND]#' -replace '^<\S*?^>', '#[FORM]#' -replace '^<', '(less)' -replace '^>', '(more)' -replace '\^|', '/'"
for /F "tokens=*" %%g in ('powershell -Command "(Get-Content $env:TEMP\batch_update.tmp) !pwsh_replace! "') do (set entry_body=%%g)
set "changelog=%entry_body:~9,-1%"
set "changelog=%changelog:#[FORM]#=[1m%"
set "changelog=%changelog:#[FORMEND]#=!formatend!%"
set "changelog=%changelog:\n#=\n\r\n#%"
set "changelog=%changelog:#=[100;37m#%"
set "changelog=%changelog:\r=!formatend!\r%"
set "changelog=%changelog: `= [1m%"
set "changelog=%changelog:` =!formatend! %"
>%TEMP%\batch_update.tmp echo %changelog%
for %%? in (%TEMP%\batch_update.tmp) do (set /A strlength=%%~z? - 2)
if %strlength% gtr 1000 (set "changelog=%changelog:~0,1000%... %textgray%[More]%formatend%")
set regex_command=powershell -Command "$x = Get-Content %updateFileName% -Raw; $k = $x | Select-String -Pattern '(?s)url(((?^^^!url).)*?)batch\.encoder'; $g = $k.Matches.Value | Select-String -Pattern '^""[^^^^"""]+?^""",'; $g.Matches.Value"
for /F "tokens=*" %%g in ('%regex_command%') do (set entry_APILink=%%g)
set "UpdateAPIURL=%entry_APILink:~1,-2%"
for %%a in (entry_ver, entry_APILink, UpdateVersion, UpdateAPIURL) do if not defined %%a goto AutoUpdateError
if exist "batch encoder %UpdateVersion%.bat" set "append=_new"
echo.
if /i not "%par_update%"=="force" (
if /i "%UpdateVersion%"=="%CurrentVersion%" (
echo %icongray% - %formatend% Current version is up-to-date.
echo.
echo The program will now restart.
call:GrayPause
del "%updateFileName%"
if /i "%par_silent%"=="true" (
(goto) 2>nul & "%~f0" %* --update false
) else (
(goto) 2>nul & "%~f0"
)
)
)
if /i "%par_update%"=="force" (
echo %iconyellow% ^^! %formatend% Version found^^! ^(%textgreen%%UpdateVersion%%formatend%^)
echo Proceeding with force update in 5 seconds; close window to cancel.
) else (
echo %iconyellow% ^^! %formatend% Differing version found^^! ^(%textred%%CurrentVersion%%formatend% -^> %textgreen%%UpdateVersion%%formatend%^)
echo Proceeding with update in 5 seconds; close window to cancel.
)
rem ... anim
echo. & echo [s
for /l %%x in (1, 1, 6) do (
timeout /nobreak /t 1 > nul
echo [u.[s
)
echo [1ADownloading files...
curl --silent -L -H "Accept: application/octet-stream" -o "batch encoder %UpdateVersion%%append%-u.bat" %UpdateAPIURL%
echo.
if not exist "batch encoder %UpdateVersion%%append%-u.bat" (
echo %iconred% ^^! %formatend% Download failed. & echo. & echo Attempting alternate download...
curl --silent --ssl-no-revoke -L -H "Accept: application/octet-stream" -o "batch encoder %UpdateVersion%%append%-u.bat" %UpdateAPIURL%
echo.
if not exist "batch encoder %UpdateVersion%%append%-u.bat" (
echo %iconred% ^^! %formatend% Alternate download failed.
goto AutoUpdateError
)
)
move /Y "batch encoder %UpdateVersion%%append%-u.bat" "batch encoder %UpdateVersion%%append%.bat" > nul
echo %icongreen% i %formatend% Download complete.
echo. & echo.
echo %changelog:\r\n=!eline!%%formatend%
echo.
echo You can read the full changelog at: https://github.com/Adam-Kay/Batch-Encoder/releases
echo.
echo %icongray% i %formatend% The program will now clean up and restart.
call:GrayPause
del "%updateFileName%"
if /i "%par_silent%"=="true" (
(goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false
) else (
(goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0"
)
:FFMPEGLocation
call:ClearAndTitle
if /i "%par_silent%"=="true" (
if not defined par_ffmpegloc (echo Error: --silent switch used but --ffmpegloc ^<path^> not provided. & exit /b 1)
set "par_ffmpegloc=%par_ffmpegloc:"=%"
if not exist "%par_ffmpegloc%" (
echo Error: --ffmpegloc path "%par_ffmpegloc%" provided does not exist.
exit /b 1
) else (
set "LOCATION=%par_ffmpegloc%"
goto EncodingOptSelect
)
)
set /p "LOCATION=%icongray% ? %formatend% Where is FFMPEG.exe located? (paste full path): "
set "LOCATION=%LOCATION:"=%"
if not exist "%LOCATION%" (
if not exist "%cd%\%LOCATION%" (
echo.
echo Error: Provided filepath "%LOCATION%" does not exist.
call:GrayPause
goto FFMPEGLocation
)
)
:EncodingOptSelect
if /i "%par_silent%"=="true" (goto AdvancedEncodeSpeed)
call:ClearAndTitle
rem Remove in future release:
if "%CurrentVersion%"=="v1.7.0" (echo [100m NEW^^! %formatend%)
echo There are multiple encoding options available. If you are not sure which to choose, select 'Simple'.
echo.
echo [%textcyan%s%formatend%] Simple encode - default options, and the one used in previous versions.
echo [%textcyan%a%formatend%] Advanced encode - options to control quality and speed of output video.
echo.
set /p "encodeopt=Option: "
if /i "%encodeopt%"=="s" (goto Before_Convert)
if /i "%encodeopt%"=="a" (goto AdvancedEncodeSpeed)
goto EncodingOptSelect
:AdvancedEncodeSpeed
call:ClearAndTitle "Advanced Encoding"
set "speeds[9]=ultrafast" & set "speeds[8]=superfast" & set "speeds[7]=veryfast" & set "speeds[6]=faster" & set "speeds[5]=fast"
set "speeds[4]=medium" & set "speeds[3]=slow" & set "speeds[2]=slower" & set "speeds[1]=veryslow" & set "speeds[d]=medium"
if /i "%par_silent%"=="true" (
if defined par_speed (
set "speed_inp=%par_speed%"
) else (
goto AdvancedEncodeQuality
)
) else (
echo [100m Speed %formatend%
echo.
echo Please enter a value between 1 and 9, where 1 is the slowest speed and 9 is the fastest
echo - or enter [%textcyan%d%formatend%] for default ^(4^).
echo.
echo Slower Speed Faster Speed
echo Smaller Filesize Larger Filesize
echo [90m^| ^|[0m
echo [90m+----+----+----+----+----+----+----+----+[0m
echo 1 2 3 4 5 6 7 8 9
echo [90m^|[0m
echo Default
echo.
set /p "speed_inp=Speed: "
)
set "speed_choice=!speeds[%speed_inp%]!
if not defined speed_choice (
if /i "%par_silent%"=="true" (
(echo Error: --speed value '%par_speed%' is invalid. Should be an integer between 1 and 9. & exit /b 1)
) else (
goto AdvancedEncodeSpeed
)
)
:AdvancedEncodeQuality
call:ClearAndTitle "Advanced Encoding"
if /i "%par_silent%"=="true" (
if defined par_quality (
set "quality_inp=%par_quality%"
) else (
goto Before_Convert
)
) else (
echo [100m Quality %formatend%
echo.
echo Please select a value between 1 and 9, where 1 is the lowest quality and 9 is the highest
echo - or enter [%textcyan%d%formatend%] for default ^(6^).
echo.
echo Lower Quality Higher Quality
echo Smaller Filesize Larger Filesize
echo [90m^| ^|[0m
echo [90m+----+----+----+----+----+----+----+----+[0m
echo 1 2 3 4 5 6 7 8 9
echo [90m^|[0m
echo Default
echo.
set /p "quality_inp=Quality: "
)
if %quality_inp: =0% geq 1 (
if %quality_inp: =0% leq 9 (
set /a "quality_choice=%quality_inp%+0"
)
)
if /i "%quality_inp%"=="d" (set "quality_choice=6")
if not defined quality_choice (
if /i "%par_silent%"=="true" (
(echo Error: --quality value '%par_quality%' is invalid. Should be an integer between 1 and 9. & exit /b 1)
) else (
goto AdvancedEncodeQuality
)
)
:Before_Convert
set "LOC_TEST=%LOCATION:\=%"
set "LOC_TEST=%LOC_TEST:/=%"
if "%LOC_TEST%"=="%LOCATION%" (set "pwsh_prefix=.\")
set "LOCATION_pwsh=%pwsh_prefix%%LOCATION:"=%
set /a "COUNTER=-1"
for %%f in (.\*) do set /a "COUNTER+=1"
set "TOTAL=%COUNTER%"
set /a "COUNTER=0"
set /a "VALIDCOUNTER=0"
set "INPUTFILE="
if defined speed_choice (
set "speed_arg=-preset %speed_choice%"
if defined quality_choice (set "spacer= ")
set "speed_text=Speed:%speed_inp%!spacer!"
)
if defined quality_choice (
set /a crfval=40+^(%quality_choice%*-3^)
set "quality_arg=-crf !crfval!"
set "quality_text=Quality:%quality_choice%"
)
:Conversion
if /i not "%par_verbose%"=="true" (set "quietargs=-v quiet -stats ")
for %%f in (.\*) do (
call:ClearAndTitle "%speed_text%%quality_text%"
set "outputfiledupe=false"
set "INPUTFILE=%%f"
set "INPUTFILE=!INPUTFILE:~2!
rem ^ removing ".\" from start of filename
if /i "!INPUTFILE!"=="%~n0%~x0" (
echo Skipping self.
call:GrayTimeout 2
) else (
set /a "COUNTER+=1"
echo [100m Encoding !COUNTER! of %TOTAL% %formatend%
set "TESTSTRING=!INPUTFILE:~-4!"
if /i not "!TESTSTRING!"==".mp4" (
echo Skipping unsupported file^: ^(!INPUTFILE!^)
call:GrayTimeout 3
) else (
set "TESTSTRING=!INPUTFILE:~-8!"
if /i "!TESTSTRING!"==".DVR.mp4" (
if not exist "!INPUTFILE:~0,-8!.ENC.mp4" (
set "OUTPUTFILE=!INPUTFILE:~0,-8!.ENC.mp4"
) else (
set "outputfiledupe=true"
set "OUTPUTFILE=!INPUTFILE:~0,-8!_!date!-!time::=-!.ENC.mp4"
)
) else (
if not exist "!INPUTFILE:~0,-4!.ENC.mp4" (
set "OUTPUTFILE=!INPUTFILE:~0,-4!.ENC.mp4"
) else (
set "outputfiledupe=true"
set "OUTPUTFILE=!INPUTFILE:~0,-4!_!date!-!time::=-!.ENC.mp4"
)
)
set /a "VALIDCOUNTER+=1"
echo Supported file found^: ^(!INPUTFILE!^)
if /i "!outputfiledupe!"=="true" (echo Proposed output file already exists^^^! Appending timestamp...)
call:GrayTimeout 5
rem Move cursor 3 lines up
echo [3A
"%LOCATION%" %quietargs% -i "%CD%\!INPUTFILE!" -map 0 %quality_arg% %speed_arg% "%CD%\!OUTPUTFILE!"
echo. & echo.
echo Performing file checks:
echo ***********************
echo.
echo %icongray% ^| %formatend% Checking for output file...
if /i not exist "%CD%\!OUTPUTFILE!" call:CritError "Output file '!OUTPUTFILE!' does not exist."
echo - Output file exists^^!
echo.
echo %icongray% ^| %formatend% Checking output file length...
for /F "tokens=*" %%g in ( 'powershell -Command "(^& '%LOCATION_pwsh%' -i '!INPUTFILE!' 2>&1 | select-String 'Duration: (.*), s').Matches.Groups[1].Value"'
) do (set LEN_INP=%%g)
for /F "tokens=*" %%g in ( 'powershell -Command "(^& '%LOCATION_pwsh%' -i '!OUTPUTFILE!' 2>&1 | select-String 'Duration: (.*), s').Matches.Groups[1].Value"'
) do (set LEN_OUT=%%g)
for /F "tokens=*" %%g in ( 'powershell -Command "[Math]::Abs(((Get-Date !LEN_INP!) - (Get-Date !LEN_OUT!)).TotalSeconds)"'
) do (set LEN_DIFF=%%g)
echo Input file: !LEN_INP! - Output file: !LEN_OUT!
if !LEN_DIFF! gtr 1 (
call:CritError "File length disparity outside of acceptable range ^(was !LEN_DIFF! seconds^^^)
)
echo - File lengths within range^^!
echo.
powershell -Command "(Get-Item '%CD%\!OUTPUTFILE!').CreationTime=((Get-Item '%CD%\!INPUTFILE!').CreationTime)"
powershell -Command "(Get-Item '%CD%\!OUTPUTFILE!').LastWriteTime=((Get-Item '%CD%\!INPUTFILE!').LastWriteTime)"
powershell -Command "(Get-Item '%CD%\!OUTPUTFILE!').LastAccessTime=((Get-Item '%CD%\!INPUTFILE!').LastAccessTime)"
if "%par_waste%"=="keep" (
echo %icongreen% ^| %formatend% Retaining file...
) else (
if "%par_waste%"=="delete" (
echo %icongreen% ^| %formatend% Safely proceeding with input file deletion...
del "%CD%\!INPUTFILE!"
) else (
if not "%par_waste%"=="recycle" (echo --waste argument "%par_waste%" not recognized. Defaulting to "recycle". & echo.)
echo %icongreen% ^| %formatend% Safely proceeding with input file recycling...
rem delete to recycle bin
powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')"
)
)
timeout /nobreak /t 1 > nul
)
)
)
:EndMsg
call:ClearAndTitle
set /a "SKIPCOUNTER=%TOTAL%-%VALIDCOUNTER%
if %VALIDCOUNTER% gtr 0 (echo [42;97m Completed encoding %VALIDCOUNTER% files. %formatend%)
if %SKIPCOUNTER% gtr 0 (echo [100;37m Skipped %SKIPCOUNTER% invalid files. %formatend%)
if %TOTAL% equ 0 (echo [100;37m No files found. %formatend%)
:EndPause
call:GrayPause
exit /b 0
:CritError
echo.
call:ErrorLine
echo.
echo A critical error occurred. The latest file has not been modified.
set errmsg=%~1
if defined errmsg (echo Error message provided: %errmsg%)
call:GrayPause
(goto) 2>nul || exit /b 3
:AutoUpdateError
del "%updateFileName%"
echo.
call:ErrorLine
echo.
echo There was a problem with the auto-updater. You can download the latest version of the program at:
echo https://github.com/Adam-Kay/Batch-Encoder/releases
echo.
if /i "%par_silent%"=="true" (exit /b 2)
echo The program will now restart.
call:GrayPause
(goto) 2>nul & "%~f0"
:GrayPause
echo %textgray%
if /i not "%par_silent%"=="true" (pause)
echo %formatend%
goto:eof
:GrayTimeout
set timer=%~1
<nul set /p=%textgray%
for %%a in (timer) do if not defined %%a (
timeout /t 5
) else (
timeout /t %timer%
)
echo %formatend%
goto:eof
:ErrorLine
rem echo [4;31m %formatend%
echo [91m____________________________________________________________%formatend%
rem echo [91m************************************************************%formatend%
rem echo [7;31m ********************************************************** %formatend%
goto:eof
:ClearAndTitle
cls
set "message=%~1"
if defined message (set "message=- %message% ")
echo [7m Batch Encoder %CurrentVersion% %message%%formatend%
echo.
goto:eof