@@ -213,7 +213,10 @@ function Invoke-FFMpeg {
213
213
[switch ]$DisableProgress
214
214
)
215
215
216
- # Writes the banner information during encoding
216
+ <#
217
+ . SYNOPSIS
218
+ Writes the banner information during encoding
219
+ #>
217
220
function Write-Banner {
218
221
if ($TestFrames ) {
219
222
$startStr = switch - Wildcard ($TestStart ) {
@@ -247,6 +250,31 @@ function Invoke-FFMpeg {
247
250
}
248
251
}
249
252
253
+ <#
254
+ . SYNOPSIS
255
+ Wait for encode jobs to finish if progress bar returns an error
256
+ . DESCRIPTION
257
+ If the progress bar function returns an error, this function will wait for the job
258
+ to complete while intercepting CTRL+C and gracefully exiting if needed
259
+ #>
260
+ function Wait-Completed ($JobName , $Exception ) {
261
+ Write-Host " ERROR: The progress bar failed to initialize`n " @errColors
262
+ Write-Verbose " ERROR: $ ( $Exception.Message ) "
263
+ Write-Verbose " LINE: $ ( $Exception.InvocationInfo.ScriptLineNumber ) "
264
+
265
+ # Intercept ctrl+C for graceful shutdown of jobs
266
+ [console ]::TreatControlCAsInput = $true
267
+ Start-Sleep - Milliseconds 500
268
+ $Host.UI.RawUI.FlushInputBuffer ()
269
+
270
+ while ((Get-Job - Name $JobName ).State -ne ' Completed' ) {
271
+ Watch-ScriptTerminated - Message $exitBanner
272
+ Start-Sleep - Milliseconds 750
273
+ }
274
+
275
+ return
276
+ }
277
+
250
278
# Infer primary language based on streams (for muxing) - NOT always accurate, but pretty close
251
279
$streams = ffprobe $Paths.InputFile - show_entries stream= index:stream_tags= language `
252
280
- select_streams a - v 0 - of compact= p= 0 :nk= 1
@@ -279,7 +307,8 @@ function Invoke-FFMpeg {
279
307
$HDR = Get-HDRMetadata @params
280
308
}
281
309
catch [System.ArgumentNullException ] {
282
- Write-Host " `u{203C} Failed to get HDR metadata: $ ( $_.Exception.Message ) . Metadata will not be copied" @errColors
310
+ $err = $_.Exception.Message
311
+ Write-Host " `u{203C} Failed to get HDR metadata: $err . Metadata will not be copied" @errColors
283
312
$HDR = $null
284
313
}
285
314
}
@@ -397,7 +426,13 @@ function Invoke-FFMpeg {
397
426
RCLookahead = $RCLookahead
398
427
}
399
428
# Set preset based arguments based on user input
400
- $presetParams = Set-PresetParameters - Settings $presetArgs - Preset $Preset - Encoder $Encoder - Verbose:$setVerbose
429
+ $params = @ {
430
+ Settings = $presetArgs
431
+ Preset = $Preset
432
+ Encoder = $Encoder
433
+ Verbose = $setVerbose
434
+ }
435
+ $presetParams = Set-PresetParameters @params
401
436
Write-Verbose " PRESET PARAMETER VALUES:`n $ ( $presetParams | Out-String ) `n "
402
437
403
438
<#
@@ -558,7 +593,12 @@ function Invoke-FFMpeg {
558
593
DolbyVision = $dovi
559
594
Verbose = $setVerbose
560
595
}
561
- Write-EncodeProgress @params
596
+ try {
597
+ Write-EncodeProgress @params
598
+ }
599
+ catch {
600
+ Wait-Completed - JobName ' 1st Pass' - Exception $_.Exception
601
+ }
562
602
}
563
603
564
604
Write-Host
@@ -584,7 +624,12 @@ function Invoke-FFMpeg {
584
624
DolbyVision = $dovi
585
625
Verbose = $setVerbose
586
626
}
587
- Write-EncodeProgress @params
627
+ try {
628
+ Write-EncodeProgress @params
629
+ }
630
+ catch {
631
+ Wait-Completed - JobName ' 2nd Pass' - Exception $_.Exception
632
+ }
588
633
}
589
634
}
590
635
# CRF/One pass x265 encode
@@ -625,7 +670,12 @@ function Invoke-FFMpeg {
625
670
DolbyVision = $dovi
626
671
Verbose = $setVerbose
627
672
}
628
- Write-EncodeProgress @params
673
+ try {
674
+ Write-EncodeProgress @params
675
+ }
676
+ catch {
677
+ Wait-Completed - JobName ' crf' - Exception $_.Exception
678
+ }
629
679
}
630
680
}
631
681
# Mux/convert audio and subtitle streams separately from elementary hevc stream
@@ -727,7 +777,12 @@ function Invoke-FFMpeg {
727
777
Verbose = $setVerbose
728
778
DolbyVision = $dovi
729
779
}
730
- Write-EncodeProgress @params
780
+ try {
781
+ Write-EncodeProgress @params
782
+ }
783
+ catch {
784
+ Wait-Completed - JobName ' ffmpeg 1st Pass' - Exception $_.Exception
785
+ }
731
786
}
732
787
}
733
788
@@ -753,7 +808,12 @@ function Invoke-FFMpeg {
753
808
Verbose = $setVerbose
754
809
DolbyVision = $dovi
755
810
}
756
- Write-EncodeProgress @params
811
+ try {
812
+ Write-EncodeProgress @params
813
+ }
814
+ catch {
815
+ Wait-Completed - JobName ' ffmpeg 2nd Pass' - Exception $_.Exception
816
+ }
757
817
}
758
818
}
759
819
# CRF encode
@@ -792,8 +852,9 @@ function Invoke-FFMpeg {
792
852
}
793
853
# Should be unreachable. Throw error and exit script if rate control cannot be detected
794
854
else {
855
+ $msg = ' Rate control method could not be determined from input parameters'
795
856
$params = @ {
796
- Exception = [System.FieldAccessException ]::new(' Rate control method could not be determined from input parameters ' )
857
+ Exception = [System.FieldAccessException ]::new($msg )
797
858
Category = ' InvalidResult'
798
859
TargetObject = $RateControl
799
860
ErrorId = 101
@@ -821,7 +882,12 @@ function Invoke-FFMpeg {
821
882
DolbyVision = $dovi
822
883
Verbose = $setVerbose
823
884
}
824
- Write-EncodeProgress @params
885
+ try {
886
+ Write-EncodeProgress @params
887
+ }
888
+ catch {
889
+ Wait-Completed - JobName ' ffmpeg' - Exception $_.Exception
890
+ }
825
891
}
826
892
827
893
# Remove ffmpeg jobs
0 commit comments