Thursday, January 6, 2022

[SOLVED] Where is the bug in this one-line Bash script?

Issue

I have two identical .mp4 files in a directory. For reasons I cannot understand, the file name of the second one gets mangled by the script below. When I have more .mp4 files, the first two or three characters of every other file seems to get deleted. This has been tested with different .mp4 files and on multiple systems. If I remove the -printf '%P\n' or do not use ffmpeg it works as expected.

$ ls -l
total 46944
-rw-rw---- 1 ubuntu ubuntu 24034673 Dec 23 09:59 file_one.mp4
-rw-rw---- 1 ubuntu ubuntu 24034673 Dec 23 09:59 file_two.mp4
$ find . -name '*.mp4' -printf '%P\n' |while read -r FILE; do ffmpeg -loglevel error -y -i "$FILE" -ss 00:00:01.000 -vframes 1 junk.png; done
le_two.mp4: No such file or directory
$ 

Why does this produce the error, le_two.mp4: No such file or directory?


Solution

As our friend (philippe) said you can use it like this.

find . -name '*.mp4' -printf '%P\n' | while read -r FILE; do ffmpeg -loglevel error -y -i "./${FILE}" -ss 00:00:01.000 -vframes 1 "junk_${FILE%.*}".png -nostdin; done

You print a list of files as standard input by find command, and all command that is in the loop can read it, thus ffmpeg can mess one byte from stdin data.

Also you can use < /dev/null for ffmpeg:

find . -name '*.mp4' -printf '%P\n' | while read -r FILE; do ffmpeg -loglevel error -y -i "./${FILE}" -ss 00:00:01.000 -vframes 1 "junk_${FILE%.*}".png < /dev/null; done



Answered By - Hossein Ebrahimzadeh