0

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?

bitinerant
  • 1,168
  • 7
  • 24
  • 2
    Try to add `-nostdin` to your ffmpeg command. – Philippe Dec 23 '21 at 10:37
  • 1
    `ffmpeg` reads from stdin, and is stealing part of the filenames you intended for `read`. See [BashFAQ #89: I'm reading a file line by line and running ssh or ffmpeg, only the first line gets processed!](https://mywiki.wooledge.org/BashFAQ/089) – Gordon Davisson Dec 23 '21 at 10:50

1 Answers1

2

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

  • This is the proper fix, but to be a good answer, it needs to also say why. – bitinerant Dec 23 '21 at 11:51
  • You use junk.png for pictures name, after the exec command you have 1 picture. I changed the output file name that you have all pictures, and use of -nostdin global option. – Hossein Ebrahimzadeh Dec 23 '21 at 15:19
  • You have still not answered my question--*why*. Without this it is not a complete answer. And do this by editing your answer, not in the comments. (Yes, I realize that the output gets overwritten with each loop. My code was to demonstrate the problem in the simplest way possible.) – bitinerant Dec 23 '21 at 15:25
  • excuse me for the delayed answer friend. I was busy. – Hossein Ebrahimzadeh Dec 26 '21 at 12:20