FFmpeg can convert between virtually any video format. This guide covers the most common conversions.
Understanding Containers vs Codecs
- Container: The file format (MP4, MKV, WebM, AVI)
- Video Codec: How video is compressed (H.264, H.265, VP9)
- Audio Codec: How audio is compressed (AAC, MP3, Opus)
Convert AVI to MP4
ffmpeg -i input.avi -c:v libx264 -c:a aac -movflags +faststart output.mp4
The -movflags +faststart flag optimizes for web streaming.
Convert MKV to MP4
# If codecs are compatible, just copy streams (fastest) ffmpeg -i input.mkv -c copy output.mp4 # If re-encoding is needed ffmpeg -i input.mkv -c:v libx264 -c:a aac output.mp4
Convert MP4 to WebM
ffmpeg -i input.mp4 -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus output.webm
Convert to H.265/HEVC (50% smaller files)
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -preset medium -c:a aac output_h265.mp4
Batch Conversion
# Convert all AVI files in folder to MP4
for f in *.avi; do
ffmpeg -i "$f" -c:v libx264 -c:a aac "${f%.avi}.mp4"
done
Preserve Quality (Lossless Copy)
# Just change container, no re-encoding ffmpeg -i input.mkv -c copy output.mp4
This is instant but only works if the codecs are compatible with the target container.
Recommended Settings by Use Case
| Use Case | Format | Settings |
|---|---|---|
| Web streaming | MP4 (H.264) | -crf 23 -preset fast |
| Archive/Quality | MKV (H.265) | -crf 18 -preset slow |
| HTML5 fallback | WebM (VP9) | -crf 30 -b:v 0 |