DEV Community

Benjamin Black
Benjamin Black

Posted on • Updated on

Using ffmpeg to compress, convert, and resize videos

My posts are usually notes and reference materials for myself, which I publish here with the hope that others might find them useful.

Given a source video source.mp4:

To compress for web at a reasonable broadband bitrate of about 1.5Mbps video / 128kbps audio:

ffmpeg -i source.mp4 -c:v libx264 -b:v 1.5M -c:a aac -b:a 128k target.mp4
Enter fullscreen mode Exit fullscreen mode

To compress and also convert to WebM:

ffmpeg -i source.mp4 -c:v libvpx-vp9 -b:v 1M -c:a libopus -b:a 128k target.webm
Enter fullscreen mode Exit fullscreen mode

(Note that the 33% lower bitrate -- 1M for VP9 vs 1.5M for H.264 -- is deliberate; VP9 encodes about 20-50% more efficiently than H.264. Opus and AAC are about equally efficient.)

To scale down a high-resolution source video to something more reasonable for Web (qHD for cellular, HD for broadband), the -filter:v argument is used:

ffmpeg -i source.mp4 -c:v libvpx-vp9 -b:v 0.33M -c:a libopus -b:a 96k \
-filter:v scale=960x540 target.webm
Enter fullscreen mode Exit fullscreen mode

Command-line options: -c:v specifies the video codec; -b:v specificies video bitrate; -c:a specifies audio codec; -b:a audio bitrate; -filter:v applies a filter (in this case, scale) to the video stream.

To use the older VP8 codec with WebM, use libvpx instead of libvpx-vp9.

If the source video does not have an audio track, then omit the -c:a and -b:a arguments.

To explicitly specify the container format, use the -f command-line option: -f mp4 or -f webm.

For MP4 containers, the video codec used is H.264 and the audio codec is AAC, which enjoys near-universal browser support.

For Chrome and related browsers. WebM containers use VP8/9 and Opus.

Chrome also supports MP4 (H.264/AAC), so there is no point providing WebM unless the file size and/or quality is improved.

Bitrates can obviously be varied; I use 0.5M video and 96k audio for cellular.

Top comments (3)

Collapse
 
aquaductape profile image
Caleb Taylor • Edited

I'm a total noob at this but it seems like compressing a silent video to mp4 via libx264 seems better if you want smaller file size. The quality seems just as good compared to Webm. Sometimes compressing small mp4 files with WebM increases the file size, while using libx264 on the same file compresses it correctly.

link to original-file.mp4

original-file.mp4 = 1.6MB
output-file.webm = 966 kB
output-file.mp4 = 538 kB

The ffmpeg args I used are from this post

 ffmpeg -i input.mp4 -vcodec libx264 -crf 28 output.mp4
Enter fullscreen mode Exit fullscreen mode

If I'm right, what's the fuss about WebM? Currently libx264 seems to do a better job. Is WebM on track to become much better at compression in the future?

Collapse
 
thetwopct profile image
James Hunt

Hey thanks for this, I'm trying to convert a video for web, but I think FFmpeg has changed the way it installs, so now libvpx-vp9 library is never installed, so I cannot seem to make webM videos. Any advice? Thanks!

Collapse
 
benjaminblack profile image
Benjamin Black

Depending on your platform, additional codecs may be provided in separate packages.