Linux – use find command for recursive conversion using ffmpeg

ffmpegfindlinuxUbuntu

How can I use the following script to recursively convert a directory containing .mp4 to .png (1 frame only).

Right now it only converts the directory. It's not recursive.

for i in *.mp4
do name=`echo "$i" | cut -d'.' -f1`
echo "$name"
ffmpeg -i "$i" -r 0.0033 -vf scale=-1:1024 -vcodec png "${name}.png"
done

Best Answer

find . -type f -name '*.mp4' -exec sh -c '
    name="${1%.*}"
    echo "$name"
    ffmpeg -i "$1" -r 0.0033 -vf scale=-1:1024 -vcodec png "${name}.png"
' find-sh {} \;

Notes:

  • In your original code name=`echo "$i" | cut -d'.' -f1` is rather low quality fragment. It cuts at the first dot, this seems wrong. It's not the only problem but I won't elaborate. If you want to remove an extension, the right way is name="${i%.*}".
  • *.mp4 is quoted because of this.
  • Mind this.
  • find-sh is explained here.
  • find descends into subdirectories but it doesn't follow symbolic links by default. If all "subdirectories" in . are if fact links to directories then you may erroneously think the tool doesn't descend in general. Check man 1 find, -L option.