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 1


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.

  • can you add a second part to your answer, that would cover cases where you want to overwrite the existing files in place? Please keep the first part of your answer as-is. That is the best practice I'd say. Sep 24, 2020 at 19:56
  • 1
    @JasonHunter ffmpeg can't perform in-place overwriting of the same input/output. You have to make a new, separate file.
    – llogan
    Sep 24, 2020 at 21:31
  • is there a command I could tack onto the end to "in essence" perform the same task? something like && mv /encoded/* /..* --overwrite TRUE Sep 24, 2020 at 23:13
  • 1
    @JasonHunter ... "encoded/${f%.*}.mp4"; && mv encoded/*.mp4 .; done
    – llogan
    Sep 24, 2020 at 23:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .