為什麼 FFmpeg 在 while 迴圈中需要 nostdin?

為什麼 FFmpeg 在 while 迴圈中需要 nostdin?

如果我有:

for i in *.mov;
do
ffmpeg -y -i $i -c:v copy -c:a copy ${i%%.mov}.mp4
done

這運行良好。但如果我跑:

find . -name "*.ts" -print0 | while read -d $'\0' file;
do
  ffmpeg -i "$file" -c copy -map 0 "${file%%.ts}_rec.mp4";
done

這失敗了。我需要輸入-nostdin。

find . -name "*.ts" -print0 | while read -d $'\0' file;
do
  ffmpeg -nostdin -i "$file" -c copy -map 0 "${file%%.ts}_rec.mp4";
done

文件解釋說,這會禁用標準輸入上的交互,並且對後台進程很有幫助。

為什麼在第二種情況下 FFmpeg 是後台進程?或是有其他問題嗎?

答案1

FFmpeg 在這裡不是後台進程。它只是讀取它的stdin.可能不會,但確實如此。這會消耗應該轉到 的字元read。實際上,您可能會遇到這樣的問題:

「while read」循環遍歷文字檔案中的行,遺失 Bash 腳本中的字元。是FFmpeg線路的錯嗎?


該文件提到了後台進程,因為通常您不希望特別是他們讀書stdin。在您的情況下,您不希望前台 FFmpeg 讀取其stdin.

答案2

工作循環和非工作循環的差異是 |這會創建一個子外殼。

如果我沒記錯的話,我已經透過簡單地使用解決了這個問題echo "" | ffmpeg ...

來自 ffmpeg man:-stdin 啟用標準輸入上的交互作用。除非使用標準輸入作為輸入,否則預設為開啟。要明確停用交互,您需要指定“-nostdin”。

       Disabling interaction on standard input is useful, for example, if ffmpeg is in the background process
       group. Roughly the same result can be achieved with "ffmpeg ... < /dev/null" but it requires a shell.

echo "" | ffmpeg是相同的ffmpeg ... < /dev/null

相關內容