What You’ll Learn in This Article
- Commands to convert any video format (AVI, MKV, MOV, WMV, etc.) to MP4
- How to adjust video quality (CRF parameter)
- How to convert only the container without re-encoding the video (stream copy)
- The trade-off between encoding speed and quality (preset)
Tested version: FFmpeg 6.1 (ubuntu-latest / CI verified) Target OS: Windows / macOS / Linux
Note on filenames: This article uses placeholder filenames such as
input.aviandinput.mkv. In actual commands, replace these with the path of the file you want to process.
Background: The Difference Between Codecs and Containers
- Container (extension):
.mp4,.mkv,.avi, etc. The “wrapper” for video and audio data - Codec: The data compression format. For MP4, the standard is
H.264 (libx264)for video andAACfor audio
The main codecs that can be placed in an MP4 container are H.264 or H.265 (HEVC) for video and AAC for audio.
Basic Command
ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4
| Option | Meaning |
|---|---|
-i input.avi | Specify the input file |
-c:v libx264 | Specify H.264 (libx264) as the video codec |
-c:a aac | Specify AAC as the audio codec |
output.mp4 | Output filename (the .mp4 extension determines the container) |
Primary source: FFmpeg Wiki - H.264 Encoding Guide
Adjusting Video Quality (CRF)
Use -crf to specify video quality. Lower values mean higher quality (larger file size).
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac output.mp4
| CRF value | Quality guide | Use case |
|---|---|---|
| 18 | Near visually lossless | Archiving |
| 23 | Default. Good balance | General use |
| 28 | Stronger compression | File size priority |
| 51 | Lowest quality | Almost never used |
“For general viewing, a range of 18–28 is recommended” — FFmpeg H.264 Guide
Adjusting Encoding Speed (preset)
Use -preset to adjust the trade-off between encoding speed and compression efficiency.
ffmpeg -i input.avi -c:v libx264 -preset slow -crf 22 -c:a aac -b:a 128k output.mp4
| preset | Speed | Compression efficiency |
|---|---|---|
| ultrafast | Fastest | Low |
| fast | Fast | Somewhat low |
| medium | Default | Balanced |
| slow | Slow | High |
| veryslow | Slowest | Highest |
At a constant CRF, slower presets produce smaller file sizes at the same quality. Primary source: H.264 Guide - preset
Stream Copy (No Re-encoding)
When the input is already H.264/AAC, you can convert only the container without re-encoding.
ffmpeg -i input.mkv -c copy output.mp4
- Advantage: Completes instantly with no quality loss
- Note: Fails if the input codec is not compatible with the MP4 container. In that case, switch to
-c:v libx264 -c:a aac.
Specifying Audio Bitrate
ffmpeg -i input.avi -c:v libx264 -crf 23 -c:a aac -b:a 192k output.mp4
-b:a value | Use case |
|---|---|
| 96k | Low quality, audio only |
| 128k | General video |
| 192k | Music / audio quality priority |
| 320k | Highest quality (approximate AAC upper limit) |
Summary of Commonly Used Command Examples
ffmpeg -i input.avi -c:v libx264 -c:a aac output.mp4
ffmpeg -i input.mkv -c:v libx264 -crf 22 -preset slow -c:a aac -b:a 192k output.mp4
ffmpeg -i input.mkv -c copy output.mp4
ffmpeg -y -i input.avi -c:v libx264 -crf 23 -c:a aac output.mp4
The final -y flag skips the overwrite confirmation if the output file already exists (useful for batch processing).
Troubleshooting
Encoder libx264 not found
Cause: The installed FFmpeg build does not include libx264
Solution: Reinstall FFmpeg from the official site or your package manager. On Windows, the Full build from gyan.dev is recommended.
Output File Is Too Large
Increase the CRF value (28–32) or change to -preset fast to reduce the file size.
Stream Copy Fails
-c copy only works when the input codec is compatible with the output container. If it fails, re-encode with -c:v libx264 -c:a aac.
Primary sources: FFmpeg H.264 Wiki / FFmpeg AAC Wiki