Back to Blog

FFmpeg Basics: How to Compress Video for Efficient Web Streaming

Dec 27, 2025•Streaming Guide

If you have ever tried to host your own video content, you know the panic of seeing your bandwidth bill—or the frustration of watching a loading spinner that just won't quit. Raw video, or even "lightly" compressed video from your camera or editor, is rarely fit for the web. It is too heavy, too slow to decode, and often in a container that browsers struggle to digest.

Enter FFmpeg. It is the open-source, command-line Swiss Army knife of multimedia handling. It powers everything from VLC media player to YouTube's backend. While the command-line interface (CLI) can be intimidating compared to a drag-and-drop GUI like Handbrake, it offers a level of control and automation that is unmatched.

In this guide, we are going to look at how to take a high-quality master file and crunch it down for the web without turning it into a pixelated mess. We aren't just making the file smaller; we are optimizing its internal structure so it streams instantly.

The Philosophy of Web Video: Bitrate vs. Quality

Before we run a single command, we need to understand what we are actually doing. Compression is essentially the art of throwing away data that the human eye won't notice.

The primary metric you are fighting against is Bitrate. This is the amount of data required to store one second of video.

  • High Bitrate: Great quality, massive file size, slow buffering on mobile networks.
  • Low Bitrate: Tiny file size, fast loading, but potential for "blocky" artifacts.

For the web, we generally want Variable Bitrate (VBR). This allows FFmpeg to use more data for complex scenes (like an explosion or confetti) and less data for static scenes (like a talking head against a plain wall).

The Holy Grail Command for Compatibility (H.264)

Despite the rise of newer codecs like AV1 and H.265, the H.264 codec (inside an MP4 container) remains the undisputed king of compatibility. It works on old iPhones, cheap Androids, and every desktop browser.

Here is the standard "set it and forget it" command for web optimization:

ffmpeg -i input_video.mov -c:v libx264 -crf 23 -preset veryfast -c:a aac -b:a 128k -movflags +faststart output_web.mp4

That looks like a mouthful. Let's break down exactly what is happening under the hood, because understanding these flags is what separates a novice from a video engineer.

1. The Codec: -c:v libx264

This tells FFmpeg to use the H.264 video library. It is widely supported and hardware accelerated on almost all modern devices, meaning it won't kill your user's battery life.

2. The Quality Control: -crf 23

This is the most important setting for quality. CRF stands for Constant Rate Factor. Instead of targeting a specific file size (like "make this 50MB"), you are targeting a specific quality level, and FFmpeg figures out the bitrate needed to achieve it.

  • The scale generally runs from 0 to 51.
  • 0 is lossless (huge files).
  • 51 is garbage.
  • 17-18 is usually considered "visually lossless" (indistinguishable from the source).
  • 23 is the default and a great starting point for the web.
  • 28+ is good for highly compressed background videos where detail matters less.

Pro Tip: If your file is too big, increase the CRF value. If it looks blocky, lower the CRF value.

3. The Speed vs. Efficiency Trade-off: -preset veryfast

This setting determines how hard the CPU works to compress the file. Slower presets result in smaller files for the same quality, but they take longer to encode.

Common options include: ultrafast, superfast, veryfast, medium (default), slow, veryslow.

For web streaming, veryfast or medium is usually the sweet spot. If you are archiving a movie and don't care if the encoding takes 10 hours, use veryslow to save a few megabytes. If you are building a transcoding pipeline for a startup, veryfast saves you server costs.

4. The Web Streaming Magic: -movflags +faststart

If you remember only one thing from this article, let it be this flag.

By default, an MP4 file writes its metadata (the "moov atom"—the index that tells the player where frames are located) at the end of the file. If you put that file on a website, the browser has to download the entire video before it can start playing the first second.

-movflags +faststart moves that metadata to the beginning of the file. This allows the browser to start playback almost instantly while the rest of the file downloads in the background.

Handling Audio

Video engineers often forget audio, but bad audio ruins a video faster than pixelated graphics. In our command, we used:

-c:a aac -b:a 128k

This converts the audio to AAC (Advanced Audio Coding), which is the standard for MP4. We set the bitrate to 128kbps, which is generally CD-quality transparency for most listeners. If you are compressing a speech or a lecture, you can drop this to 96k or even 64k to save space. For a music video, bump it to 192k or 256k.

Pixel Formats and "Why won't this play on Safari?"

Sometimes you encode a video, and it plays fine in VLC but shows a black screen in QuickTime or Safari. This is usually a pixel format issue.

Professional cameras often record in 4:2:2 or 4:4:4 color spaces. Most web browsers and hardware decoders only support 4:2:0 (YUV420P). To ensure maximum compatibility, you should explicitly force the pixel format:

ffmpeg -i input.mov -c:v libx264 -pix_fmt yuv420p output.mp4

This forces the color space to the web standard, ensuring your colors don't look washed out or broken on Apple devices.

Resizing and Scaling

Serving a 4K video to a user on a mobile phone is a waste of bandwidth. You should often resize your video to 1080p or 720p for web distribution. FFmpeg uses the scale filter for this.

To resize to 1080p width while maintaining the aspect ratio:

-vf scale=1920:-2

Why -2? It tells FFmpeg to calculate the height automatically to keep the aspect ratio, but ensures the height is a multiple of 2 (which is required for H.264 encoding). If you used -1, you might end up with a height like 803 pixels, and the encoder would crash.

Next-Gen: Moving to H.265 (HEVC) or VP9

If you are willing to sacrifice some compatibility for better compression (or if you are serving different files to different browsers), you can look at H.265 or VP9.

H.265 (HEVC)

HEVC offers about 25-50% better compression than H.264 at the same quality. However, licensing is a nightmare, and browser support is historically spotty (though improving). Use this if bandwidth cost is your primary concern and you have a fallback mechanism.

ffmpeg -i input.mp4 -c:v libx265 -crf 28 -c:a aac -b:a 128k output_hevc.mp4

Note that CRF values in x265 don't map 1:1 to x264. You usually want a slightly higher number for HEVC.

VP9 (The Google Route)

VP9 is open and royalty-free. It plays beautifully on Chrome and Android. It is highly efficient but encodes very slowly compared to x264.

ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -c:a libopus output_vp9.webm

Here we use -b:v 0 to enforce constant quality mode via CRF. We also switch the container to WebM and the audio to Opus, which is a superior audio codec at lower bitrates.

Summary Checklist

Before you push that file to your S3 bucket or web server, run through this mental checklist:

  1. Container: Is it MP4? (Or WebM for modern fallbacks?)
  2. Codec: Is it H.264 for max compatibility?
  3. Audio: Is it AAC at a reasonable bitrate?
  4. Faststart: Did you move the Moov atom so it streams immediately?
  5. Pixel Format: Is it yuv420p?

FFmpeg is a deep rabbit hole. You can use it to burn in subtitles, add watermarks, or segment video for HLS streaming. But mastering the simple CRF encoding command above puts you ahead of 90% of the people blindly uploading raw files to the web.