-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFFEncoder.ps1
1590 lines (1440 loc) · 67.5 KB
/
FFEncoder.ps1
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
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
.SYNOPSIS
Cross-platform script for encoding HD/FHD/UHD audio/video content using ffmpeg, VapourSynth, x264, and x265
.DESCRIPTION
This script that is meant to make video encoding easier with ffmpeg. Instead of manually changing
the script parameters for each encode, you can pass dynamic parameters to this script using a
simplified, yet powerful, API. Supports HD/FHD/UHD encoding with automatic fetching of HDR
metadata (including HDR10+), automatic cropping, and multiple audio & subtitle options.
.EXAMPLE
## Windows ##
.\FFEncoder.ps1 -InputPath "Path\To\file.mkv" -CRF 16.5 -Preset medium -Deblock -3,-3 -Audio copy -OutputPath "Path\To\Encoded\File.mkv"
.EXAMPLE
## MacOS or Linux ##
./FFEncoder.ps1 -InputPath "Path/To/file.mp4" -CRF 16.5 -Preset medium -Deblock -2,-2 -Audio none -OutputPath "Path/To/Encoded/File.mp4"
.EXAMPLE
## Test run. Encode only 10 frames ##
./FFEncoder.ps1 "~/Movies/Ex.Machina.2014.DTS-HD.mkv" -CRF 20.0 -Audio copy -Subtitles none -TestFrames 10 -OutputPath "~/Movies/Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Using shorthand parameter aliases ##
.\FFEncoder.ps1 "C:\Users\user\Videos\Ex.Machina.2014.DTS-HD.mkv" -c 20.5 -a c -dbf -3,-3 -a copyall -s d -o "C:\Users\user\Videos\Ex Machina Test.mkv" -t 500
.EXAMPLE
## Copy English subtitles and all audio streams ##
./FFEncoder.ps1 -i "~/Movies/Ex.Machina.2014.DTS-HD.mkv" -CRF 22.0 -Subtitles eng -Audio copyall -o "~/Movies/Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Copy everything EXCEPT English subtitles and all audio streams ##
./FFEncoder.ps1 -i "~/Movies/Ex.Machina.2014.DTS-HD.mkv" -CRF 22.0 -Subtitles !eng -Audio copyall -o "~/Movies/Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Copy existing AC3 stream, or transcode to AC3 if no existing streams are found ##
.\FFEncoder.ps1 -i "C:\Users\user\Videos\Ex.Machina.2014.DTS-HD.mkv" -Audio ac3 -Subtitles default -o "C:\Users\user\Videos\Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Copy the primary audio stream and transcode a second audio stream to FDK AAC 2.0 using VBR 5 ##
.\FFEncoder.ps1 -i "C:\Users\user\Videos\Ex.Machina.2014.DTS-HD.mkv" -Audio c -Audio 2 faac -ABitrate2 5 -Stereo2 -o "C:\Users\user\Videos\Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Encode the video at 25 mb/s using the -VideoBitrate parameter ##
.\FFEncoder.ps1 -i "C:\Users\user\Videos\Ex.Machina.2014.DTS-HD.mkv" -Audio copy -VideoBitrate 25M -OutputPath "C:\Users\user\Videos\Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Adjust psycho visual settings and aq-mode level/strength ##
./FFEncoder.ps1 "~/Movies/Ex.Machina.2014.DTS-HD.mkv" -PsyRd 4.0 -PsyRdoq 1.50 -AqMode 1 -AqStrength 0.90 -o "C:\Users\user\Videos\Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Pass additional ffmpeg arguments not covered by other script parameters ##
.\FFEncoder.ps1 -i "C:\Users\user\Videos\Ex.Machina.2014.DTS-HD.mkv" -CRF 18 -FFMpegExtra @{'-t' = 20}, 'nostats' -o "C:\Users\user\Videos\Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## Pass additional x265 arguments not covered by other script parameters ##
./FFEncoder.ps1 "~/Movies/Ex.Machina.2014.DTS-HD.mkv" -PsyRd 4.0 -CRF 20 -x265Extra @{'max-merge' = 1} -o "C:\Users\user\Videos\Ex Machina (2014) DTS-HD.mkv"
.EXAMPLE
## ScaleKernel 2160p video down to 1080p using zscale and spline36 ##
.\FFEncoder "$HOME\Videos\Ex.Machina.2014.DTS-HD.2160p.mkv" -Scale zscale -ScaleFilter spline36 -Res 1080p -CRF 18 -o "$HOME\Videos\Ex Machina (2014) DTS-HD 1080p.mkv"
.EXAMPLE
## Use a Vapoursynth script as input
.\FFEncoder 'in.mkv' -VapourSynthScript "$HOME/script.vpy -CRF 18 -o 'out.mkv'"
.INPUTS
HD/FHD/UHD video file
Vapoursynth Script
.OUTPUTS
Crop file
Log file(s)
Intermediary/temporary files
Encoded video file
.NOTES
For script binaries to work, they must be included in the system PATH (consult OS documentation for more information):
- ffmpeg
- deew / dee
- mkvmerge
- mkvextract
- x265
.PARAMETER Help
Displays help information for the script
.PARAMETER TestFrames
Performs a test encode with the number of frames provided
.PARAMETER TestStart
Starting point for test encodes. Accepts 3 formats:
- 00:01:30 - Sexagesimal time format. This is the default
- 200f - Frame specifier. Add the 'f' modifier after the frame number to specify a starting frame. Accurate to +/- 1 frame
- 200t - Time specifier, in seconds. Add the 't' modifier after the number to specify a starting time. Accepts floating point values
.PARAMETER InputPath
Location of the file to be encoded
.PARAMETER Audio
Audio selection options. FFEncoder has several audio options:
* copy/c - Pass through the primary audio stream without re-encoding
* copyall/ca - Pass through all audio streams without re-encoding
* none/n - No audio will be copied
* aac - Convert primary audio stream to AAC. Default setting is 512 kb/s for multi-channel, and 128 kb/s for stereo
* fdkaac/faac - Convert primary audio stream to AAC using FDK AAC. Default setting is -vbr 3
* aac_at - Convert the primary audio stream to AAC using Apple's Core AudioToolbox encoder. MacOS only
* dts - Convert/copy DTS to the output file. If -AudioBitrate is present, the stream will be transcoded. If not, any existing DTS stream will be copied
* ac3 - Convert/copy AC3 to the output file. If -AudioBitrate is present, the stream will be transcoded. If not, any existing AC3 stream will be copied
* eac3 - Convert/copy E-AC3 to the output file. If -AudioBitrate is present, the stream will be transcoded. If not, any existing E-AC3 stream will be copied
* flac/f - Convert the primary audio stream to FLAC lossless audio
* Stream # - Copy an audio stream by its identifier in ffmpeg
* dee_ddp/dee_eac3 - Encode Dolby Digital Plus audio using Dolby Encoding Engine (requires external software, not included)
* dee_ddp_51 - Force encode Dolby Digital Plus 5.1 audio using Dolby Encoding Engine (requires external software, not included)
* dee_dd/dee_ac3 - Encode Dolby Digital audio using Dolby Encoding Engine (requires external software, not included)
* dee_thd - Encode TrueHD audio using Dolby Encoding Engine (requires external software, not included)
.PARAMETER AudioBitrate
Specifies the bitrate for the chosen codec (in kb/s). Values 1-5 are used to signal -vbr with libfdk_aac or special options with aac_at
.PARAMETER Stereo
Switch to downmix the paired audio stream to stereo
.PARAMETER Subtitles
Supports passthrough of embedded subtitles with the following options and languages:
- All - "all" / "a"
- None - "none" / "n"
- Default (first) - "default" / "d"
- English - "eng"
- French - "fra"
- German - "ger"
- Spanish - "spa"
- Dutch - "dut" / "nld"
- Danish - "dan"
- Finnish - "fin"
- Norwegian - "nor"
- Czech - "cze"
- Polish - "pol"
- Chinese - "chi" / "zho"
- Korean - "kor"
- Greek - "gre" / "ell"
- Romanian - "rum"
- Arabic - "ara"
- Bulgarian - "bul"
- Estonian - "est"
- Indonesian - "ind"
- Hindi - "hin"
- Turkish - "tur"
- Vietnamese - "vie"
- Thai - "tha"
- Slovenian - "slv"
- Hebrew - "heb"
Prefixing a '!' before any language will return all subtitles EXCLUDING that language
.PARAMETER Preset
The x265 preset to be used. Ranges from "placebo" (slowest) to "ultrafast" (fastest). Slower presets improve quality by enabling additional, more expensive, x265 parameters at the expensive of encoding time.
Recommended presets (depending on source and purpose) are slow, medium, or fast.
.PARAMETER CRF
Constant rate factor setting for video rate control. This setting attempts to keep quality consistent from frame to frame, and is most useful for targeting a specific quality level.
Ranges from 0.0 to 51.0. Lower values equate to a higher bitrate (better quality). Recommended: 14.0 - 24.0. At very low values, the output file may actually grow larger than the source.
CRF 4.0 is considered mathematically lossless in x265 (vs. CRF 0.0 in x264)
.PARAMETER ConstantQP
Constant quantizer rate control mode. Forces a consistent QP throughout the encode. Generally not recommended outside of testing.
.PARAMETER VideoBitrate
Average bitrate (ABR) setting for video rate control. This can be used as an alternative to CRF rate control, and is most useful for targeting a specific file size (bitrate / duration).
Use the 'K' suffix to denote kb/s, or the 'M' suffix for mb/s:
ex: 10000k (10,000 kb/s)
ex: 10m (10 mb/s) | 10.5M (10.5 mb/s)
.PARAMETER Pass
The number of passes to perform when running an average bitrate encode using the VIdeoBitrate parameter
.PARAMETER FirstPassType
Tuning option for the first pass of a two pass encode. Accepted values (from slowest to fastest): Default/d, Custom/c, Fast/f. Default value is 'Default'/'d'.
x265 only, as x264 automatically reduces certain settings during the first pass unless the slow-firstpass parameter is used
.PARAMETER Deblock
Deblock filter settings. The first value represents strength, and the second value represents frequency
.PARAMETER AqMode
x265 AQ mode setting. Ranges from 0 (disabled) - 3 (x264) / 4 (x265). See encoder documentation for more info on AQ Modes and how they work
.PARAMETER AqStrength
Adjusts the adaptive quantization offsets for AQ. Raising AqStrength higher than 2 will drastically affect the QP offsets, and can lead to high bitrates
.PARAMETER PsyRd
Psycho-visual enhancement. Higher values of PsyRd/PsyRDO strongly favor similar energy over blur.
x265: Expects a decimal value
ex: 1.00
x264: You may pass psy-rdo and psy-trellis as one value like you normally would (MUST be quoted or errors will occur), or pass only psy-rdo
ex: '1.00,0.05' - Passing both psy-rdo and psy-trellis
ex: 1.00 - Passing only psy-rdo
.PARAMETER PsyRdoq
Psycho-visual enhancement. Favors high AC energy in the reconstructed image, but it less efficient than PsyRd.
x264: This parameter can also be used for psy-trellis
.PARAMETER NoiseReduction
Filter to help reduce high frequency noise (such as film grain).
x265: First value represents intra frames, and the second value represents inter frames
x264: Pass a single integer value
.PARAMETER TuDepth
Recursion depth for transform units (TU). Accepted values are 1-4. First value represents intra depth, and the second value represents inter depth.
Default values are 1, 1 (x265 only)
.PARAMETER LimitTu
Early exit condition for TU depth recursion. Accepted values are 0-4. Default is 0 (x265 only)
.PARAMETER BFrames
The number of consecutive B-Frames within a GOP. This is especially helpful for test encodes to determine the ideal number of B-Frames to use
.PARAMETER BIntra
Enables the evaluation of intra modes in B slices. Accepted values are 0 (off) or 1 (on). Has a minor impact on performance (x265 only)
.PARAMETER Subme
The amount of subpel motion refinement to perform. At values larger than 2, chroma residual cost is included. Has a large performance impact
.PARAMETER Merange
Sets the motion estimation range. Higher values result in a more thorough motion vector search during inter-frame prediction
.PARAMETER Ref
Sets the number of reference frames used. Default value is based on the preset used. For x264, this may affect hardware compatibility
.PARAMETER Tree
Enable or disable encoder-specific motion vector lookahead algorithm. 1 is enabled, 0 is disabled
.PARAMETER StrongIntraSmoothing
Enables/disables strong-intra-smoothing. Default enabled (x265 only)
.PARAMETER RCLookahead
Sets the rate control lookahead size. Higher values will use more memory, but provide better compression efficiency
.PARAMETER Threads
Set the number of threads used by the encoder. More threads equate to faster encoding, but with slightly decreased quality. If no value is passed, the encoder default
is used based on the number of logical CPU cores available to the system. If you aren't sure what this does, don't set it
.PARAMETER Level
Specifies the encoder level to use. Default value is unset (let the encoder decide)
.PARAMETER VBV
Sets video buffering verifier options. If passed, requires 2 arguments in the following order: (vbv-bufsize, vbv-maxrate). Default is unset (decided by the encoder level)
.PARAMETER QComp
Sets the quantizer curve compression factor, which effects the bitrate variance throughout the encode
.PARAMETER OutputPath
Location of the encoded output video file
.PARAMETER FFMpegExtra
Pass additional settings to ffmpeg that are not supplied by the script. Accepts single array arguments or hashtables in the form of <key = value>.
WARNING: The script does not check for valid syntax, and assumes you know what you're doing
.PARAMETER EncoderExtra
Pass additional settings to the encoders that are not supplied by the script. Settings must be passed as a hashtable in the form of <key = value>.
WARNING: The script does not check for valid syntax, and assumes you know what you're doing
.PARAMETER ScaleKernel
Upscale/downscale input to a different resolution using the specified convolution kernel
.PARAMETER ScaleFilter
Filtering method used for rescaling input with the -Scale parameter. Compatible arguments:
- scale: fast_bilinear, neighbor, area, gauss, sinc, spline, bilinear, bicubic, lanczos
- zscale: point, spline16, spline36, bilinear, bicubic, lanczos
If an argument is chosen which exists in both sets, zscale will be used if available
.PARAMETER Unsharp
Enable the unsharp filter and specify the search range. Use one of the presets specified in the project wiki, in the form:
<luma|chroma|yuv>_<small|medium|large>
or pass a custom filter string as:
'custom=<filter string>'
Mandatory parameter for sharpening/blurring a video source.
.PARAMETER UnsharpStrength
Sets the strength of the unsharp filter. Use one of the presets defined in the project wiki, in the form: <sharpen|blur>_<mild|medium|strong>
.PARAMETER Resolution
Upscale/downscale resolution used with the -Scale parameter. Default value is 1080p (1920 x 1080)
.PARAMETER SkipDolbyVision
Skip Dolby Vision encoding, even if metadata is present
.PARAMETER SkipHDR10Plus
Skip HDR10+ encoding, even if metadata is present
.PARAMETER HDR10PlusSkipReorder
Fix for HDR10+ decoding order. Whether this parameter should be used must be validated manually
.PARAMETER ExitOnError
Converts certain non-terminating errors to terminating ones, such as input validation prompts. This can prevent blocking on automation when one
running instance encounters an error
.PARAMETER DisableProgress
Switch to disable the progress bar during encoding
.PARAMETER RemoveFiles
Switch to delete extraneous files generated by the script (crop file, log file, etc.). The input, output, and report files will not be deleted
.PARAMETER Deinterlace
Deinterlacing filter using yadif. Currently only works with CRF encoding
.PARAMETER GenerateReport
Generates a user friendly report file with important encoding metrics pulled from the log file. File is saved with a .rep extension
.PARAMETER GenerateMKVTagFile
Generate an XML tag file for MKV containers using the TMDB API. Requires a valid TMDB API key
.PARAMETER CompareVMAF
Switch to enable a VMAF comparison. Mandatory to enable this feature
.PARAMETER EnablePSNR
VMAF option. Enables Peak Signal to Noise Ratio (PSNR) evaluation
.PARAMETER EnableSSIM
VMAF option. Enables Structural Similarity Index Measurement (SSIM) evaluation
.PARAMETER VMAFResizeKernel
VMAF option. Specify which kernel to use for resizing the distorted stream (default is bicubic)
.PARAMETER LogFormat
Specify the log format for VMAF. Options:
- json
- csv
- sub
- xml
.PARAMETER VapourSynthScript
Pass a VapourSynth script for filtering. Note that all filtering (including cropping) must be done in the VS script
.lINK
Check out the full documentation and script wiki on GitHub - https://github.com/patrickenfuego/FFEncoder
.LINK
FFMpeg documentation - https://ffmpeg.org
.LINK
x265 HEVC Documentation - https://x265.readthedocs.io/en/master/introduction.html
#>
using namespace System.IO
[CmdletBinding(DefaultParameterSetName = 'CRF')]
param (
[Parameter(Mandatory = $true, ParameterSetName = 'Help')]
[Alias('H')]
[switch]$Help,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('x264', 'x265')]
[Alias('Enc')]
[string]$Encoder = 'x265',
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'CRF', HelpMessage='Enter full path to source file')]
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'VMAF', HelpMessage='Enter full path to source file')]
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'PASS', HelpMessage='Enter full path to source file')]
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'QP', HelpMessage='Enter full path to source file')]
[ValidateScript( { if (Test-Path $_) { $true } else { throw 'Input path does not exist' } } )]
[Alias('I', 'Reference', 'Source')]
[string]$InputPath,
[Parameter(Mandatory = $false, Position = 1, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, Position = 1, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, Position = 1, ParameterSetName = 'QP')]
[ValidateScript(
{
if (!(Test-Path $_)) {
throw "Could not locate Vapoursynth script. Check the script path and try again"
}
if (($(ffmpeg 2>&1) -join ' ') -notmatch 'vapoursynth') {
throw "ffmpeg was not compiled with Vapoursynth. Ensure the '--enable-vapoursynth' flag was set during compilation"
}
$true
}
)]
[Alias('VSScript', 'VPY')]
[string]$VapourSynthScript,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('copy', 'c', 'copyall', 'ca', 'aac', 'none', 'n', 'ac3', 'dee_dd', 'dee_ac3', 'dd', 'dts', 'flac', 'f',
'eac3', 'ddp', 'dee_ddp', 'dee_eac3', 'dee_ddp_51', 'dee_eac3_51', 'dee_thd', 'fdkaac', 'faac', 'aac_at',
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)]
[Alias('A')]
[string]$Audio = 'copy',
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(-1, 3000)]
[Alias('AB', 'ABitrate')]
[int]$AudioBitrate,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('2CH', 'ST')]
[switch]$Stereo,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('copy', 'c', 'copyall', 'ca', 'aac', 'none', 'n', 'ac3', 'dee_dd', 'dee_ac3', 'dd', 'dts', 'flac', 'f',
'eac3', 'ddp', 'dee_ddp', 'dee_eac3', 'dee_ddp_51', 'dee_eac3_51', 'dee_thd', 'fdkaac', 'faac', 'aac_at',
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
)]
[Alias('A2')]
[string]$Audio2 = "none",
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(-1, 3000)]
[Alias('AB2', 'ABitrate2')]
[int]$AudioBitrate2,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('2CH2', 'ST2')]
[switch]$Stereo2,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('all', 'a', 'copyall', 'ca', 'none', 'default', 'd', 'n', 'eng', 'fre', 'ger', 'spa', 'dut', 'dan',
'fin', 'nor', 'cze', 'pol', 'chi', 'zho', 'kor', 'gre', 'rum', 'rus', 'swe', 'est', 'ind', 'slv', 'tur', 'vie',
'hin', 'heb', 'ell', 'bul', 'ara', 'por', 'nld', 'tha',
'!eng', '!fre', '!ger', '!spa', '!dut', '!dan', '!fin', '!nor', '!cze', '!pol', '!chi', '!zho', '!kor', '!ara',
'!rum', '!rus', '!swe', '!est', '!ind', '!slv', '!tur', '!vie', '!hin', '!heb', '!gre', '!ell', '!bul', '!por',
'!nld', '!tha'
)]
[Alias('S', 'Subs')]
[string]$Subtitles = 'default',
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet("placebo", "veryslow", "slower", "slow", "medium", "fast", "faster", "veryfast", "superfast", "ultrafast")]
[Alias('P')]
[string]$Preset = 'slow',
[Parameter(Mandatory = $true, ParameterSetName = 'CRF', HelpMessage = 'Enter CRF value (1-51)')]
[ValidateRange(0.0, 51.0)]
[Alias('C')]
[double]$CRF,
[Parameter(Mandatory = $true, ParameterSetName = 'QP')]
[ValidateRange(0, 51)]
[Alias('QP')]
[int]$ConstantQP,
[Parameter(Mandatory = $true, ParameterSetName = 'PASS', HelpMessage = 'Enter 2-pass Average Bitrate (Ex: 5M or 5000k)')]
[Alias('VBitrate')]
[ValidateScript(
{
$_ -cmatch "(?<num>\d+\.?\d{0,2})(?<suffix>[K k M]+)"
if ($Matches) {
switch ($Matches.suffix) {
'K' {
if ($Matches.num -gt 99000 -or $Matches.num -lt 1000) {
throw "Bitrate out of range. Must be between 1,000-99,000 kb/s"
}
else { $true }
}
'M' {
if ($Matches.num -gt 99 -or $Matches.num -le 1) {
throw "Bitrate out of range. Must be between 1-99 mb/s"
}
else { $true }
}
default { throw "Invalid Suffix. Suffix must be 'K/k' (kb/s) or 'M' (mb/s)" }
}
}
else { throw "Invalid bitrate input. Example formats: 10000k (10,000 kb/s) | 10M (10 mb/s)" }
}
)]
[string]$VideoBitrate,
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[ValidateRange(1, 2)]
[int]$Pass = 2,
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[ValidateSet('Default', 'd', 'Fast', 'f', 'Custom', 'c')]
[Alias('FPT', 'PassType')]
[string]$FirstPassType = 'Default',
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(-6, 6)]
[ValidateCount(2, 2)]
[Alias('DBF')]
[int[]]$Deblock = @(-2, -2),
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 4)]
[Alias('AQM')]
[int]$AqMode,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0.0, 3.0)]
[Alias('AQS')]
[double]$AqStrength = 1.00,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('PRD', 'PsyRDO')]
[string]$PsyRd,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0.0, 50.0)]
[Alias('PRQ', 'PsyTrellis')]
[double]$PsyRdoq,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(1, 16)]
[int]$Ref,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 1)]
[Alias('MBTree', 'CUTree')]
[int]$Tree = 1,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(1, 32768)]
[Alias('MR')]
[int]$Merange,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 2000)]
[ValidateCount(1, 2)]
[Alias('NR')]
[int[]]$NoiseReduction = @(0, 0),
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(1, 4)]
[ValidateCount(2, 2)]
[Alias('TU')]
[int[]]$TuDepth = @(1, 1),
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(1, 4)]
[Alias('LTU')]
[int]$LimitTu = 0,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0.0, 1.0)]
[Alias("Q")]
[double]$QComp = 0.60,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 16)]
[Alias('B')]
[int]$BFrames,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 1)]
[Alias('BINT')]
[int]$BIntra,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 11)]
[Alias('SM', 'Subpel')]
[int]$Subme,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 1)]
[Alias('SIS')]
[int]$StrongIntraSmoothing = 1,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('1', '1b', '2', '1.1', '1.2', '1.3', '2.1', '21', '2.2', '3.1', '3.2', '4', '4.1', '4.2', '41',
'5', '5.1', '51', '5.2', '52', '6', '6.1', '61', '6.2', '62', '8.5', '85')]
[Alias('L')]
[string]$Level,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateCount(2, 2)]
[Alias('VideoBuffer')]
[int[]]$VBV,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(1, 64)]
[Alias('FrameThreads')]
[int]$Threads,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateRange(0, 250)]
[Alias('RCL', 'Lookahead')]
[int]$RCLookahead,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('FE', 'FFExtra')]
[array]$FFMpegExtra,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('Extra')]
[hashtable]$EncoderExtra,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('T', 'Test')]
[int]$TestFrames,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('Start', 'TS')]
[string]$TestStart = '00:01:30',
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('Del', 'RM')]
[switch]$RemoveFiles,
# Filtering related parameters
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateScript(
{
if ($_.Count -eq 0) { throw "NLMeans Hashtable must contain at least 1 value" }
$flag = $false
foreach ($k in $_.Keys) {
if ($k -notin 's', 'p', 'pc', 'r', 'rc') {
throw "Invalid key. Valid keys are 's', 'p', 'pc', 'r', 'rc'"
}
else { $flag = $true }
}
if ($flag = $true) { $true }
else { throw "Invalid NLMeans hashtable. See https://ffmpeg.org/ffmpeg-filters.html#nlmeans-1" }
}
)]
[Alias('NL')]
[hashtable]$NLMeans,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ArgumentCompletions(
'luma_small', 'luma_medium', 'luma_large', 'chroma_small',
'chroma_medium', 'chroma_large', 'yuv_small', 'yuv_medium',
'yuv_large'
)]
[Alias('U')]
[string]$Unsharp,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet(
'sharpen_mild', 'sharpen_medium', 'sharpen_strong',
'blur_mild', 'blur_medium', 'blur_strong'
)]
[Alias('UStrength')]
[string]$UnsharpStrength = 'sharpen_mild',
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('DI')]
[switch]$Deinterlace,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('point', 'spline16', 'spline36', 'bilinear', 'bicubic', 'lanczos',
'fast_bilinear', 'neighbor', 'area', 'gauss', 'sinc', 'spline', 'bicublin')]
[Alias('ResizeKernel')]
[string]$ScaleKernel = 'bilinear',
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateSet('2160p', '1080p', '720p')]
[Alias('Res', 'R')]
[string]$Resolution,
# Utility parameters
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[alias('Report', 'GR')]
[switch]$GenerateReport,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('NoDV', 'SDV')]
[switch]$SkipDolbyVision,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[alias('No10P', 'STP')]
[switch]$SkipHDR10Plus,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('SkipReorder')]
[switch]$HDR10PlusSkipReorder,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[Alias('Exit')]
[switch]$ExitOnError,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[alias('NoProgressBar')]
[switch]$DisableProgress,
[Parameter(Mandatory = $false, ParameterSetName = 'CRF')]
[Parameter(Mandatory = $false, ParameterSetName = 'PASS')]
[Parameter(Mandatory = $false, ParameterSetName = 'QP')]
[ValidateScript(
{
$flag = $false
if ($null -eq $_['APIKey']) {
throw "MKV Tag Hashtable must include an APIKey"
}
foreach ($k in $_.Keys) {
if ($k -notin 'APIKey', 'Path', 'Title', 'Year', 'Properties', 'SkipProperties', 'NoMux', 'AllowClobber') {
throw "Invalid key. Valid keys are 'APIKey', 'Path', 'Title', 'Year', 'Properties', 'SkipProperties', 'NoMux', 'AllowClobber'"
}
else { $flag = $true }
}
if ($flag) { $true }
else { throw "Invalid MKV Tag hashtable" }
}
)]
[Alias('CreateTagFile')]
[hashtable]$GenerateMKVTagFile,
[Parameter(Mandatory = $true, ParameterSetName = 'CRF', HelpMessage = 'Enter full path to encoded output file')]
[Parameter(Mandatory = $true, ParameterSetName = 'VMAF', HelpMessage = 'Enter full path to encoded output file')]
[Parameter(Mandatory = $true, ParameterSetName = 'PASS', HelpMessage = 'Enter full path to encoded output file')]
[Parameter(Mandatory = $true, ParameterSetName = 'QP', HelpMessage = 'Enter full path to encoded output file')]
[ValidateNotNullOrEmpty()]
[Alias('O', 'Encode', 'Distorted')]
[string]$OutputPath,
## VMAF-Specific Parameters
[Parameter(Mandatory = $true, ParameterSetName = 'VMAF')]
[Alias('VMAF', 'EnableVMAF')]
[switch]$CompareVMAF,
[Parameter(Mandatory = $false, ParameterSetName = 'VMAF')]
[alias('SSIM')]
[switch]$EnableSSIM,
[Parameter(Mandatory = $false, ParameterSetName = 'VMAF')]
[alias('PSNR')]
[switch]$EnablePSNR,
[Parameter(Mandatory = $false, ParameterSetName = 'VMAF')]
[ValidateSet('json', 'xml', 'csv', 'sub')]
[Alias('LogType', 'VMAFLog')]
[string]$LogFormat = 'json',
[Parameter(Mandatory = $false, ParameterSetName = 'VMAF')]
[ValidateSet('point', 'spline16', 'spline36', 'bilinear', 'bicubic', 'lanczos',
'fast_bilinear', 'neighbor', 'area', 'gauss', 'sinc', 'spline', 'bicublin')]
[Alias('VMAFKernel')]
[string]$VMAFResizeKernel = 'bicubic'
)
#########################################################
# Function Definitions #
#########################################################
# Returns an object containing the paths needed throughout the script
function Set-ScriptPaths ([hashtable]$OS) {
if ($InputPath -match "(?<root>.*(?:\\|\/)+)(?<title>.*)\.(?<ext>[a-z 2 4]+)") {
$root = $Matches.root
$title = $Matches.title
$ext = $Matches.ext
if ($OutputPath -match "(?<oRoot>.*(?:\\|\/)+)(?<oTitle>.*)\.(?<oExt>[a-z 2 4]+)") {
$oRoot = $Matches.oRoot
$oTitle = $Matches.oTitle
$oExt = $Matches.oExt
}
# If regex match can't be made on the output path, use input matches instead
else {
$oRoot = $root
$oTitle = $title
$oExt = $ext
}
# Creating path strings used throughout the script
$cropPath = [Path]::Join($root, "$title`_crop.txt")
$logPath = [Path]::Join($root, "$title`_encode.log")
$x265Log = [Path]::Join($root, "x265_2pass.log")
$stereoPath = [Path]::Join($root, "$oTitle`_stereo.$oExt")
$reportPath = [Path]::Join($root, "$oTitle.rep")
$hdr10PlusPath = [Path]::Join($root, "metadata.json")
$dvPath = [Path]::Join($root, "rpu.bin")
$hevcPath = [Path]::Join($oRoot, "$oTitle.hevc")
}
# Regex match could not be made on the folder pattern
else {
Write-Host "Could not match root folder pattern. Using OS default path instead..." @warnColors
Write-Host $os.OperatingSystem "detected. Using path: <$($os.DefaultPath)>"
# Creating path strings if regex match fails - use OS default
$cropPath = [Path]::Join($os.DefaultPath, "crop.txt")
$logPath = [Path]::Join($os.DefaultPath, "encode.log")
$x265Log = [Path]::Join($os.DefaultPath, "x265_2pass.log")
$stereoPath = [Path]::Join($os.DefaultPath, "stereo.mkv")
$reportPath = [Path]::Join($os.DefaultPath, "$InputPath.rep")
$hdr10PlusPath = [Path]::Join($os.DefaultPath, "metadata.json")
$dvPath = [Path]::Join($os.DefaultPath, "rpu.bin")
$hevcPath = [Path]::Join($os.DefaultPath, "$InputPath.hevc")
}
if ($psReq) {
Write-Host "Crop file path is: $($PSStyle.Foreground.Cyan+$PSStyle.Underline)$cropPath"
Write-Host ""
}
else {
Write-Host "Crop file path is: " -NoNewline
Write-Host "<$cropPath>" @emphasisColors
Write-Host ""
}
# Check for existing log - concurrent encodes of same source
if ([File]::Exists($logPath) -and
((Get-Process 'ffmpeg' -ErrorAction SilentlyContinue) -or
(Get-Process 'x265*' -ErrorAction SilentlyContinue))) {
# Check if a process is writing to the current log file
$length1 = (Get-Content $logPath).Length
Start-Sleep -Seconds 1.2
$length2 = (Get-Content $logPath).Length
if ($length2 -gt $length1) {
$logCount = (Get-ChildItem $root -Filter '*encode*.log' | Measure-Object).Count
if ($logCount) {
Write-Host "Existing encode detected...creating a separate log file" @warnColors
$logPath = [Path]::Join($root, "$title`_encode$($logCount + 1).log")
}
}
}
$pathObject = @{
InputFile = $InputPath
Root = $root
Extension = $oExt
RemuxPath = $remuxPath
StereoPath = $stereoPath
CropPath = $cropPath
LogPath = $logPath
X265Log = $x265Log
Title = $oTitle
ReportPath = $reportPath
HDR10Plus = $hdr10PlusPath
DvPath = $dvPath
HevcPath = $hevcPath
OutputFile = $OutputPath
}
if ($VapoursynthScript) {
$pathObject['VPY'] = $VapoursynthScript
}
Write-Verbose "PATHS OBJECT:`n $($pathObject | Out-String)"
return $pathObject
}
## End Functions ##
#########################################################
# Main Script Logic #
#########################################################
<#
SETUP
Help
Console config
Verbose preference
Verify PowerShell version
Import Module
Import Config File contents
#>
# Print help content and exit
if ($Help) {
Get-Help .\FFEncoder.ps1 -Full
exit 0
}
# Enable verbose logging if passed. Cascade down setVerbose
if ($PSBoundParameters['Verbose']) {
$VerbosePreference = 'Continue'
$ErrorView = 'NormalView'
$Global:setVerbose = $true
}
else {
$VerbosePreference = 'SilentlyContinue'
$Global:setVerbose = $false
}
# Set console options for best experience
$Global:console = $Host.UI.RawUI
$Global:currentTitle = $console.WindowTitle
$console.ForegroundColor = 'White'
$console.BackgroundColor = 'Black'
$console.WindowTitle = 'FFEncoder'
# Reset intercept if previous exit wasn't clean
[console]::TreatControlCAsInput = $false
# Import FFTools module
Import-Module -Name "$PSScriptRoot\modules\FFTools" -Force
Write-Verbose "`n`n---------------------------------------"
# Import config file options
$params = @{
EncoderExtra = $EncoderExtra
FFMpegExtra = $FFMpegExtra
Encoder = $Encoder
Verbose = $setVerbose
}
$EncoderExtra, $FFMpegExtra, $scriptHash, $tagHash, $vmafHash = Import-Config @params
# Source version functions
. $([Path]::Join($ScriptsDirectory, 'VerifyVersions.ps1')).ToString()
# Verify the current version of pwsh & exit if version not satisfied
$Global:psReq = Confirm-PoshVersion
# Check for updates to FFencoder and prompt to download if git is available
Update-FFEncoder -CurrentRelease $release -Verbose:$setVerbose
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$startTime = (Get-Date).ToLocalTime()
# Write the welcome banner
Write-Host "----------------------------------------------------------------------------------------------" @emphasisColors
if ($psReq) {
Write-Host "$($PSStyle.Foreground.FromRGB(92, 255, 114))$($PSStyle.Bold)$($banner1)$($PSStyle.Reset)"
Write-Host "$($PSStyle.Foreground.FromRGB(97, 30, 164))$($PSStyle.Bold)$($banner2)$($PSStyle.Reset)"
}
else {
Write-Host $banner1 -ForegroundColor 'Green' -BackgroundColor 'Black'
Write-Host $banner2 -ForegroundColor 'Magenta' -BackgroundColor 'Black'
}
Write-Host "----------------------------------------------------------------------------------------------" @emphasisColors
Write-Host "Start Time: $startTime`n"
if ($PSBoundParameters['CompareVMAF']) {
Write-Host " $("`u{25c7}" * 3) VMAF Selected $("`u{25c7}" * 3)" @emphasisColors
Write-Host "$("`u{25c7}" * 4) STARTING ASSESSMENT $("`u{25c7}" * 4)" @progressColors
Write-Host ""
# Check params from config file
if ($vmafHash) {
foreach ($item in $vmafHash.GetEnumerator()) {
if (!$PSBoundParameters[$item.Name]) {
Set-Variable "$($item.Name)" -Value $item.Value
Write-Verbose "VMAF Variable set: $($item.Name) = $(Get-Variable "$($item.Name)" -ValueOnly)"
}
else {
Write-Verbose "VMAF variable $($item.Name) set via parameter. Skipping..."
}
}
Write-Host ""
}
$params = @{
Source = $InputPath
Encode = $OutputPath
SSIM = $EnableSSIM
PSNR = $EnablePSNR
LogFormat = $LogFormat
ResizeKernel = $VMAFResizeKernel
Verbose = $setVerbose
}
try {
Invoke-VMAF @params
$console.WindowTitle = $currentTitle
exit 0
}
catch {
Write-Error "An exception occurred during VMAF comparison: $($_.Exception.Message)"
$console.WindowTitle = $currentTitle
exit 43
}
}
# Set switch params from config if not passed via param
if ($scriptHash) {
foreach ($item in $scriptHash.GetEnumerator()) {
if (!$PSBoundParameters[$item.Name]) {
Set-Variable "$($item.Name)" -Value $item.Value
Write-Verbose "Switch Variable set: $($item.Name) = $(Get-Variable "$($item.Name)" -ValueOnly)"
}
else {
Write-Verbose "Switch variable $($item.Name) set via parameter. Skipping..."
}
}
Write-Host ""
}
# Set tag generator hash. Parameter option overrides config
if (!$PSBoundParameters['GenerateMKVTagFile'] -and $tagHash) {
$GenerateMKVTagFile = $tagHash
}
# Validate and set script params from config file values
if ($EncoderExtra) {
Write-Host "Parsing encoder configuration...`n" @progressColors
$removeKeys = [System.Collections.ArrayList]::new()
$x = $PsStyle.Bold + "`u{2717}" + $PSStyle.BoldOff
$c1 = $PSStyle.Foreground.Blue
$c2 = $PSStyle.Foreground.Red
# Create hash mapping between script params and associated encoder settings
$paramHash = @{
'Deblock' = 'deblock'
'AqMode' = 'aq-mode'
'AqStrength' = 'aq-strength'
'PsyRd' = 'psy-rd'
'PsyRdoq' = 'psy-rdoq'
'Ref' = 'ref'
'Tree' = 'cutree', 'mbtree'
'Merange' = 'Merange'