Search code examples
bashloopsfilerecursionffmpeg

Recursively encode video files in many directories


I'd like to encode video files in many directories in one go. In each directory when there is at least one *.mp4 file ffmpeg application should be run with correct arguments. Say a video file is named 01_file.mp4. I want the output file to be in the same directory as the source is located with name suffix processed at the end of filename but before extension. So the source 01_file.mp4would be saved as01_file_processed.mp4`. I'm quite close to the correct solution. Here's my one-liner:

find -mindepth 2 -type f ! -path '*processed.mp4' -name '*.mp4' -exec bash -c 'ffmpeg -loglevel warning -i "$1" -vf fps=23.976025 "${1%/*}/${1##*/}_processed"' _ {} \;

I'm getting the error from ffmpeg since the output file is not correct. Instead of correct path ending with 01_file_processed.mp4 the file is 01_file.mp4_processed. I believe the error is in the following part ${1%/*}/output/${1##*/}". How do I fix this?


Solution

  • Why are you trying to separate dirname from basename, if the only thing you do with this splitting is to reconcatenate it?

    I feel that you have tried to imitate another code that has another objective.

    In your case, if I understand correctly, you don't need to extract basename nor dirname. But you want to remove the extension to add a _processed before it.

    Which is quite easy to do, with your technique, since you know the extension is .mp4. So just remove the trailing .mp4, exactly as you've removed the trailing /* to extract dirname. And then replace that .mp4 by what you want, namely, _processed.mp4

    find -mindepth 2 -type f ! -path '*processed.mp4' -name '*.mp4' -exec bash -c 'ffmpeg -loglevel warning -i "$1" -vf fps=23.976025 "${1%.mp4}_processed.mp4"' _ {} \;