How can I use ffmpeg to reduce the size of a video by lowering the quality (as minimally as possible, naturally, but I need it to run on a mobile device that doesn't have much available space)?

I forgot to write one thing yet. When the video can use subtitles (*.srt or *.sub) I'd like to convert them too to fit the parameters of converted video file.

  • 4
    I haven't used it but the ffmpeg man page shows a -fs option to limit the output size, does something like ffmpeg -i in.avi -fs 100M out.avi work? – Kevin Jan 10 '12 at 23:41
  • I will not redirect you to the man page: man ffmpeg | wc -l --> 5254 – user13742 Jan 11 '12 at 0:11
  • 3
    The .avi is not the main issue.. avi is just a container. The main issue is which codecs you use.. Many (most?) .avi vids use older style codecs (eg XviD) which are fine, but are larger for the same quality when compared to the later generation of codecs .. You can typically get a tight encoding by using the H.264 video compression standard (eg. codecx264) and aac compression for audio.. The container and codecs you use is up to you and your phone... The .mp4 container is well accepted.. (but can your phone handle it: see this link – Peter.O Jan 11 '12 at 1:16
  • @Kevin This wants more parameters for conversion. – xralf Jan 11 '12 at 8:34
  • @hesse What does it mean? – xralf Jan 11 '12 at 8:36
up vote 156 down vote accepted

See this answer. Quoted below for convenience:

Calculate the bitrate you need by dividing 1 GB by the video length in seconds. So, for a video of length 16:40 (1000 seconds), use a bitrate of 1000000 bytes/sec:

ffmpeg -i input.mp4 -b 1000000 output.mp4

Additional options that might be worth considering is setting the Constant Rate Factor, which lowers the average bit rate, but retains better quality. Vary the CRF between around 18 and 24 — the lower, the higher the bitrate.

ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4
  • The video of size 338 MB was reduced on size 130 MB. The quality rapidly lowered. Is there some explanation for this process? The original author doesn't explain his guidline. – xralf May 11 '12 at 7:41
  • 5
    This is a four year some time. 😂 – wener Mar 28 '16 at 14:59
  • 6
    Second command, using -crf 24 took a 255.3MB video I had and reduced it to 72.7MB without lowering the quality noticeably. Have an upvote! – Patrick Roberts Mar 25 '17 at 18:14
  • 3
    Might be good to note that you can now use libx265 for even more size reduction. – ZN13 Apr 13 at 19:55
  • 4
    Used ffmpeg -i input.avi -vcodec libx264 -crf 24 output.avi. It reduced a 100mb video to 9mb.. Very little change in video quality. Thank you! – alpha_989 May 23 at 22:02

Unless you're looking for a specific bitrate, I'd recommend the -crf option. This is the most commonly used for x264 encoding: http://slhck.info/articles/crf

In short: a CRF of 23 would make "DVD" quality movie (~700MB-1GB) and lower CRF values would be higher quality (larger files).

  • Please give examples of the full command instead of linking to an external website (which could break someday:) – Jake Berger Aug 25 at 17:17
  • 1
    @Vicky Chijwani provides the code in the example above. This is better suited to a comment but it was my first activity on this site. The link has more explanation on the crf option but isn’t necessary to get the code to work. – Tom Kelly Aug 27 at 12:05

You mentioned wanting to reduce filesize to fit more videos on a mobile device, which is my usecase as well. All the answers here are for reducing the compression quality but nobody has mentioned reducing video frame size. It's a lot quicker, from about 3 to 5 times quicker than recompressing in my experience. See the ffmpeg docs on scaling for more info.

ffmpeg -i input.mkv -vf "scale=iw/2:ih/2" half_the_frame_size.mkv
ffmpeg -i input.mkv -vf "scale=iw/3:ih/3" a_third_the_frame_size.mkv
ffmpeg -i input.mkv -vf "scale=iw/4:ih/4" a_fourth_the_frame_size.mkv

You'll need to use 2-pass encoding to "fit" a video within a designated file size (bitrate), without reducing the quality too drastically. This is quite a detailed topic: http://www.mpabo.com/2014/12/14/ffmpeg-and-x264-encoding-guide/

I have a recipe I originally forged for myself in order to convert the Motion JPEG videos my old camera generates (they are very large videos, since each frame is an entire JPEG image) to h264. Here's an adaptation for other kinds of videos (courses, etc).

I'm not using ffmpeg, but mplayer and mencoder. First, We have to demux the audio with mplayer:

mplayer -vo null -ao pcm:fast:file=<audio_pcm.wav> <video>
  • The -vo null and -ao null parameters tells mplayer to not extract video.

In the next steps, we'll do a 3-pass compression with mencoder. At the first pass we'll choose a suitable Constant Quality Mode compression (crf parameter) as a start point:

mencoder <video> -ovc x264 \ 
         -x264encopts ratetol=100:preset=veryslow:crf=<value>:pass=1 \
         -nosound -o video1.h264
  • You can add slow_firstpass parameter to the -x264encopts if you are paranoid with the final quality of the video. Mencoder manual says that this option disable some parameters that “significantly improve encoding speed while having little or no impact on the quality of the final pass”. So, use it only at the last step.

  • You should try several values for crf — try starting from 25 and goes on increasing it until you note artifacts at the resulting video (higher values compresses more). Remember subsequent encoding passes will improve the quality you have choosed for crf.

  • Alternatives for the veryslow preset are slower, slow, medium etc. See mencoder manual for the complete list.

  • ratetol controls the bitrate variation — I'm not sure if I'm doing the right thing here, but I set it to the maximum value in order to let total freedom to mencoder to choose the right bitrate for each scene.

After the first pass, you'll note that the last line gives you the average bitrate you will use at the next steps:

(...)
x264 [info]: kb/s:526.43

Change the crf parameter, recommended at the first pass, to bitrate, required at the subsequent passes:

mencoder <video> -ovc x264 \
       -x264encopts slow_firstpass:ratetol=100:preset=veryslow:bitrate=526:pass=3 \
       -nosound -o video2.h264

This second pass encoding will read the statistics generated at the first pass (divx2pass.log and divx2pass.log.mbtree) in order to optimize the compression.

  • Note you'll use the same video input, not the generated by the first pass — first pass' output video is only useful to check the initial quality.

  • Note also that the pass=3 (not pass=2) will generate a new statistics file, so you can repeat the last step as many times you want. I usually do pass=3 twice, always paying attention to the result bitrate.

Meanwhile, you can compress the audio too, using lame or oggenc:

oggenc -q<n> <audio_pcm.wav>

Finally, we'll remux audio and video

mencoder -audiofile <audio>.ogg video2.h264 -oac copy -ovc copy \
         -of lavf -lavfopts format=mp4 -o <video>.mp4
  • The -of lavf -lavfopts format=mp4 generates mp4 file format using the lavopts muxers.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.