解决CMD环境下FFmpeg静默检测结果的多行输出写入文件问题
Solutions to Capture All Silence Durations to File Without Batch
CMD Command (Single Line, Overwrite Mode)
This approach starts with a clean file and writes all matching results in one overwrite operation:
del "C:\TEMP.txt" 2>nul & ffmpeg -i "C:\any-video.avi" -af silencedetect=noise=-50dB:d=0.1 -f null - 2>&1 | findstr "silence_duration:" | (for /f "tokens=8" %f in ('more') do @echo %f) > "C:\TEMP.txt"
Breakdown:
del "C:\TEMP.txt" 2>nul: Deletes the existing file (suppresses errors if it doesn’t exist)ffmpeg ... 2>&1: Runs ffmpeg and redirects its stderr output (wheresilencedetectlogs data) to stdoutfindstr "silence_duration:": Filters output to only lines containing silence duration values- Wrapping the
for /floop in parentheses and using>at the end redirects all echoed lines to the file in one go, preserving every result instead of overwriting per line
PowerShell Command (Simpler Native Overwrite)
PowerStream’s stream processing handles multi-line output natively, making it easier to capture all results and overwrite the file by default:
ffmpeg -i "C:\any-video.avi" -af silencedetect=noise=-50dB:d=0.1 -f null - 2>&1 | Select-String "silence_duration:" | ForEach-Object { $_.Line.Split()[7] } | Out-File "C:\TEMP.txt" -Encoding Unicode
Breakdown:
ffmpeg ... 2>&1: Redirects ffmpeg’s stderr to stdout, same as CMDSelect-String "silence_duration:": Filters for lines with silence duration dataForEach-Object { $_.Line.Split()[7] }: Splits each line into tokens and extracts the 8th value (zero-based index 7)Out-File ...: Writes all results to the file, overwriting existing content by default.-Encoding Unicodematches CMD’s/Uoutput format if needed
内容的提问来源于stack exchange,提问作者DannyhelMont
相关产品推荐
相关产品推荐

