AVI is a container format from 1992. Old camcorder clips, ripped DivX/Xvid movies, MJPEG captures and recordings from old screen-capture tools are often .avi files, and modern phones, browsers and editors rarely play them properly. Converting them to MP4 fixes that. The safest way is usually to re-encode to H.264 video and AAC audio. In a few cases you can change only the container instead.
1. What’s Inside an AVI
An AVI file can hold many different codecs, depending on when and with which tool it was made:
| Common video codec | Era / source | MP4-compatible? |
|---|---|---|
| DivX / Xvid (MPEG-4 ASP) | Ripped movies, 2000s | Sometimes (needs care) |
| MJPEG | Old cameras, capture cards | Copyable, but many players can’t play it |
| Cinepak, Indeo, MS-MPEG4 | 1990s clips | No |
| H.264 in AVI (rare) | Some capture tools | Yes (copyable) |
| Common audio codec | MP4-compatible? |
|---|---|
| MP3 | Yes (copyable) |
| PCM (uncompressed) | Re-encode (8-bit/μ-law cannot be copied) |
| AC-3 | Yes in MP4, but re-encode for safety |
You cannot predict what is inside, so re-encoding to H.264 + AAC is the reliable default. The result plays everywhere, whatever the AVI contained.
2. The One Command for (Almost) Any AVI
This re-encodes both video and audio and prepares the file for web playback:
ffmpeg -i input.avi -c:v libx264 -crf 23 -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart output.mp4
-c:v libx264… encode video as H.264, the most compatible codec-crf 23… quality level (lower = better quality, bigger file)-pix_fmt yuv420p… store the color as 4:2:0, which almost every device plays. Without it, many MJPEG and DV sources become 4:2:2 H.264 (High 4:2:2), which phones and other devices may not play-c:a aac -b:a 192k… re-encode audio to AAC; 192k suits the MP3 audio common in AVIs-movflags +faststart… move the file’s index (the moov atom) to the start, so web playback can begin right away
It works the same for DivX, Xvid, MJPEG, PCM and MP3 input.
3. When Stream-Copy Works (and When It Doesn’t)
If your AVI already contains codecs that MP4 supports (most reliably H.264 video with MP3 or AAC audio), you can change the container without re-encoding. This takes a second or two and loses no quality:
ffmpeg -i input.avi -c:v copy -c:a copy -movflags +faststart output.mp4
It can fail, or give a file that many players cannot play, when:
- The video is DivX/Xvid (MPEG-4 ASP). FFmpeg can often copy it into MP4 (stored as
mp4v), but many modern players and devices handle it poorly. Re-encoding to H.264 is more reliable. - The video is Cinepak, Indeo or MS-MPEG4. These cannot be copied into MP4 at all. MJPEG can be copied, but many players cannot play it.
- The audio is PCM. FFmpeg 8.1 can put 16/24-bit PCM into MP4, but many players cannot play it. 8-bit and μ-law PCM cannot go into MP4 at all.
To see what is inside:
ffprobe -hide_banner input.avi
Look at the Video: and Audio: lines. Video: h264 with Audio: mp3 or aac copies cleanly. DivX/Xvid shows as mpeg4; it can often be copied too (stored as mp4v), but H.264 plays on more devices. For MJPEG, Cinepak, Indeo or PCM audio, re-encode with the Section 2 command.
A common middle ground is to copy the video and re-encode only the audio:
ffmpeg -i input.avi -c:v copy -c:a aac -b:a 192k -movflags +faststart output.mp4
Use this when the video is already H.264 but the audio is PCM or another format MP4 players do not handle well.
4. Choosing a CRF Value (Quality vs. Size)
CRF sets the balance between quality and file size when you re-encode:
| CRF | Visual quality | Typical use | File size |
|---|---|---|---|
| 18 | Visually lossless | Preserving rare footage | Large |
| 20 | Excellent | Family videos worth keeping | Medium-large |
| 23 | Very good (default) | General conversion | Medium |
| 26 | Good | Casual viewing | Small |
Old AVIs are often low resolution (640×480 or smaller), so even a low CRF gives a small file. For footage you want to keep, use CRF 18–20:
ffmpeg -i input.avi -c:v libx264 -crf 18 -preset slow -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart output.mp4
H.264 is more efficient than MPEG-4 ASP, but re-encoding does not always make the file smaller. A low-bitrate DivX/Xvid source can come out larger at CRF 20.
5. Fixing Old AVI Audio
Audio causes many AVI problems:
- MP3 audio can be copied into MP4 with
-c:a copy. Re-encoding it to AAC at 192k makes it play on more players. - PCM audio should be re-encoded to AAC. 16/24-bit PCM can be copied into MP4, but many players cannot play it. 8-bit and μ-law PCM cannot be copied.
- AC-3 audio can stay as it is, but re-encoding it to stereo AAC avoids playback problems on phones.
This command converts the audio to stereo AAC:
ffmpeg -i input.avi -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 192k -ac 2 -movflags +faststart output.mp4
-ac 2 makes the output stereo, so devices that cannot handle surround tracks from old captures still play sound.
6. Deinterlacing Camcorder Footage
Many AVI files from MiniDV camcorders and analog capture cards are interlaced. On moving objects this shows up as comb-like horizontal lines. Add the yadif filter to remove them (deinterlace):
ffmpeg -i input.avi -vf yadif -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart output.mp4
-vf yadif turns the video into clean progressive frames. Motion may still look slightly soft. That is normal: interlaced video loses a little detail when it is deinterlaced.
7. Troubleshooting
Error 1: Could not find tag for codec ... in stream
Cause: You used -c:v copy on a video codec that MP4 does not accept, such as Cinepak, Indeo or MS-MPEG4. DivX/Xvid and MJPEG can usually be copied.
Fix: Re-encode the video with -c:v libx264 (Section 2).
Error 2: Output has no sound
Cause: -c:a copy copied PCM or another audio format the player cannot play.
Fix: Re-encode the audio: -c:a aac -b:a 192k.
Error 3: Video shows horizontal comb lines on motion
Cause: The source is interlaced.
Fix: Add -vf yadif to deinterlace (Section 6).
Error 4: Audio and video drift out of sync
Cause: Some old AVIs have a variable frame rate or a broken header.
Fix: Set a constant frame rate that matches the source when you re-encode. The example is for 30 fps footage; for 25 fps (PAL) footage, use -r 25. A different rate makes FFmpeg duplicate or drop frames:
ffmpeg -i input.avi -r 30 -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 192k -movflags +faststart output.mp4
Error 5: File starts playing only after a long buffer online
Cause: The file was written without faststart, so its index is at the end.
Fix: Add -movflags +faststart (it is in every command above).
FAQ
Q1. Will converting AVI to MP4 reduce quality? A. Re-encoding loses some quality, but at CRF 18–20 the loss is usually invisible. If your AVI already holds H.264 + MP3, copying the streams (Section 3) is lossless.
Q2. How do I know if I can use the fast copy method?
A. Run ffprobe input.avi. h264 with mp3 or aac copies cleanly. DivX/Xvid (mpeg4) can usually be copied too (as mp4v), but re-encoding to H.264 plays on more devices. For other codecs such as MJPEG or PCM, re-encode.
Q3. Why does my old AVI fail to play on my phone even after copying? A. Copying changes the container, not the codec. The built-in players on phones usually cannot play DivX/Xvid, so re-encode to H.264.
Q4. What CRF is best for irreplaceable home videos?
A. Use CRF 18 with -preset slow to keep as much detail as possible.
Q5. My AVI is tiny and low-resolution. Should I upscale? A. Usually not. Upscaling cannot add detail. Convert at a low CRF to keep the detail that is there.
Q6. Can I batch-convert a folder of AVI files? A. Yes. Use a shell loop that runs the Section 2 command once for each file.