Can someone help me creating a for loop for mutiple video files in directory structure

ffmpeg -i input.mp4 -c:v libx265 -c:a copy -x265-params crf=25 ouput.mp4

This works good for one video, but I'm lost how to do this in batch for multiple files


1 Answer 1


This will work for all files that end in ".mp4" in the current directory. The command will output to new-filename.mp4 for each file. For example, if your file is video1.mp4, it will output a new file named new-video1.mp4

for i in *.mp4; do ffmpeg -i "$i" -c:v libx265 -c:a copy -x265-params crf=25 new-"$i"; done


  • the first part for i in *.mp4 needs to be unquoted.

  • subsequent references to the variable $i should be quoted.

  • don't forget done at the end

  • if you need to work recursively, the loop would need to be more complex

  • 3
    Using Bash, recursive work wouldn't be that complex. Find input files with **/*.mp4 (need shopt -s globstar), and construct output file names with ${i%/*}/new-${i##*/}, or use $(dirname "$i")/new-$(basename "$i") for readability.
    – iBug
    Jun 23, 2022 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.