@@ -213,7 +213,10 @@ function Invoke-FFMpeg {
213213 [switch ]$DisableProgress
214214 )
215215
216- # Writes the banner information during encoding
216+ <#
217+ . SYNOPSIS
218+ Writes the banner information during encoding
219+ #>
217220 function Write-Banner {
218221 if ($TestFrames ) {
219222 $startStr = switch - Wildcard ($TestStart ) {
@@ -247,6 +250,31 @@ function Invoke-FFMpeg {
247250 }
248251 }
249252
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+
250278 # Infer primary language based on streams (for muxing) - NOT always accurate, but pretty close
251279 $streams = ffprobe $Paths.InputFile - show_entries stream= index:stream_tags= language `
252280 - select_streams a - v 0 - of compact= p= 0 :nk= 1
@@ -279,7 +307,8 @@ function Invoke-FFMpeg {
279307 $HDR = Get-HDRMetadata @params
280308 }
281309 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
283312 $HDR = $null
284313 }
285314 }
@@ -397,7 +426,13 @@ function Invoke-FFMpeg {
397426 RCLookahead = $RCLookahead
398427 }
399428 # 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
401436 Write-Verbose " PRESET PARAMETER VALUES:`n $ ( $presetParams | Out-String ) `n "
402437
403438 <#
@@ -558,7 +593,12 @@ function Invoke-FFMpeg {
558593 DolbyVision = $dovi
559594 Verbose = $setVerbose
560595 }
561- Write-EncodeProgress @params
596+ try {
597+ Write-EncodeProgress @params
598+ }
599+ catch {
600+ Wait-Completed - JobName ' 1st Pass' - Exception $_.Exception
601+ }
562602 }
563603
564604 Write-Host
@@ -584,7 +624,12 @@ function Invoke-FFMpeg {
584624 DolbyVision = $dovi
585625 Verbose = $setVerbose
586626 }
587- Write-EncodeProgress @params
627+ try {
628+ Write-EncodeProgress @params
629+ }
630+ catch {
631+ Wait-Completed - JobName ' 2nd Pass' - Exception $_.Exception
632+ }
588633 }
589634 }
590635 # CRF/One pass x265 encode
@@ -625,7 +670,12 @@ function Invoke-FFMpeg {
625670 DolbyVision = $dovi
626671 Verbose = $setVerbose
627672 }
628- Write-EncodeProgress @params
673+ try {
674+ Write-EncodeProgress @params
675+ }
676+ catch {
677+ Wait-Completed - JobName ' crf' - Exception $_.Exception
678+ }
629679 }
630680 }
631681 # Mux/convert audio and subtitle streams separately from elementary hevc stream
@@ -727,7 +777,12 @@ function Invoke-FFMpeg {
727777 Verbose = $setVerbose
728778 DolbyVision = $dovi
729779 }
730- Write-EncodeProgress @params
780+ try {
781+ Write-EncodeProgress @params
782+ }
783+ catch {
784+ Wait-Completed - JobName ' ffmpeg 1st Pass' - Exception $_.Exception
785+ }
731786 }
732787 }
733788
@@ -753,7 +808,12 @@ function Invoke-FFMpeg {
753808 Verbose = $setVerbose
754809 DolbyVision = $dovi
755810 }
756- Write-EncodeProgress @params
811+ try {
812+ Write-EncodeProgress @params
813+ }
814+ catch {
815+ Wait-Completed - JobName ' ffmpeg 2nd Pass' - Exception $_.Exception
816+ }
757817 }
758818 }
759819 # CRF encode
@@ -792,8 +852,9 @@ function Invoke-FFMpeg {
792852 }
793853 # Should be unreachable. Throw error and exit script if rate control cannot be detected
794854 else {
855+ $msg = ' Rate control method could not be determined from input parameters'
795856 $params = @ {
796- Exception = [System.FieldAccessException ]::new(' Rate control method could not be determined from input parameters ' )
857+ Exception = [System.FieldAccessException ]::new($msg )
797858 Category = ' InvalidResult'
798859 TargetObject = $RateControl
799860 ErrorId = 101
@@ -821,7 +882,12 @@ function Invoke-FFMpeg {
821882 DolbyVision = $dovi
822883 Verbose = $setVerbose
823884 }
824- Write-EncodeProgress @params
885+ try {
886+ Write-EncodeProgress @params
887+ }
888+ catch {
889+ Wait-Completed - JobName ' ffmpeg' - Exception $_.Exception
890+ }
825891 }
826892
827893 # Remove ffmpeg jobs
0 commit comments