What video formats and codecs does FFmpeg support? How to choose the appropriate format?
FFmpeg supports numerous video formats and codecs. Understanding their characteristics is crucial for choosing the appropriate encoding scheme.
Common Video Container Formats
MP4 (MPEG-4 Part 14)
Features:
- Excellent compatibility, supported by almost all devices
- Supports multiple codecs (H.264, H.265, AAC, etc.)
- Suitable for network transmission and storage
Use Cases: Web video, mobile devices, social media
bash# Create standard MP4 ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4 # Create fast-start MP4 ffmpeg -i input.mp4 -c:v libx264 -c:a aac -movflags +faststart output.mp4
MKV (Matroska)
Features:
- Open source format, powerful features
- Supports almost all codecs
- Supports multiple audio tracks and subtitle tracks
- Suitable for storage and collection
Use Cases: HD video storage, multi-language videos
bash# Create MKV ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mkv # Preserve all tracks ffmpeg -i input.mp4 -c copy output.mkv
AVI (Audio Video Interleave)
Features:
- Legacy format, good compatibility
- Relatively simple features
- Does not support modern codecs
Use Cases: Legacy device compatibility
bash# Create AVI ffmpeg -i input.mp4 -c:v mpeg4 -c:a mp3 output.avi
WebM
Features:
- Open source format developed by Google
- Optimized for web
- Supports VP8, VP9, AV1 codecs
- Small file size, good quality
Use Cases: Web video, HTML5 video
bash# Create WebM ffmpeg -i input.mp4 -c:v libvpx -c:a libvorbis output.webm # Use VP9 encoding ffmpeg -i input.mp4 -c:v libvpx-vp9 -c:a libopus output.webm
MOV
Features:
- Format developed by Apple
- Supports professional codecs like ProRes
- Suitable for professional video production
Use Cases: Professional video production, Apple devices
bash# Create MOV ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mov # Use ProRes encoding ffmpeg -i input.mp4 -c:v prores_ks -profile:v 3 output.mov
Common Video Codecs
H.264 (AVC)
Features:
- Most widely used video encoding standard
- Excellent compatibility
- Medium compression efficiency
- Fast encoding speed
Use Cases: General video, web video, mobile devices
bash# H.264 encoding ffmpeg -i input.mp4 -c:v libx264 -crf 23 output.mp4 # High quality H.264 ffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow output.mp4 # Fast H.264 ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset ultrafast output.mp4
H.265 (HEVC)
Features:
- Successor to H.264
- 50% higher compression efficiency than H.264
- Slower encoding speed
- Compatibility is improving
Use Cases: 4K/8K video, high compression requirements
bash# H.265 encoding ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4 # High quality H.265 ffmpeg -i input.mp4 -c:v libx265 -crf 23 -preset slow output.mp4
VP9
Features:
- Open source codec developed by Google
- Compression efficiency close to H.265
- Completely free
- Suitable for web video
Use Cases: Web video, YouTube
bash# VP9 encoding ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 output.webm # High quality VP9 ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 18 -b:v 0 output.webm
AV1
Features:
- Next-generation open source codec
- 30% higher compression efficiency than H.265
- Slow encoding speed
- Limited compatibility
Use Cases: Future video, high quality compression
bash# AV1 encoding ffmpeg -i input.mp4 -c:v libaom-av1 -crf 30 -strict experimental output.webm
ProRes
Features:
- Professional codec developed by Apple
- Visually lossless compression
- Larger file size
- Suitable for post-production
Use Cases: Professional video production, post-production
bash# ProRes encoding ffmpeg -i input.mp4 -c:v prores_ks -profile:v 3 output.mov # ProRes 422 ffmpeg -i input.mp4 -c:v prores_ks -profile:v 1 output.mov
Audio Codecs
AAC
Features:
- Most widely used audio encoding
- Excellent compatibility
- Good quality, small file size
- Supports multi-channel
Use Cases: General audio, web audio
bash# AAC encoding ffmpeg -i input.wav -c:a aac -b:a 128k output.aac # High quality AAC ffmpeg -i input.wav -c:a aac -b:a 256k output.aac
MP3
Features:
- Most popular audio format
- Excellent compatibility
- Lossy compression
- Suitable for music
Use Cases: Music, portable devices
bash# MP3 encoding ffmpeg -i input.wav -c:a libmp3lame -b:a 192k output.mp3 # High quality MP3 ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3
Opus
Features:
- Open source audio codec
- Excellent quality, low latency
- Suitable for speech and music
- Small file size
Use Cases: Web audio, voice calls
bash# Opus encoding ffmpeg -i input.wav -c:a libopus -b:a 128k output.opus # High quality Opus ffmpeg -i input.wav -c:a libopus -b:a 256k output.opus
FLAC
Features:
- Lossless audio compression
- Quality same as original file
- Smaller file than WAV
- Suitable for music collection
Use Cases: Music collection, lossless audio
bash# FLAC encoding ffmpeg -i input.wav -c:a flac output.flac
Format Selection Guide
Choose Based on Use Case
| Scenario | Recommended Format | Recommended Codec |
|---|---|---|
| Web video | MP4 | H.264 + AAC |
| HD storage | MKV | H.265 + AAC |
| Web video | WebM | VP9 + Opus |
| Professional production | MOV | ProRes |
| Music storage | FLAC | FLAC |
| Mobile devices | MP4 | H.264 + AAC |
Choose Based on Quality Requirements
High Quality:
bashffmpeg -i input.mp4 -c:v libx264 -crf 18 -preset slow -c:a aac -b:a 256k output.mp4
Medium Quality:
bashffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Low Quality (Small File):
bashffmpeg -i input.mp4 -c:v libx264 -crf 28 -preset fast -c:a aac -b:a 96k output.mp4
Format Conversion
Format Interconversion
bash# MP4 to MKV ffmpeg -i input.mp4 -c copy output.mkv # AVI to MP4 ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4 # WebM to MP4 ffmpeg -i input.webm -c:v libx264 -c:a aac output.mp4
Codec Conversion
bash# H.264 to H.265 ffmpeg -i input.mp4 -c:v libx265 -crf 28 output.mp4 # VP9 to H.264 ffmpeg -i input.webm -c:v libx264 -crf 23 output.mp4
Choosing the appropriate format and codec requires considering multiple factors such as compatibility, quality, file size, and encoding speed.