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

New contributor
Ace is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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

  • 1
    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
    2 days ago

Your Answer

Ace is a new contributor. Be nice, and check out our Code of Conduct.

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.