10

I want to encode a video, that has a length of 60 seconds, to a target or maximum size of 10 MB.

There are two approaches I know of. One is explained in the FFMPEG-Wiki, and the other one I found in the documentation. Unfortunately, I haven't found an explanation on when to use what method.

Is any of those methods recommended? If not, what are the upsides/downsides of each method?

1) Calculate and set Bitrate to match the length of the video as explained in the ffmpeg-wiki

(10 MB * 8192 [converts MB to kilobits]) / 60 seconds = ~1365 kbits/s total bitrate 1365k - 128k (desired audio bitrate) = 1237k video bitrate

ffmpeg -y -i input -c:v copy -preset medium -b:v 1237k -pass 1 -c:a copy -b:a 128k -f mp4 /dev/null && \
ffmpeg -i input -c:v libx264 -preset medium -b:v 1237k -pass 2 -c:a libfdk_aac -b:a 128k output.mp4

2) Use the -fs parameter and let ffmpeg figure it out.

ffmpeg -i input -c:v copy -c:a copy -preset medium -crf 23 -fs 10485760 output.mp4
  • 1
    In your 1st example, the first step should also use libx264 as c:v. In the fs example, you aren't transcoding the video, since -c:v copy is used. So, the preset and crf also don't have any effect. – Gyan Dec 27 '15 at 12:35
  • This was helpful for me: stackoverflow.com/a/34547102/470749 – Ryan Mar 20 at 23:59
11

The fs parameter will stop the encode once it hits its value. So, if the output hits the 10MB mark while encoding the 15th second, then that's the duration of your output file.

If you want to make sure that the entire file is encoded but it doesn't cross the set target size, then use the bitrate method. To accommodate the muxing overhead and other data within the target size, set your video bitrate slightly lower by, say, 1-2%

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.