ffmpeg を呼び出すシェルスクリプトで標準入力を用いる場合は ffmpeg に -nostdin オプションをつける

試した環境

本題

ffmpeg を呼び出すシェルスクリプト内で標準入力を用いる場合は ffmpeg-nostdinオプションをつけます。

例として、処理のリストを読み込んで順次ffmpegで実行するシェルスクリプトを考えます。 リストは1列目に入力ファイル名、2列目に出力ファイル名を書いた以下のようなテキストファイル(list.txt)を考えます。

countup_r.mp4 countup_r.hevc
countup_g.mp4 countup_g.hevc
countup_b.mp4 countup_b.hevc

シェルスクリプトは以下のようになります。 list.txtを標準入力で読み込みます。

#!/bin/bash
while read LINE
do
  INPUT=$(echo ${LINE} | awk '{print $1}')
  OUTPUT=$(echo ${LINE} | awk '{print $2}')
  ffmpeg -nostdin -i ${INPUT} ${OUTPUT}
done < "list.txt"

ffmpeg-nostdinオプションをつけることで3つのmp4ファイルの処理が行われます。 上記のffmpeg -nostdin -i ${INPUT} ${OUTPUT}ffmpeg -i ${INPUT} ${OUTPUT}とすると期待した動作にはなりません。 1つ目の処理中に以下のエラーメッセージが出て、2つ目以降の処理は行われません。

Enter command: <target>|all <time>|-1 <command>[ <argument>]

Parse error, at least 3 arguments were expected, only 1 given in string 'ountup_g.mp4 countup_g.hevc'

ffmpeg-nostdinオプションをつけないと処理中に以下の記述が出力されます。

Press [q] to stop, [?] for help

この記述の通りffmpegは標準入力を受け付けています。 そのためシェルスクリプトwhile readの標準入力が ffmpeg の標準入力に持ってかれるようです。
-nostdinオプションをつけると上記の記述はされません。 そのため ffmpeg は標準入力を受け付けなくなりシェルスクリプトwhile readの標準入力が期待した動作になります。

ffmpegのドキュメントにある -stdin オプションの説明は以下の通りです。

-stdin Enable interaction on standard input. On by default unless standard input is used as an input. To explicitly disable interaction you need to specify -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.

参考

https://ffmpeg.org/ffmpeg.html