I am running the below command, to reduce the size of a mp4 video

ffmpeg -i input.mp4 -acodec mp2 output.mp4

After the output is created, I run the command again on the output and so on, because the reduction size is only around 0.1 Mb with each command execution, so I need to reduce around 2-3 Mb, that"s why I need to run the command iteratively.

I want to write a bash script to do this automatically, instead of I keep running the command again and again. And if there is an option to stop running the command, if the files size reached the size I want ?

  • 2
    Instead of re-encoding the video 30 times, which sounds like a terrible idea to me, you should rather try and change the quality settings in ffmpeg and just convert it once, I believe. See man ffmpeg for supported arguments. – Byte Commander Jul 14 '18 at 10:27
  • @ByteCommander hey, yes I know it is a terrible idea, but unfortunately, I searched a lot online, and I didn't find a good alternative solution – Mostafa Hussein Jul 14 '18 at 10:29
  • I believe the suggestions given are valid iterations would be a bad idea but in any case, this should help although not tested: for i in ./*; do for y in $(seq 1 <END>); do ffmpeg -i "$i" -acodec mp2 output.mp4; done; done – George Udosen Jul 14 '18 at 11:43
  • Some hints here for targeting a specific size: askubuntu.com/q/1027340/57576 – andrew.46 Jul 15 '18 at 23:15

Really dont do this -- you damage the quality each time.

Try an ffmpeg command like this:

ffmpeg -i input.mp4 -c:v libx265 -crf 30 -c:a copy output.mp4

So (in this example) I use the h265 codec for video and set a factor of 30. A smaller number will give a larger file with better quality and a higher number a smaller file and poorer quality. The audio is just copied. I don't know whats in your source file, but usually the biggest saving come from re compressing the video

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.