5

How do I reduce the filesize of videos? For some reason, by phone takes giant videos. The worst of these is a 20 minute video that takes up an entire 13 GB. How can I shrink the filesizes of these? They are mp4s encoded in H.264 and MPEG-4 AAC and play at 30 fps.

It's okay if there is reduction in quality.

I tried using avconv to reduce resolution, but even though this outputted a video with smaller dimensions, the size had bizarrely increased.

What is a good way for reducing the sizes of these file?

  • I have seen that. That's the answer where running the commands results in an increase in size – quantumbutterfly Jun 9 '17 at 22:04
  • Maybe avconv is not the right tool. There is more then one command in that answer. Also, look into compressions level, as well as resolution. – mikewhatever Jun 9 '17 at 22:11
  • Since making this post, I tried an ffmpeg command which also increased the size, and then another command that cut the video down to 10 mb, which was too far and left the video unwatchable. Trying another command now that should be done in about 10 minutes – quantumbutterfly Jun 9 '17 at 22:13
  • @mikewhatever The duplicate you have pointed out has more to do with reducing the screen dimensions of a video file than actually reducing file size... – andrew.46 Jun 9 '17 at 23:49
  • @andrew.46: which will decrease file size at the same quality settings. – David Foerster Jun 10 '17 at 15:30
10

To reduce the size of your mp4 you have two good choices with FFmpeg:

  1. Experiment with Constant Rate Factor (CRF) settings to find a match between video output size and video quality
  2. Experiment with the set bitrate of your choice and use a 2 pass encode.

Details of both below:

1. Resize using FFmpeg's Constant Rate Factor (CRF) option:

In FFmpeg the CRF settings vary from 0-51 with 0 being lossless and 51 being shockingly poor quality. The default is 23 and you need to find the 'sweet spot' for your particular video of file size and video quality with some experimentation.

A sample command-line would be:

ffmpeg -i input.mp4 \
       -c:v libx264 -preset slow -crf 22 \
       -c:a copy \
       output.mp4

Note that in this command-line the video is re-encoded and the audio is simply copied across. Things to consider:

  1. If the size is still too big and the video quality is still acceptable you would try -crf 24 and so on (incrementally increasing the crf integer) until you find an acceptable compromise between video quality and file size.
  2. If the video quality is too poor you would try crf 20 and so on (incrementally decreasing the crf integer) until you find an acceptable compromise between video quality and file size.

2. Resize using FFmpeg's 'set bitrate' + '2 pass encode' options:

Another choice, which arguably will not produce as good results as the CRF example is to use the set bitrate of your choice and use a 2 pass encode. The following example of this is a single command:

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

Again in this example the video is re-encoded and the audio is simply copied over. Things to consider:

  1. Video still too big? Decrease the bitrate...
  2. Quality too poor? Increase the bitrate...

Keep doing this until you find an acceptable compromise between video quality and file size.

References:

  • wow, amazing comment here sir, thanks! was really helpful to me – Dazag May 24 at 8:57
  • Glad this helped out! – andrew.46 May 24 at 10:21
  • Why is the CRF value range 0-51 and not 0-100 ? – Freek Nortier Jun 24 at 15:52
  • @FreekNortier Hmmm... I have not dug into the source and looked for the raw numbers. Do you think that there is an allowance for the extra numbers? – andrew.46 Jun 24 at 20:55
2

So I figured it out. The "-b" flag in ffmpeg sets the bit rate. I also found out that 1,000,000 is a good general use value that provides a decent tradeoff between file size and quality.

So run:

ffmpeg -i inputfilename.mp4 -b 1000000 outputfilename.mp4

You can substitute 1000000 for another bitrate value if you want. Also, you can include a "-s WxH" flag to set the size of the output video. So to make the output video 720p, you'd include "-s 1080x720"

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.