Batch Convert Mp4 with Ffmpeg

Batch Convert Mp4 with Ffmpeg

I have a whole bunch of text files, jpg and mp4 in a folder. I want the jpg and text files to be left alone. I want to compress all the mp4 using ffmpeg's -crf argument with a value of 23, to save disk space. Files should be overwritten in place.

I used @llogan answer here, and replaced .avi with .mp4 but that does not work.

for f in *.mp4; do ffmpeg -i "$f" -c:v libx264 -crf 23 -preset medium \
  -c:a aac -b:a 128k -movflags +faststart -vf scale=-2:720,format=yuv420p \
  "encoded/${f%.avi}.mp4"; done

What would the for loop be to convert mp4 to lower quality (-crf 23), in place, without deleting and/or modifying any other files in the same working directory? I don't really want the other flags either, because I won't be doing resizing, youtube uploading, changing encoding speed, etc.

1 Answer

You just needed to change "encoded/${f%.avi}.mp4" to "encoded/${f%.mp4}.mp4" or "encoded/${f%.*}.mp4":

mkdir encoded
for f in *.mp4; do ffmpeg -i "$f" -c:v libx264 -crf 23 -preset medium -c:a copy -movflags +faststart "encoded/${f%.*}.mp4"; done

I removed all of the other stuff you didn't need. I assumed your input audio is already AAC so I used -c:a copy to enable stream copy mode which is like a copy and paste.

-crf 23 -preset medium are the default so you can remove those if you are fine with those values. Otherwise, see FFmpeg Wiki: H.264 for guidance on what -crf and -preset values to use.

4

Your Answer

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

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.