-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvencc64_wrapper.cmd
More file actions
807 lines (726 loc) · 29.7 KB
/
nvencc64_wrapper.cmd
File metadata and controls
807 lines (726 loc) · 29.7 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
@echo off & setlocal enabledelayedexpansion
:INIT
call :SETESC
call :SETTOKEN
set "FF_FLAGS=-v info -hide_banner -stats -err_detect ignore_err -fflags +genpts+igndts"
if '%1'=='-h' goto USAGE
if '%1'=='' goto USAGE
set "EDIT_TAGS=1"
set "CHECK_ENCODED=1"
set "DEBUG_AUTOCROP=0"
if "%DEBUG_AUTOCROP%"=="1" (set "DBG=call :DEBUG") else (set "DBG=call :NOP")
call :VALIDATE-PARAMS %*
if "!PARAM_ERR!"=="1" goto :END
call :SETENCODER %1 %2 %3 %4 %5 %6 %7
call :SETAUDIO %1 %2 %3 %4 %5 %6 %7
call :SETCROP %1 %2 %3 %4 %5 %6 %7
call :SETFILTER %1 %2 %3 %4 %5 %6 %7
set "FILTER_HAS_RESIZE=0"
if defined FILTER (
echo(!FILTER! | findstr /i /c:"--vpp-resize" >nul && set "FILTER_HAS_RESIZE=1"
)
call :SETMODE %1 %2 %3 %4 %5 %6 %7
if defined MODE (
echo(!MODE! | findstr /i /c:"--vpp-resize" >nul && set "FILTER_HAS_RESIZE=1"
)
call :SETDECODER %1 %2 %3 %4 %5 %6 %7
set "DECODER_PARAM="
if defined DECODER set "DECODER_PARAM=--%DECODER%"
set "REQ_Q=%3"
if "!REQ_Q!"=="" set "REQ_Q=def"
call :MAIN
goto :END
:MAIN
call :ENSURE_DIR "_Converted"
set "FOUND=0"
for %%I in (*.mkv *.mp4 *.mpg *.mov *.avi *.webm) do if exist "%%I" if not exist "_Converted\%%~nI.mkv" (
echo %ESC%[101;93m %%I %ESC%[0m
set "FOUND=1"
set "FILENAME=%%~nI"
set "SKIP_FILE="
set "RESIZE_PARAM="
set "CROP_L=0"
set "CROP_R=0"
set "TARGET_DIR="
set "RESIZE_REQUIRED=0"
set "SRC_CODEC="
for /f "usebackq delims=" %%C in (`mediainfo "--Inform=Video;%%Format%%" "%%I"`) do (
set "SRC_CODEC=%%C"
)
if not defined SRC_CODEC (
echo ERROR: Could not detect codec. Moving file to _Check.
call :ENSURE_DIR "_Check"
move /Y "%%I" "_Check\" >nul
set "SKIP_FILE=1"
) else (
if "%CHECK_ENCODED%"=="1" (
if /i "!SRC_CODEC!"=="HEVC" if /i "%ENCODER%"=="hevc" set "TARGET_DIR=_Converted"
if /i "!SRC_CODEC!"=="AVC" if /i "%ENCODER%"=="h264" set "TARGET_DIR=_Converted"
if /i "!SRC_CODEC!"=="AV1" if /i "%ENCODER%"=="av1" set "TARGET_DIR=_Converted"
)
)
if defined TARGET_DIR (
call :ENSURE_DIR "!TARGET_DIR!"
set "MOVED_FILE=!TARGET_DIR!\%%~nxI"
echo %ESC%[91mWARNING: Source already encoded as !SRC_CODEC!. Moving file to !TARGET_DIR!.%ESC%[0m
echo.
move /Y "%%I" "!MOVED_FILE!" >nul
setlocal DisableDelayedExpansion
powershell -command "write-output ('file:///' + (get-item '%%~dpI').FullName.Replace('\', '/') -replace [char]34, [char]7 -replace ' ', '%%20' -replace '#', '%%23' -replace [char]39, '%%27' -replace '!', '%%21' -replace '\(', '%%28' -replace '\)', '%%29')"
endlocal
set "SKIP_FILE=1"
if "%EDIT_TAGS%"=="1" call :EDIT_TAGS "!MOVED_FILE!"
)
if not defined SKIP_FILE (
%DBG% ==========================================
%DBG% File: %%I
%DBG% CROP_MODE: "!CROP_MODE!"
%DBG% ==========================================
if "%ENCODER%"=="h264" (
call :SETQUALITY-H264
) else if "%ENCODER%"=="hevc" (
call :SETQUALITY-HEVC
) else if "%ENCODER%"=="av1" (
call :SETQUALITY-HEVC
)
if "!REQ_Q!"=="auto" (
echo "!FILENAME!" | findstr /c:"(19" >nul || echo "!FILENAME!" | findstr /c:"(20" >nul || (
echo %ESC%[91mWARNING: No year found in filename. Falling back to default quality ^(!QUALITY!^).%ESC%[0m
)
)
if /i "!CROP_MODE!"=="AUTO" (
set "PROBE_OK=0"
%DBG% RUN_PROBE is being executed
call :RUN_PROBE "%%I"
if "!PROBE_OK!"=="0" (
%DBG% RUN_PROBE failed, moving file to _Check
echo %ESC%[91mWARNING: Probe failed or source too small. Moving file to _Check.%ESC%[0m
call :ENSURE_DIR "_Check"
move /Y "%%I" "_Check\" >nul
set "SKIP_FILE=1"
) else (
if "!AUTO_CROP!"=="0:0:0:0" (
%DBG% AUTO-CROP: no crop detected, passthrough
set "CROP=--crop 0,0,0,0"
set "RESIZE_REQUIRED=0"
set "RESIZE_PARAM="
) else (
set "AUTO_CROP_FIX=!AUTO_CROP::=,!"
set "CROP=--crop !AUTO_CROP_FIX! --output-res !AUTO_RES!"
for /f "tokens=1,3 delims=:" %%A in ("!AUTO_CROP!") do (
set "CROP_L=%%A"
set "CROP_R=%%C"
)
)
%DBG% AUTO-CROP final result: !CROP!
)
)
if not "!CROP_MODE!"=="AUTO" if defined CROP (
echo(!CROP! | findstr /i /c:"--output-res" >nul && (
if not "!CROP!"=="--crop 0,0,0,0" (
set "RESIZE_REQUIRED=1"
)
)
)
setlocal DisableDelayedExpansion
powershell -command "write-output ('file:///' + (get-item '%%~dpI').FullName.Replace('\', '/') -replace [char]34, [char]7 -replace ' ', '%%20' -replace '#', '%%23' -replace [char]39, '%%27' -replace '!', '%%21' -replace '\(', '%%28' -replace '\)', '%%29')"
endlocal
mediainfo --Inform="General;%%Duration/String2%% - %%FileSize/String4%%" "%%I"
%DBG% NVEnc parameters:
%DBG% CROP = "!CROP!"
%DBG% FILTER = "!FILTER!"
%DBG% MODE = "!MODE!"
%DBG% AUDIO = "!AUDIO!"
if not defined SKIP_FILE (
set "RESIZE_PARAM="
if "!RESIZE_REQUIRED!"=="1" if "!FILTER_HAS_RESIZE!"=="0" (
set "RESIZE_PARAM=--vpp-resize spline36"
)
%DBG% RESIZE_REQUIRED = "!RESIZE_REQUIRED!"
%DBG% FILTER_HAS_RESIZE = "!FILTER_HAS_RESIZE!"
%DBG% RESIZE_PARAM = "!RESIZE_PARAM!"
nvencc64.exe --thread-priority all=lowest --input-thread 1 --output-buf 16 !DECODER_PARAM! -i "%%I" -c %ENCODER% --profile %PROFILE% --tier high --level auto --qvbr !QUALITY! !PRESET! --aq --aq-temporal --aq-strength 10 --lookahead 24 !TUNING! !B_REF! --bref-mode middle !RESIZE_PARAM! !CROP! !FILTER! !MODE! !AUDIO! --sub-copy --chapter-copy -o "_Converted\%%~nI.mkv"
if exist "_Converted\%%~nI.mkv" (
if "%EDIT_TAGS%"=="1" call :EDIT_TAGS "_Converted\%%~nI.mkv"
)
for /L %%X in (5,-1,1) do (
echo Waiting for %%X seconds...
timeout /t 1 >nul
)
echo.
)
)
)
if "%FOUND%"=="0" (
echo No files found.
) else (
powershell -command "$o=ls . -inc *.mkv,*.mp4,*.avi,*.webm; $s=0; $d=0; foreach($f in $o){$c='_Converted\'+$f.Name; if(test-path $c){$s+=$f.Length; $d+=(ls $c).Length}}; if($s -gt 0){write-host ('[INFO] Savings: {0:N2} GB ({1:P1})' -f (($s-$d)/1GB), (($s-$d)/$s)) -fg Green}"
)
exit /b
:SETQUALITY-HEVC
set "ACTUAL_Q=!REQ_Q!"
if "!REQ_Q!"=="auto" (
set "ACTUAL_Q=none"
echo "!FILENAME!" | findstr /c:"(19" >nul && set "ACTUAL_Q=hq"
echo "!FILENAME!" | findstr /c:"(20" >nul && set "ACTUAL_Q=def"
if "!ACTUAL_Q!"=="none" set "ACTUAL_Q=def"
)
set "PRESET=--preset p7"
set "B_REF=--bframes 3 --ref 4"
if "!ACTUAL_Q!"=="uhq" (set "QUALITY=24" & set "TUNING=--tune uhq --multipass 2pass-full" & set "B_REF=--bframes 4 --ref 4")
if "!ACTUAL_Q!"=="hq" (set "QUALITY=26" & set "TUNING=--tune hq --multipass 2pass-full")
if "!ACTUAL_Q!"=="def" (set "QUALITY=28" & set "TUNING=--tune hq --multipass 2pass-quarter")
if "!ACTUAL_Q!"=="lq" (set "QUALITY=30" & set "TUNING=--tune lowlatency --multipass none")
if "!ACTUAL_Q!"=="ulq" (set "QUALITY=32" & set "TUNING=--tune ultralowlatency --multipass none" & set "PRESET=--preset p1")
exit /b
:SETQUALITY-H264
set "ACTUAL_Q=!REQ_Q!"
if "!REQ_Q!"=="auto" (
set "ACTUAL_Q=none"
echo "!FILENAME!" | findstr /c:"(19" >nul && set "ACTUAL_Q=hq"
echo "!FILENAME!" | findstr /c:"(20" >nul && set "ACTUAL_Q=def"
if "!ACTUAL_Q!"=="none" set "ACTUAL_Q=def"
)
set "PRESET=--preset p7"
set "B_REF=--bframes 3 --ref 4"
if "!ACTUAL_Q!"=="uhq" (set "QUALITY=20" & set "TUNING=--tune hq --multipass 2pass-full" & set "B_REF=--bframes 4 --ref 4")
if "!ACTUAL_Q!"=="hq" (set "QUALITY=22" & set "TUNING=--tune hq --multipass 2pass-full")
if "!ACTUAL_Q!"=="def" (set "QUALITY=24" & set "TUNING=--tune hq --multipass 2pass-quarter")
if "!ACTUAL_Q!"=="lq" (set "QUALITY=26" & set "TUNING=--tune lowlatency --multipass none")
if "!ACTUAL_Q!"=="ulq" (set "QUALITY=28" & set "TUNING=--tune ultralowlatency --multipass none" & set "PRESET=--preset p1")
exit /b
:SETENCODER
set "ENCODER=hevc" & set "PROFILE=main"
if "%1"=="def" (set "ENCODER=hevc" & set "PROFILE=main")
if "%1"=="hevc" (set "ENCODER=hevc" & set "PROFILE=main")
if "%1"=="he10" (set "ENCODER=hevc" & set "PROFILE=main10 --output-depth 10")
if "%1"=="h264" (set "ENCODER=h264" & set "PROFILE=high")
if "%1"=="av1" (set "ENCODER=av1" & set "PROFILE=high")
exit /b
:SETAUDIO
set "AUDIO=--audio-codec ac3 --audio-bitrate stereo:192,5.1:384 --audio-encode-other-codec-only"
if "%2"=="copy" (set "AUDIO=--audio-copy")
if "%2"=="copy1" (set "AUDIO=--audio-copy 1")
if "%2"=="copy2" (set "AUDIO=--audio-copy 2")
if "%2"=="copy12" (set "AUDIO=--audio-copy 1,2")
if "%2"=="copy23" (set "AUDIO=--audio-copy 2,3")
if "%2"=="ac3" (set "AUDIO=--audio-codec ac3 --audio-bitrate stereo:192,5.1:384 --audio-encode-other-codec-only --audio-stream 7.1:5.1")
if "%2"=="aac" (set "AUDIO=--audio-codec aac --audio-bitrate stereo:128,5.1:256 --audio-encode-other-codec-only")
if "%2"=="eac3" (set "AUDIO=--audio-codec eac3 --audio-bitrate stereo:320,5.1:640 --audio-encode-other-codec-only")
exit /b
:SETCROP
set "CROP=" & set "CROP_MODE="
if "%4"=="auto" (
set "CROP_MODE=AUTO"
exit /b
)
if "%4"=="none" (set "CROP=")
if "%4"=="696" (set "CROP=--crop 0,192,0,192")
if "%4"=="768" (set "CROP=--crop 0,156,0,156")
if "%4"=="800" (set "CROP=--crop 0,140,0,140")
if "%4"=="804" (set "CROP=--crop 0,138,0,138")
if "%4"=="808" (set "CROP=--crop 0,136,0,136")
if "%4"=="812" (set "CROP=--crop 0,134,0,134")
if "%4"=="816" (set "CROP=--crop 0,132,0,132")
if "%4"=="872" (set "CROP=--crop 0,104,0,104")
if "%4"=="960" (set "CROP=--crop 0,60,0,60")
if "%4"=="1012" (set "CROP=--crop 0,34,0,34")
if "%4"=="1024" (set "CROP=--crop 0,28,0,28")
if "%4"=="1036" (set "CROP=--crop 0,22,0,22")
if "%4"=="1040" (set "CROP=--crop 0,20,0,20")
if "%4"=="720" (set "CROP=--output-res 1280x-2 --crop 0,0,0,0")
if "%4"=="720p" (set "CROP=--output-res -2x720 --crop 0,0,0,0")
if "%4"=="720f" (set "CROP=--output-res 1280x720 --crop 0,0,0,0")
if "%4"=="1080" (set "CROP=--output-res 1920x-2 --crop 0,0,0,0")
if "%4"=="1080p" (set "CROP=--output-res -2x1080 --crop 0,0,0,0")
if "%4"=="1080f" (set "CROP=--output-res 1920x1080 --crop 0,0,0,0")
if "%4"=="2160" (set "CROP=--output-res 3840x-2 --crop 0,0,0,0")
if "%4"=="2160p" (set "CROP=--output-res -2x2160 --crop 0,0,0,0")
if "%4"=="2160f" (set "CROP=--output-res 3840x2160 --crop 0,0,0,0")
if "%4"=="1440" (set "CROP=--output-res 1440x1080 --crop 240,0,240,0")
if "%4"=="1348" (set "CROP=--output-res 1348x1080 --crop 286,0,286,0")
if "%4"=="1420" (set "CROP=--output-res 1420x1080 --crop 250,0,250,0")
if "%4"=="1480" (set "CROP=--output-res 1480x1080 --crop 220,0,220,0")
if "%4"=="1500" (set "CROP=--output-res 1500x1080 --crop 210,0,210,0")
if "%4"=="1764" (set "CROP=--output-res 1764x1080 --crop 78,0,78,0")
if "%4"=="1780" (set "CROP=--output-res 1780x1080 --crop 70,0,70,0")
if "%4"=="1788" (set "CROP=--output-res 1788x1080 --crop 66,0,66,0")
if "%4"=="1792" (set "CROP=--output-res 1792x1080 --crop 64,0,64,0")
if "%4"=="1800" (set "CROP=--output-res 1800x1080 --crop 60,0,60,0")
if "%4"=="c1" (set "CROP=--crop 246,6,246,6 --output-res 1440x1080")
if "%4"=="c2" (set "CROP=--crop 266,8,266,8 --output-res 1400x1080")
if "%4"=="c3" (set "CROP=--crop 262,10,262,10 --output-res 1420x1080")
if "%4"=="c4" (set "CROP=--crop 0,0,0,0 --output-res 1920x1012")
if "%4"=="c5" (set "CROP=")
if "%4"=="c6" (set "CROP=")
exit /b
:SETFILTER
set "FILTER="
if "%5"=="none" (set "FILTER=")
if "%5"=="edgelevel" (set "FILTER=--vpp-edgelevel")
if "%5"=="smooth" (set "FILTER=--vpp-smooth")
if "%5"=="smooth31" (set "FILTER=--vpp-smooth quality=6,qp=31,prec=fp32")
if "%5"=="smooth63" (set "FILTER=--vpp-smooth quality=6,qp=63,prec=fp32")
if "%5"=="nlmeans" (set "FILTER=--vpp-nlmeans")
if "%5"=="gauss" (set "FILTER=--vpp-gauss 3")
if "%5"=="gauss5" (set "FILTER=--vpp-gauss 5")
if "%5"=="sharp" (set "FILTER=--vpp-unsharp")
if "%5"=="denoise" (set "FILTER=--vpp-nvvfx-denoise strength=0")
if "%5"=="denoisehq" (set "FILTER=--vpp-nvvfx-denoise strength=1")
if "%5"=="artifact" (set "FILTER=--vpp-nvvfx-artifact-reduction mode=0")
if "%5"=="artifacthq" (set "FILTER=--vpp-nvvfx-artifact-reduction mode=1")
if "%5"=="superres" (set "FILTER=--vpp-resize algo=nvvfx-superres,superres-mode=0,superres-strength=0.4")
if "%5"=="superreshq" (set "FILTER=--vpp-resize algo=nvvfx-superres,superres-mode=1,superres-strength=0.4")
if "%5"=="vsr" (set "FILTER=--vpp-resize algo=ngx-vsr,vsr-quality=4 --vpp-unsharp")
if "%5"=="vsrdenoise" (set "FILTER=--vpp-resize algo=ngx-vsr,vsr-quality=4 --vpp-unsharp --vpp-nvvfx-denoise strength=0")
if "%5"=="vsrdenoisehq" (set "FILTER=--vpp-resize algo=ngx-vsr,vsr-quality=4 --vpp-unsharp --vpp-nvvfx-denoise strength=1")
if "%5"=="vsrartifact" (set "FILTER=--vpp-resize algo=ngx-vsr,vsr-quality=4 --vpp-unsharp --vpp-nvvfx-artifact-reduction mode=0")
if "%5"=="vsrartifacthq" (set "FILTER=--vpp-resize algo=ngx-vsr,vsr-quality=4 --vpp-unsharp --vpp-nvvfx-artifact-reduction mode=1")
if "%5"=="log" (set "FILTER=--log-packets input_packets.log")
if "%5"=="f1" (set "FILTER=--vpp-colorspace lut3d=regrade.cube,lut3d_interp=tetrahedral --vpp-unsharp radius=1,threshold=0.0,weight=1.2 --vpp-tweak brightness=-0.02,contrast=1.03,gamma=0.98 --vpp-knn radius=3,strength=0.08 --output-res 1280x720")
if "%5"=="f2" (set "FILTER=--vpp-unsharp radius=3,threshold=4.0,weight=0.5 --vpp-knn radius=3,strength=0.08")
if "%5"=="f3" (set "FILTER=")
if "%5"=="f4" (set "FILTER=")
if "%5"=="f5" (set "FILTER=")
if "%5"=="f6" (set "FILTER=")
exit /b
:SETMODE
set "MODE="
if "%6"=="none" (set "MODE=")
if "%6"=="deint" (set "MODE=--interlace auto --vpp-deinterlace adaptive")
if "%6"=="yadif" (set "MODE=--interlace auto --vpp-yadif mode=auto")
if "%6"=="yadifbob" (set "MODE=--interlace auto --vpp-yadif mode=bob --vpp-select-every 2")
if "%6"=="double" (set "MODE=--vpp-fruc double")
if "%6"=="23fps" (set "MODE=--fps 24000/1001")
if "%6"=="25fps" (set "MODE=--fps 25.0")
if "%6"=="30fps" (set "MODE=--fps 30.0")
if "%6"=="60fps" (set "MODE=--fps 60.0")
if "%6"=="29fps" (set "MODE=--fps 30000/1001")
if "%6"=="59fps" (set "MODE=--fps 60000/1001")
if "%6"=="tweak" (set "MODE=--vpp-tweak brightness=0.0,contrast=1.0,gamma=1.0,saturation=1.0,hue=0.0")
if "%6"=="brighter" (set "MODE=--vpp-curves preset=lighter")
if "%6"=="darker" (set "MODE=--vpp-curves preset=darker")
if "%6"=="vintage" (set "MODE=--vpp-curves preset=vintage")
if "%6"=="linear" (set "MODE=--vpp-curves green=0/0 0.5/0.5 1/1:red=0/0 0.5/0.5 1/1:blue=0/0 0.5/0.5 1/1")
if "%6"=="HDRtoSDR" (set "MODE=--vpp-colorspace matrix=bt2020nc:bt709,colorprim=bt2020:bt709,transfer=smpte2084:bt709,range=auto:auto,hdr2sdr=bt2390")
if "%6"=="HDRtoSDRR" (set "MODE=--vpp-colorspace matrix=bt2020nc:bt709,colorprim=bt2020:bt709,transfer=smpte2084:bt709,range=auto:auto,hdr2sdr=reinhard")
if "%6"=="HDRtoSDRM" (set "MODE=--vpp-colorspace matrix=bt2020nc:bt709,colorprim=bt2020:bt709,transfer=smpte2084:bt709,range=auto:auto,hdr2sdr=mobius")
if "%6"=="HDRtoSDRH" (set "MODE=--vpp-colorspace matrix=bt2020nc:bt709,colorprim=bt2020:bt709,transfer=smpte2084:bt709,range=auto:auto,hdr2sdr=hable")
if "%6"=="dv" (set "MODE=--dolby-vision-profile copy --dolby-vision-rpu copy --master-display copy --max-cll copy")
if "%6"=="dolby-vision" (set "MODE=--dolby-vision-profile copy --dolby-vision-rpu copy --master-display copy --max-cll copy")
exit /b
:SETDECODER
set "DECODER=avhw"
if "%7"=="def" (set "DECODER=avhw")
if "%7"=="hw" (set "DECODER=avhw")
if "%7"=="sw" (set "DECODER=avsw")
if "%7"=="auto" (set "DECODER=")
exit /b
:VALIDATE_ONE
if "%~1"=="" exit /b
set "VALID=0"
for %%A in (!%2!) do if /i "%~1"=="%%A" set "VALID=1"
if "!VALID!"=="0" (
set "ERR_MSG=Invalid %3 '%~1' at position %4. Valid values: [!%2: =|!]"
goto :PARAM_ERROR
)
exit /b
:VALIDATE-PARAMS
set "PARAM_ERR=0"
call :VALIDATE_ONE "%1" TOK_ENCODER encoder 1
call :VALIDATE_ONE "%2" TOK_AUDIO audio 2
call :VALIDATE_ONE "%3" TOK_QUALITY quality 3
call :VALIDATE_ONE "%4" TOK_CROP crop 4
call :VALIDATE_ONE "%5" TOK_FILTER filter 5
call :VALIDATE_ONE "%6" TOK_MODE mode 6
call :VALIDATE_ONE "%7" TOK_DECODER decoder 7
exit /b
:PARAM_ERROR
set "PARAM_ERR=1"
echo %ESC%[91mERROR: !ERR_MSG!%ESC%[0m
exit /b
:EDIT_TAGS
if not exist "%~1" exit /b 1
setlocal EnableDelayedExpansion
set "S=" & set "E="
set "FILE=%~1"
set "PS_SCRIPT=%TEMP%\edit_tags_%RANDOM%.ps1"
set "PS_SET_FILE=%TEMP%\edit_tags_set_%RANDOM%.cmd"
if exist "%PS_SCRIPT%" del /F "%PS_SCRIPT%"
if exist "%PS_SET_FILE%" del /F "%PS_SET_FILE%"
for /f "usebackq tokens=1 delims=:" %%A in (`findstr /n "^#PS_EDIT_TAGS_BEGIN#" "%~f0"`) do set /a S=%%A
for /f "usebackq tokens=1 delims=:" %%A in (`findstr /n "^#PS_EDIT_TAGS_END#" "%~f0"`) do set /a E=%%A-S
if not defined S endlocal & exit /b 9
set /a E=E
if %E% LEQ 0 endlocal & exit /b 9
powershell -NoProfile -Command ^
"$lines = Get-Content -Path '%~f0' -Encoding UTF8;" ^
"$start = %S%;" ^
"$end = $start + %E% - 1;" ^
"$lines[$start..$end] | Out-File -FilePath '%PS_SCRIPT%' -Encoding utf8 -Force"
powershell.exe -NoProfile -ExecutionPolicy Bypass ^
-File "%PS_SCRIPT%" "%FILE%" "%PS_SET_FILE%"
if errorlevel 1 (
echo EDIT_TAGS PowerShell failed
call :ENSURE_DIR "_Check"
move "%FILE%" "_Check\" >nul
endlocal & exit /b 1
)
if not exist "%PS_SET_FILE%" (
echo EDIT_TAGS: missing PS output
call :ENSURE_DIR "_Check"
move "%FILE%" "_Check\" >nul
endlocal & exit /b 1
)
call "%PS_SET_FILE%"
if defined EDIT_ACTIONS (
mkvpropedit "%FILE%" --edit info --delete title !EDIT_ACTIONS! >nul
) else (
mkvpropedit "%FILE%" --edit info --delete title >nul
)
if errorlevel 1 (
echo mkvpropedit failed
call :ENSURE_DIR "_Check"
move "%FILE%" "_Check\" >nul
endlocal & exit /b 1
)
:EDIT_TAGS_CLEANUP
if exist "%PS_SCRIPT%" del /F "%PS_SCRIPT%"
if exist "%PS_SET_FILE%" del /F "%PS_SET_FILE%"
endlocal & exit /b
:RUN_PROBE
setlocal
set "S=" & set "E="
set "PROBE_OK=0"
set "AUTO_CROP="
set "AUTO_RES="
set "NVEnc_Crop="
set "NVEnc_Res="
set "PS_SCRIPT=%TEMP%\probe_temp_%RANDOM%.ps1"
set "PS_SET_FILE=%TEMP%\probe_set_vars_%RANDOM%.cmd"
set "PS_STATUS_FILE=%TEMP%\probe_status_output_%RANDOM%.tmp"
if exist "%PS_SET_FILE%" del /F "%PS_SET_FILE%"
if exist "%PS_STATUS_FILE%" del /F "%PS_STATUS_FILE%"
for /f "usebackq tokens=1 delims=:" %%A in (`findstr /n "^#PS_RUN_PROBE_BEGIN#" "%~f0"`) do set /a S=%%A
for /f "usebackq tokens=1 delims=:" %%A in (`findstr /n "^#PS_RUN_PROBE_END#" "%~f0"`) do set /a E=%%A-S
if not defined S endlocal & exit /b 9
set /a E=E
if %E% LEQ 0 endlocal & exit /b 9
powershell -NoProfile -Command ^
"$lines = Get-Content -Path '%~f0' -Encoding UTF8;" ^
"$start = %S%;" ^
"$end = $start + %E% - 1;" ^
"$lines[$start..$end] | Out-File -FilePath '%PS_SCRIPT%' -Encoding utf8 -Force"
powershell.exe -executionpolicy bypass -file "%PS_SCRIPT%" "%~1" -SetFile "%PS_SET_FILE%" -StatusFile "%PS_STATUS_FILE%"
set "RC=%ERRORLEVEL%"
if exist "%PS_SET_FILE%" call "%PS_SET_FILE%"
%DBG% RUN_PROBE: RC=%RC%
%DBG% RUN_PROBE: NVEnc_Crop=%NVEnc_Crop%
%DBG% RUN_PROBE: NVEnc_Res=%NVEnc_Res%
if exist "%PS_SCRIPT%" del /F "%PS_SCRIPT%"
if exist "%PS_SET_FILE%" del /F "%PS_SET_FILE%"
if exist "%PS_STATUS_FILE%" del /F "%PS_STATUS_FILE%"
if "%RC%"=="0" goto :PROBE_OK
if "%RC%"=="8" if defined NVEnc_Crop goto :PROBE_OK
endlocal & exit /b 1
:PROBE_OK
set "TEMP_CROP=%NVEnc_Crop%"
set "TEMP_RES=%NVEnc_Res%"
endlocal & (
set "AUTO_CROP=%TEMP_CROP%"
set "AUTO_RES=%TEMP_RES%"
set "PROBE_OK=1"
)
exit /b 0
:PRINT_TOK
setlocal EnableDelayedExpansion
set "LABEL=%~1"
set "INFO=%~2"
set "TOKVAR=%~3"
set LABEL_PAD=10
set INFO_PAD=12
set PAD=22
set "SPACES= "
set "L=%LABEL%%SPACES%"
set "L=!L:~0,%LABEL_PAD%!"
set "I=%INFO%%SPACES%"
set "I=!I:~0,%INFO_PAD%!"
set "LEFT=!L!!I!"
set /a PAD1=%PAD%-1
set "INDENT=!SPACES:~0,%PAD1%!"
set "LINE=!LEFT!["
set FIRST=1
set WRAP=118
for %%T in (!%TOKVAR%!) do (
if "!FIRST!"=="1" (
set FIRST=0
set "LINE=!LINE!%%T"
) else (
set "TEST=!LINE!|%%T"
if not "!TEST:~0,%WRAP%!"=="!TEST!" (
echo !LINE!]
set "LINE=!INDENT! [%%T"
) else (
set "LINE=!LINE!|%%T"
)
)
)
echo !LINE!]
endlocal & exit /b
:USAGE
setlocal EnableDelayedExpansion
cls
echo Usage: %~n0 ^<encoder^> [audio=ac3] [quality=28] [crop=none] [filter=none] [mode=none] [decoder=avhw]
echo.
call :PRINT_TOK "encoder" "(required)" TOK_ENCODER
call :PRINT_TOK "audio" "(def=ac3)" TOK_AUDIO
call :PRINT_TOK "quality" "(def=28)" TOK_QUALITY
call :PRINT_TOK "crop" "(def=none)" TOK_CROP
call :PRINT_TOK "filter" "(def=none)" TOK_FILTER
call :PRINT_TOK "mode" "(def=none)" TOK_MODE
call :PRINT_TOK "decoder" "(def=hw)" TOK_DECODER
echo.
echo Example: %~n0 ^| %UL%encoder%NO% ^| %UL%audio%NO% ^| %UL%quality%NO% ^| %UL%crop%NO% ^| %UL%filter%NO% ^| %UL%mode%NO% ^| %UL%decoder%NO% ^|
echo Example: %~n0 ^| hevc ^| ac3 ^| ^| ^| ^| ^| ^|
echo Example: %~n0 ^| hevc ^| ac3 ^| auto ^| auto ^| ^| ^| ^|
echo Example: %~n0 ^| hevc ^| copy ^| auto ^| 1080 ^| vsr ^| ^| ^|
echo Example: %~n0 ^| hevc ^| copy ^| hq ^| 1080 ^| gauss ^| ^| ^|
echo Example: %~n0 ^| hevc ^| copy ^| def ^| none ^| none ^| none ^| sw ^|
echo.
endlocal
goto :END
:SETESC
for /f "usebackq delims=" %%A in (`echo prompt $E^| cmd`) do set "ESC=%%A"
set "UL=%ESC%[4m"
set "NO=%ESC%[24m"
exit /b
:SETTOKEN
set "TOK_ENCODER=def hevc he10 h264 av1"
set "TOK_AUDIO=copy copy1 copy2 copy12 copy23 ac3 aac eac3"
set "TOK_QUALITY=def auto hq uhq lq ulq"
set "TOK_CROP=none auto 696 768 800 804 808 812 816 872 960 1012 1024 1036 1040 720 720p 720f 1080 1080p 1080f 2160 2160p 2160f 1440 1348 1420 1480 1500 1764 1780 1788 1792 1800 c1 c2 c3 c4 c5 c6"
set "TOK_FILTER=none edgelevel smooth smooth31 smooth63 nlmeans gauss gauss5 sharp denoise denoisehq artifact artifacthq superres superreshq vsr vsrdenoise vsrdenoisehq vsrartifact vsrartifacthq log f1 f2 f3 f4 f5 f6"
set "TOK_MODE=none deint yadif yadifbob double 23fps 25fps 30fps 60fps 29fps 59fps brighter darker vintage linear tweak HDRtoSDR HDRtoSDRR HDRtoSDRM HDRtoSDRH dv dolby-vision"
set "TOK_DECODER=def hw sw auto"
exit /b
:NOP
exit /b
:ENSURE_DIR
if not exist "%~1" md "%~1"
exit /b
:DEBUG
setlocal EnableDelayedExpansion
set "DBG_MSG=%*"
echo [DEBUG] !DBG_MSG!
endlocal & exit /b
:END
exit /b 0
#PS_RUN_PROBE_BEGIN#
param(
[Parameter(Position=0, Mandatory=$true)]
[string]$VideoFile,
[Parameter(Mandatory=$true)]
[string]$SetFile,
[Parameter(Mandatory=$true)]
[string]$StatusFile
)
$ExitCode = 0
$RejectReason = $null
function Write-Status ($Message) {
$Timestamp = Get-Date -Format "HH:mm:ss"
Add-Content -Path $StatusFile -Value "[$Timestamp] $Message"
}
function Get-Median {
param([int[]]$Numbers)
$Count = $Numbers.Count
if ($Count -eq 0) { return 0 }
$Sorted = $Numbers | Sort-Object
if ($Count % 2 -eq 1) {
return $Sorted[[math]::Floor($Count / 2)]
} else {
return [int](($Sorted[($Count / 2) - 1] + $Sorted[$Count / 2]) / 2)
}
}
$StandardResolutions = @{
384 = @{ Crop="0:192:0:192"; Res="1920x696" }
312 = @{ Crop="0:156:0:156"; Res="1920x768" }
280 = @{ Crop="0:140:0:140"; Res="1920x800" }
276 = @{ Crop="0:138:0:138"; Res="1920x804" }
272 = @{ Crop="0:136:0:136"; Res="1920x808" }
268 = @{ Crop="0:134:0:134"; Res="1920x812" }
264 = @{ Crop="0:132:0:132"; Res="1920x816" }
208 = @{ Crop="0:104:0:104"; Res="1920x872" }
120 = @{ Crop="0:60:0:60"; Res="1920x960" }
68 = @{ Crop="0:34:0:34"; Res="1920x1012" }
56 = @{ Crop="0:28:0:28"; Res="1920x1024" }
44 = @{ Crop="0:22:0:22"; Res="1920x1036" }
40 = @{ Crop="0:20:0:20"; Res="1920x1040" }
0 = @{ Crop="0:0:0:0"; Res="1920x1080" }
}
$StandardWidths = @(1800,1792,1788,1780,1764,1500,1480,1440,1420,1348)
$ffmpegCmd = (get-command ffmpeg.exe).source
$ffprobeCmd = (get-command ffprobe.exe).source
$ProbeTimes = @("00:02:00","00:10:00","00:20:00")
$CropResults = @()
if (-not (Test-Path $VideoFile)) { $ExitCode = 1 }
if ($ExitCode -eq 0) {
$ResolutionInfo = & $ffprobeCmd -v error -select_streams v:0 -show_entries stream=width,height -of default=noprint_wrappers=1:nokey=1 $VideoFile 2>&1
$CleanRes = $ResolutionInfo | Where-Object { $_ -match '^\d+$' }
if ($CleanRes.Count -lt 2) { $ExitCode = 2 }
$OrigWidth = [int]$CleanRes[0]
$OrigHeight = [int]$CleanRes[1]
if ($OrigWidth -lt 1280 -or $OrigHeight -lt 696) {
$RejectReason = "source too small (${OrigWidth}x${OrigHeight})"
}
if ($RejectReason) {
Write-Status "REJECTED: $RejectReason"
exit 8
}
}
if ($ExitCode -eq 0) {
foreach ($Time in $ProbeTimes) {
$out = & $ffmpegCmd -ss $Time -i $VideoFile -t 5 -vf "cropdetect=limit=24:round=2:reset=0" -f null - 2>&1
$m = [regex]::Matches($out,"crop=(\d+:\d+:\d+:\d+)") | Select-Object -Last 1
if ($m) {
$c = ($m.Value -replace 'crop=','') -split ':'
$CropResults += [PSCustomObject]@{ W=[int]$c[0]; H=[int]$c[1]; X=[int]$c[2]; Y=[int]$c[3] }
}
}
if ($CropResults.Count -eq 0) { $ExitCode = 4 }
}
if ($ExitCode -eq 0) {
$W = Get-Median $CropResults.W
$H = Get-Median $CropResults.H
$X = Get-Median $CropResults.X
$Y = Get-Median $CropResults.Y
$CropL = $X; $CropR=$OrigWidth-$X-$W
$CropT = $Y; $CropB=$OrigHeight-$Y-$H
$TotalV = $CropT + $CropB
$TotalH = $CropL + $CropR
if ($TotalV -le 4 -and $TotalH -le 10) {
"SET NVEnc_Crop=0:0:0:0" | Out-File -Encoding ASCII $SetFile
"SET NVEnc_Res=${OrigWidth}x${OrigHeight}" | Out-File -Encoding ASCII $SetFile -Append
exit 0
}
if ($TotalV -le 2 -and $TotalH -gt 8) {
$BestW = $StandardWidths |
Sort-Object { [math]::Abs($_ - ($OrigWidth - $TotalH)) } |
Select-Object -First 1
$Side = [int](($OrigWidth - $BestW) / 2)
"SET NVEnc_Crop=${Side}:0:${Side}:0" | Out-File -Encoding ASCII $SetFile
"SET NVEnc_Res=${BestW}x${OrigHeight}" | Out-File -Encoding ASCII $SetFile -Append
exit 0
}
if ([math]::Abs($CropT - $CropB) -gt 10) {
$RejectReason = "vertical asymmetry (T=$CropT B=$CropB)"
}
elseif ( ($CropT -eq 0 -and $CropB -gt 0) -or ($CropB -eq 0 -and $CropT -gt 0) ) {
$RejectReason = "one-sided vertical crop (T=$CropT B=$CropB)"
}
elseif ($TotalV -eq 0 -and [math]::Abs($CropL - $CropR) -gt 2) {
$RejectReason = "horizontal asymmetry (no matching standard width)"
}
if ($RejectReason) {
Write-Status "REJECTED: $RejectReason"
exit 8
}
$Best = $StandardResolutions.GetEnumerator() | Sort-Object { [math]::Abs($_.Key-$TotalV) } | Select-Object -First 1
$Top=$Best.Value.Crop.Split(':')[1]
$Bottom=$Best.Value.Crop.Split(':')[3]
$TargetH=$Best.Value.Res.Split('x')[1]
$FinalL=0; $TargetW=$OrigWidth
$NVEncCrop="${FinalL}:${Top}:${FinalL}:${Bottom}"
$NVEncRes="${TargetW}x$TargetH"
"SET NVEnc_Crop=$NVEncCrop" | Out-File -Encoding ASCII $SetFile
"SET NVEnc_Res=$NVEncRes" | Out-File -Encoding ASCII $SetFile -Append
}
exit $ExitCode
#PS_RUN_PROBE_END#
#PS_EDIT_TAGS_BEGIN#
param(
[string]$VideoFile,
[string]$SetFile
)
$ErrorActionPreference='Stop'
$j=& mkvmerge.exe -J "$VideoFile" | ConvertFrom-Json
$actions=@()
function IsPureLang($n){
if([string]::IsNullOrWhiteSpace($n)){return $false}
$n -match '^(?i)(Deutsch|Englisch|German|English|French|Stereo|Surround)$'
}
function IsFullWord($n){
$n -match '^(?i)full$'
}
function NormalizeName($n){
if([string]::IsNullOrWhiteSpace($n)){return $null}
if($n -match '(?i)sdh'){ return 'SDH' }
if($n -match '(?i)forced'){ return 'Forced' }
return $null
}
$audioGer=@()
foreach($t in $j.tracks){
if($t.type -eq 'audio' -and $t.properties.language -match '^(?i)(ger|deu)$'){
$audioGer+=$t
}
}
$defaultAudioNum=$null
if($audioGer.Count -gt 0){
$defaultAudioNum=$audioGer[0].properties.number
}
$forcedDone = $false
foreach($t in $j.tracks){
$num=$t.properties.number
$type=$t.type
$name=$t.properties.track_name
if($type -eq 'video'){
$actions+="--edit track:$num --set language=und --set flag-default=0 --set flag-forced=0 --delete name"
continue
}
if($type -eq 'audio'){
$actions+="--edit track:$num --set flag-forced=0"
if($num -eq $defaultAudioNum){
$actions+="--edit track:$num --set flag-default=1"
}else{
$actions+="--edit track:$num --set flag-default=0"
}
if(-not [string]::IsNullOrWhiteSpace($name)){
$normalizedName = NormalizeName $name
if($null -ne $normalizedName){
$actions+="--edit track:$num --set name=$normalizedName"
}elseif(IsPureLang $name){
$actions+="--edit track:$num --delete name"
}elseif(IsFullWord $name){
$actions+="--edit track:$num --delete name"
}
}
continue
}
if($type -eq 'subtitles'){
$isForced = $false
if($t.properties.forced_track -eq $true){ $isForced = $true }
if($name -match '(?i)forced'){ $isForced = $true }
if($isForced){
if(-not $forcedDone){
$actions+="--edit track:$num --set flag-default=1 --set flag-forced=1"
$forcedDone = $true
} else {
$actions+="--edit track:$num --set flag-default=0 --set flag-forced=1"
}
} else {
$actions+="--edit track:$num --set flag-default=0 --set flag-forced=0"
}
if(-not [string]::IsNullOrWhiteSpace($name)){
$normalizedName = NormalizeName $name
if($null -ne $normalizedName){
$actions+="--edit track:$num --set name=$normalizedName"
}elseif(IsPureLang $name){
$actions+="--edit track:$num --delete name"
}elseif(IsFullWord $name){
$actions+="--edit track:$num --delete name"
}
}
continue
}
}
"SET EDIT_ACTIONS=$($actions -join ' ')" | Out-File -Encoding Default -FilePath $SetFile
#PS_EDIT_TAGS_END#