On its first test, my clip-preview loop made three previews from six clips and exited 0. The job is simple: make a low-resolution copy of every clip in a folder, so I can skim a day of footage without opening each file. It lists the clips with find, pipes the list into a while read loop, and runs ffmpeg once per name, with set -e at the top.
Nothing was wrong with the clips or the list. ffmpeg was reading the list too. It treats its standard input as a keyboard, and inside that loop its standard input is the list of file names. What it does with those names depends on which letters they start with.
Every block below was run with ffmpeg 4.4.2 and GNU Bash 5.1.16 on Linux, and every output block is pasted unedited from the run, stderr included.
The test folders
#!/bin/bash # Six synthetic test clips in each of two folders, $1 seconds long. # Nothing here comes from a phone. set -e for dir in clips queue; do rm -rf "$dir" && mkdir "$dir" for i in 1 2 3 4 5 6; do ffmpeg -nostdin -loglevel error -y \ -f lavfi -i "testsrc2=size=320x240:rate=15:duration=$1" \ -c:v libx264 -pix_fmt yuv420p "$dir/clip_0$i.mp4" done done Two folders, six clips each, identical except for the folder name. That name turns out to matter.
The loop
#!/bin/bash # Make a small preview of every clip in the folder given as $1. set -e rm -rf previews && mkdir previews find "$1" -name '*.mp4' | sort | while IFS= read -r f; do ffmpeg -loglevel error -y -i "$f" -vf scale=160:-2 "previews/$(basename "$f")" echo "made: $f" done echo "previews: $(ls previews | wc -l)" #!/bin/bash # Length and size of each preview, as ffprobe and stat see them. for p in previews/*.mp4; do printf '%s %s s %s bytes\n' "$p" \ "$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$p")" \ "$(stat -c %s "$p")" done This is the loop shape most examples use: find, a pipe, while IFS= read -r. It quotes the name, survives spaces, and stops on any failed command. I ran it on clips/ with three-second clips:
Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_02.mp4' made: clips/clip_01.mp4 Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_04.mp4' made: clips/clip_03.mp4 Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_06.mp4' made: clips/clip_05.mp4 previews: 3 script exit: 0 previews/clip_01.mp4 3.000000 s 28851 bytes previews/clip_03.mp4 3.000000 s 28851 bytes previews/clip_05.mp4 3.000000 s 28851 bytes Three previews, every other clip, and the script exited 0. The three previews it did make are complete, three seconds each.
The stderr lines say what happened. ffmpeg printed Enter command:, then complained about a string that reads lips/clip_02.mp4. That is the next line of the list with its first letter missing. The c went somewhere, and the rest of the line was taken as the argument to whatever c started. Then read got the line after that, clips/clip_03.mp4, and the loop carried on as if nothing had been skipped.
Asking ffmpeg what those letters mean
ffmpeg will print its own key list if you send it a question mark:
#!/bin/bash # Ask ffmpeg for its own key list by sending it a question mark. echo '?' | ffmpeg -loglevel error -f lavfi -i "testsrc2=duration=5" -f null - 2>&1 | sed -n '/^key/,/^s /p' key function ? show this help + increase verbosity - decrease verbosity c Send command to first matching filter supporting it C Send/Queue command to all matching filters D cycle through available debug modes h dump packets/hex press to cycle through the 3 states q quit s Show QP histogram c sends a command to a filter. It reads the rest of the line as that command, which is why one whole file name vanished per clip. The ffmpeg documentation describes the option behind this as "Enable interaction on standard input. On by default unless standard input is used as an input." The input file here is named with -i, not read from stdin, so interaction stays on, and the loop's list is what it reads.
The same loop on a folder named queue/
q is also on that list. I ran the identical script on the second folder:
made: queue/clip_01.mp4 ueue/clip_02.mp4: No such file or directory script exit: 1 previews/clip_01.mp4 0.000000 s 262 bytes This one is worse in a quieter way. The first preview exists and is 262 bytes, and ffprobe gives its duration as zero. ffmpeg read the q from the next name, quit before writing any video, and returned success: the made: line printed after it, and with set -e on, that line only prints when the command before it exits 0.
The script did stop, but not because of the empty file. The next name read handed over was ueue/clip_02.mp4, which does not exist, so the second ffmpeg failed and set -e ended the run. That was luck. A folder whose clips were named so that the shortened path still existed would have kept going.
Longer clips lose more
Back to clips/, with every clip regenerated at twenty seconds:
Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_02.mp4' Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_03.mp4' Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_04.mp4' made: clips/clip_01.mp4 Enter command: <target>|all <time>|-1 <command>[ <argument>] Parse error, at least 3 arguments were expected, only 1 given in string 'lips/clip_06.mp4' made: clips/clip_05.mp4 previews: 2 script exit: 0 previews/clip_01.mp4 20.000000 s 192780 bytes previews/clip_05.mp4 20.000000 s 192780 bytes Two previews out of six. The first ffmpeg ran long enough to read three lines, not one: three Enter command: prompts before the first made: line. A longer encode gives it more chances to read standard input, so the number of skipped clips moves with clip length, and probably with machine speed. I ran each case three times and got the same counts on this machine. I would not expect the same counts elsewhere, and that is part of the problem. The bug can look different every night.
Three fixes, and what each one changes
#!/bin/bash # The same loop three ways, on the 20-second clips in clips/. set -e count() { echo "$1 previews: $(ls previews | wc -l)"; rm -rf previews && mkdir previews; } rm -rf previews && mkdir previews find clips -name '*.mp4' | sort | while IFS= read -r f; do ffmpeg -nostdin -loglevel error -y -i "$f" -vf scale=160:-2 "previews/$(basename "$f")" done count "-nostdin:" find clips -name '*.mp4' | sort | while IFS= read -r f; do ffmpeg -loglevel error -y -i "$f" -vf scale=160:-2 "previews/$(basename "$f")" < /dev/null done count "</dev/null:" while IFS= read -r -u 3 f; do ffmpeg -nostdin -loglevel error -y -i "$f" -vf scale=160:-2 "previews/$(basename "$f")" done 3< <(find clips -name '*.mp4' | sort) count "read -u 3:" -nostdin: previews: 6 </dev/null: previews: 6 read -u 3: previews: 6 script exit: 0 All three made six previews from the twenty-second clips.
-
-nostdinturns interaction off from theffmpegside. The documentation says "To explicitly disable interaction you need to specify-nostdin." This is the one I now add to everyffmpegcall in a script, loop or not. -
< /dev/nullgivesffmpegan empty standard input instead of the loop's. It does the same job for any command, which matters if you do not know what else in the loop body reads stdin. The ffmpeg docs call this "Roughly the same result" and note it "requires a shell", which a script has by definition. -
read -u 3moves the list off standard input altogether, onto file descriptor 3. Nothing in the loop body is likely to read fd 3 by accident. I kept-nostdinin that version too. Without it,ffmpegwould still read the script's own stdin, which is your keyboard when you run the script by hand.
The third shape has another effect. A while loop at the end of a pipe runs in a subshell, so variables set inside it are gone when it ends. I tested that separately in an earlier post on a clip counter that read 0 after counting six. With done 3< <(...), the loop runs in the current shell, so a count kept inside it survives.
Why set -e did not help on the first run
set -e reacts to exit statuses. In the clips/ runs every ffmpeg that ran exited 0, read succeeded each time it was called, and the loop ended normally when the list ran out. No command failed. Clips were skipped because the loop did not see their names, and a name nobody reads cannot produce an error.
The same goes for the queue/ case up to the moment a mangled name happened not to exist. An empty preview with a success status passes every check that only looks at status.
What I changed in the preview script
- Every
ffmpegin a script gets-nostdin. It costs nothing and removes the keyboard from a program that has no keyboard. - Loops over a list of names read from fd 3, using
while IFS= read -r -u 3 f; do ...; done 3< <(find ...). That protects against the next tool that reads stdin, not just this one. - The script compares counts at the end. It counts the clips it was given and the previews that exist, and exits non-zero if they differ.
- It checks what it made, not only that the command returned. A preview with a duration of zero is treated as a failure, however
ffmpegexited.
What these runs do not cover
- One ffmpeg, one Bash. ffmpeg 4.4.2 and Bash 5.1.16. The key list printed above comes from that build. Other versions may have other keys, which would change which file names get eaten, but not the fix.
- One machine. The counts of skipped clips depend on timing. The pattern held across repeated runs here; the exact numbers may not hold anywhere else.
- Other programs. The FAQ entry that covers this, BashFAQ/089 on loops that run ssh or ffmpeg, names
sshas the other common case. I did not testsshor anything else here. - A slightly different story from the FAQ. Its title says "only the first line gets processed!", and it describes
ffmpegas "sucking up all the input". In these runsffmpegtook one character forqand one line forc, so the loop kept going with gaps instead of stopping after one file. Both are the same bug, and which you get depends on what the names spell. - Toy clips. Every clip is a synthetic test pattern made by
ffmpegitself.
Sources. The -stdin quotes are from the ffmpeg documentation, and the FAQ quotes are from Greg's Wiki BashFAQ/089.
The clips in a folder like this come from somewhere. Mine come from an old phone running Background Camera RemoteStream on Google Play, an Android app I make that records with the screen off. The scripts above work the same on any folder of video files.
So if a while read loop over file names ends with exit 0 and fewer results than names, look for a command in the body that reads standard input before you look at the files. For ffmpeg that is -nostdin; for whatever reads stdin next, it is the loop reading its list from fd 3.