Transcoding Video
Convert videos between formats and codecs. Control quality with CRF, manage bitrates, and choose the right encoding preset for your use case.
What is Transcoding?
Transcoding is the process of decoding a video from its current format and re-encoding it into a different format. You might transcode to shrink a file for the web, make a video compatible with a specific device, or switch to a more efficient codec to save storage costs.
This is different from remuxing (which we covered in Lesson 1 using -c copy). Remuxing just changes the container without touching the encoded data. Transcoding re-encodes the data itself.
Containers vs. Codecs
Before diving into transcoding, it helps to understand the difference between containers and codecs — the two concepts people confuse most often:
- Container (MP4, MKV, WebM, MOV) — the "box" that holds video and audio streams, subtitles, and metadata
- Codec (H.264, H.265, VP9, AV1) — the algorithm used to compress and decompress the actual video data
An MP4 file could contain H.264 video, H.265 video, or even AV1 video. The container and codec are independent choices. When you transcode, you are changing the codec. When you remux, you are changing the container.
Choosing a Video Codec
H.264 (libx264) — The Safe Default
Plays everywhere: browsers, phones, smart TVs, social media. If you are unsure which codec to use, use H.264.
H.265 / HEVC (libx265) — Smaller Files
Produces 40-50% smaller files than H.264 at the same quality. Supported on most modern devices but not all browsers.
The -tag:v hvc1 flag ensures compatibility with Apple devices and QuickTime.
VP9 (libvpx-vp9) — Open Source, Web-Optimized
Google's open codec. Used by YouTube. Comparable compression to H.265. Works in Chrome, Firefox, and Edge.
AV1 (libsvtav1) — Best Compression, Slowest Encode
Next-generation codec. 30% smaller than H.265. Encoding is slow, but playback support is growing quickly.
Controlling Quality with CRF
CRF (Constant Rate Factor) is the most common way to control quality in FFmpeg. It tells the encoder: "I care about quality, not file size — pick whatever bitrate is needed to maintain this quality level."
Lower CRF = higher quality = larger file. The scale depends on the codec:
| CRF Value | Quality (H.264) | Use Case |
|---|---|---|
0 | Lossless | Archiving (huge files) |
18 | Visually lossless | Master copies, editing |
23 | Default — good quality | General use (recommended starting point) |
28 | Noticeable compression | Previews, thumbnails |
51 | Worst | Minimum file size |
Tip: Start at CRF 23 for H.264 (or CRF 28 for H.265 — the scales are different). Test the output. If it looks good, try a higher CRF to save space. If you see artifacts, lower it.
Target Bitrate Encoding
When you need predictable file sizes — for example, streaming at a fixed bandwidth — use target bitrate instead of CRF:
Common bitrate targets for H.264:
- 720p: 2-4 Mbps
- 1080p: 4-8 Mbps
- 4K: 15-30 Mbps
Encoding Speed with Presets
Presets control the trade-off between encoding speed and compression efficiency. A slower preset produces a smaller file at the same quality, but takes longer to encode.
The available presets, from fastest to smallest output:
medium is the default and a solid choice for most use cases. Use fast or veryfast when encoding time matters more than file size (e.g. live processing). Use slow for final renders where you want every byte optimized.
Two-Pass Encoding
When you need both a target bitrate and the best possible quality, use two-pass encoding. The first pass analyzes the video complexity. The second pass uses that analysis to distribute bits optimally.
Two-pass is especially useful for long videos with varying complexity — like a tutorial that alternates between a talking head (simple) and a screen recording (complex). The encoder allocates more bits to the complex scenes.
Practical Recipes
Web-optimized MP4 (fast start)
The -movflags +faststart flag moves the metadata to the beginning of the file, so browsers can start playing before the full download completes. Always use this for web video.
Batch convert a folder
Convert to H.265 for storage savings
Key Takeaways
- Containers (MP4, MKV) hold streams; codecs (H.264, H.265) compress them — they are independent choices
- H.264 is the safest default. H.265 saves ~50% space. AV1 saves more but encodes slowly.
- Use CRF when quality matters, target bitrate when file size matters
- Start at CRF 23 (H.264) or CRF 28 (H.265) and adjust from there
- Always add
-movflags +faststartfor web video - Two-pass encoding gives the best quality at a fixed bitrate
Try This With FFmpeg Micro
Instead of running FFmpeg locally, you can use FFmpeg Micro's API to process videos in the cloud. No installation required.