I have this command

for ($i=0; $i -gt -1; $i++) {
    $path = "..."
    ffmpeg -f dshow -i audio="Microphone (USB Microphone)" -y -t 00:10:00 -b:a 128k $path

I need to get the current state of the last line of the command output stream, then if the line remains unchanged (staled/freezing/hanging) over 5 seconds log "warning: the program is freezing. trying to restart...", then stop the process and re-start the command.

But I wonder, is it even possible? Thanks for your help.

1 Answer 1


Utilizing PowerShell functions and advanced techniques, in addition to appending 2> $Log to the ffmpeg command efficiently redirects its standard error output to a specified log file.

This approach defines the $path, $log, and $micName variables initially. Following that, four straightforward functions are defined and subsequently called by additional logic, using the preset variables from the beginning.

The run-ffmpeg function incorporates three explicitly defined parameters. Whereas, the tail, check, and logchecks functions utilize $args[0] as a placeholder to capture the first unnamed parameter passed in with their function call.


$path = "D:\ffmpeg\out\test.wav";
$log = "D:\ffmpeg\out\err.log";
$micName = "Microphone (USB Microphone)";
Function run-ffmpeg {param([string]$Path,[string]$Log,[string]$MicName)
    $ffmpegCommand = "ffmpeg -f dshow -i audio='$MicName' -y -t 00:10:00 -b:a 128k $Path 2> $Log"
    Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command $($ffmpegCommand)" -WindowStyle Hidden;   
    Start-Sleep -Seconds 4;
    logchecks $Log;

Function tail {Get-Content -Path $args[0] -Tail 1};
Function check {tail $args[0]};

Function logchecks {
    do {
        $checklog = check $args[0];
        Start-Sleep -Seconds 5;
        $rechecklog = check $args[0];
    } while ($checklog -ne $rechecklog)
    run-ffmpeg -Path $path -Log $log -MicName $micName;  

logchecks $log;

Logic Support Notes

  • Implemented 2> $Log as mentioned earlier to redirect ffmpeg command output to a log file.
  • Utilized start-process to execute the ffmpeg command within logic to ensure proper functionality.
  • Used get-content with the -tail 1 parameter to monitor the latest content in the log.
  • Implemented a do while loop to periodically monitor the tail of the ffmpeg log file at 5-second intervals, ensuring that the content differs from the previous 5-second check.
  • Incorporated start-sleep at strategic points to introduce delays, allowing time for subsequent checks post-execution of processes, etc.

Supporting Resources

  • @丶 Limeー来夢 丶... This is a verified method which I tested and confirmed personally to work for meeting the expectations for a functional solution as you describe you seek. While slight adjustments may be necessary based on your specific requirements, the intention is to provide a thoroughly validated solution that should address your needs comprehensively. Please let me know if you encounter any issues, have questions, or require assistance with additional minor adjustments. Commented Dec 23, 2023 at 5:59
  • Additionally, I was playing with this but it may be overkill for your need [timespan]$rechecklog.Split("=")[2].Split(".")[0] -eq [timespan]$checklog.Split("=")[2].Split(".")[0].... Just in case you were looking for a way to parse out the time part of the string. I think checking the match on the last line and the 5 second pause is more what you are after. Commented Dec 23, 2023 at 6:21
  • 1
    Thanks, it works very well so far. Commented Dec 23, 2023 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .