試した環境
- WSL バージョン: 2.1.5.0
- Ubuntu 20.04.6 LTS (GNU/Linux 5.15.146.1-microsoft-standard-WSL2 x86_64)
- GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
- ffmpeg version 4.2.7-0ubuntu0.1
本題
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.