How to Convert AVI to MP4 using FFmpeg? Lossy and Lossless Conversion

In this article, let’s learn how to use FFmpeg to convert videos from AVI to MP4 format – with and without re-encoding the AVI file. As a bonus, learn how to install FFmpeg on Ubuntu, Mac, Windows, and do a lossless conversion from AVI to MP4 using FFmpeg.


One of the most asked questions online by video enthusiasts is “How do I convert AVI to MP4?”. Though the question might seem intimidating, the converting AVI to MP4 using FFmpeg is simple and straightforward.

For those who do not know, FFmpeg is one of the most important and popular video processing and compression libraries there is! It powers some of the most efficient encoders that are commercially available along with software like Handbrake and VLC player.

In this article, let us understand the format conversion process, get a basic understanding of codecs, containers, and see whether we should re-encode the video during the format conversion process or not.

Note: if you are looking for steps to convert MKV to MP4, then go here.

Please Stop Talking and Just Give Me the Command

Ok, if you are in a hurry to convert AVI to MP4 using FFmpeg, then simply run the following FFmpeg command. It should work for most of the use-cases.

ffmpeg -i input_filename.avi -c:v copy -c:a copy -y output_filename.mp4

In this commandline, you are providing

  • the AVI video as input
  • specifying the name of the output MP4 file,
  • instructing FFmpeg to directly copy the audio and video (seen here: -c:v copy -c:a copy) from the AVI container format to the MP4 container format.

Note: I am assuming you already have an AVI file that you want to convert. If not, then you can get one online by searching for “avi video samples” or simply download this file https://file-examples-com.github.io/uploads/2018/04/file_example_AVI_1280_1_5MG.avi which is what I will be using for this tutorial.

If you want to know what’s happening under the hood, then read on. If not, thank you for visiting and hope to see you again 🙂

What are Codecs and Containers?

The most important thing you need to understand is the difference between containers and codecs.

Simply put,

  • codec takes raw audio and video and compresses it so that it can fit in a reasonable amount of space. This compression is done taking video and audio quality into consideration. Read more about Video Compression here where we describe the “why” of video compression.
  • The output of the video and audio compression process is packaged into a well-defined format called a container and there are rules and guidelines on how to unpack the container and read the audio + video. There are different container formats such as MP4, AVI, WebM, MKV, etc.

I’ll write a series of articles on containers soon – so please subscribe to OTTVerse and get notified when new articles are published!

What Happens When FFmpeg Converts from AVI to MP4

When you give FFmpeg an AVI file and tell it to produce an MP4 file without any other parameters, it will demux the input file, extract the raw video, re-encode it, and put it into an MP4 container format, and give you the output file.

The critical part to note here is that, FFmpeg will re-encode the content unless you tell it not to. Remember this at all costs! Re-encoding is the default setting.

Which is why, we instructed FFmpeg to only copy the audio and video at the start of this article.

Now, let’s try to convert an AVI file to MP4 using FFmpeg with re-encoding.

Before that, how about we inspect the AVI file and look into its parameters? For that, we can use ffprobe that is a utility running FFmpeg internally and is very useful for inspecting video files.

Run the following command –

ffprobe -i file_example_AVI_1280_1_5MG.avi

And you get,

Input #0, avi, from 'file_example_AVI_1280_1_5MG.avi':
  Metadata:
    encoder         : Lavf57.19.100
  Duration: 00:00:30.61, start: 0.000000, bitrate: 387 kb/s
    Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 234 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc
    Stream #0:1: Audio: aac (LC) ([255][0][0][0] / 0x00FF), 48000 Hz, stereo, fltp, 139 kb/s

It clearly shows that there are two streams – video #0:0 and audio #0:1. The video resolution is 1280x720, 30 fps, and encoded at 234 kbps using H.264/AVC’s High profile. The audio uses aac at 139 kbps and 48 KHz.

Now, let’s tell FFmpeg to convert this AVI video to MP4, but, instead of H.264/AVC, let’s use VP9. Here’s how –

ffmpeg -i file_example_AVI_1280_1_5MG.avi -c:a copy -c:v vp9 -b:v 100K outputVP9.mp4

Here, you are instructing FFmpeg to

  • copy the audio as is using the -c:a copy parameter (no audio re-encoding),
  • use vp9 to re-encode the video at a bitrate of 100kbps (-c:v vp9 -b:v 100K )
  • and provide the output in an MP4 file named outputVP9.mp4.

Here is what the console output looks when you run the above command –

Input #0, avi, from 'file_example_AVI_1280_1_5MG.avi':
  Metadata:
    encoder         : Lavf57.19.100
  Duration: 00:00:30.61, start: 0.000000, bitrate: 387 kb/s
    Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p(progressive), 1280x720 [SAR 1:1 DAR 16:9], 234 kb/s, 30 fps, 30 tbr, 30 tbn, 60 tbc
    Stream #0:1: Audio: aac (LC) ([255][0][0][0] / 0x00FF), 48000 Hz, stereo, fltp, 139 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> vp9 (libvpx-vp9))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[libvpx-vp9 @ 0x7f8fcb00f600] v1.8.2
Output #0, mp4, to 'outputVP9.mp4':
  Metadata:
    encoder         : Lavf58.45.100
    Stream #0:0: Video: vp9 (libvpx-vp9) (vp09 / 0x39307076), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 100 kb/s, 30 fps, 15360 tbn, 30 tbc
    Metadata:
      encoder         : Lavc58.91.100 libvpx-vp9
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
    Stream #0:1: Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 139 kb/s
frame=  903 fps=1.2 q=0.0 Lsize=     959kB time=00:00:30.54 bitrate= 257.2kbits/s dup=2 drop=0 speed=0.0414x
video:421kB audio:521kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.800530%

If you read through the console output carefully, you can see that FFmpeg is indeed re-encoding the video using libvpx-vp9 at 100kbps as requested.

As always, you can verify that the conversion was done correctly using the specified VP9 codec and MP4 container using ffprobe.

ffprobe -i outputVP9.mp4

The console output confirms encoding using VP9 at 114 kbps (tries to achieve 100kbps)

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'outputVP9.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.45.100
  Duration: 00:00:30.57, start: 0.000000, bitrate: 257 kb/s
    Stream #0:0(und): Video: vp9 (Profile 0) (vp09 / 0x39307076), yuv420p(tv, progressive), 1280x720, 114 kb/s, SAR 1:1 DAR 16:9, 30 fps, 30 tbr, 15360 tbn, 15360 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 137 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

The major-brand is isom which tells us that this is file conforms to ISO/IEC base media file format (ISO/IEC 14496-12 – MPEG-4 Part 12) i.e, MP4.

Is Re-encoding During Conversion Good or Bad?

Well, it depends on whether you want to do a direct copy of the video and only change the container (i.e., AVI to MP4), OR, you indeed want to re-encode the video and change the video quality, size, etc.

What if I Don’t Re-Encode (Lossless conversion from AVI to MP4)

Well, in that case your job is very easy. Just run the following commandline and you’re done!

ffmpeg -i input_filename.avi -c:v copy -c:a copy -y output_filename.mp4

Here you are telling FFMpeg the following things –

  • the AVI video is the input
  • specifying the name of the output MP4 file,
  • instructing FFmpeg to directly copy the audio and video (seen here: -c:v copy -c:a copy) from the AVI container format to the MP4 container format.

By the way, the above operation takes 0.3 seconds on my Macbook. That’s because FFmpeg is only copying the audio and video from the AVI file and putting it into an MP4 container.

In contrast, the re-encoding procedure we tried earlier took

You can consider this sort of AVI to MP4 conversion as a lossless conversion because the video is not being re-encoded. However, if I were you, I’d be careful using the word lossless video compression within 10 feet of a video compression engineer. Even if you do, please buy him/her a cup of coffee and some Aspirin 🙂

So, that’s it – now you know how to convert AVI to MP4 using FFmpeg. Easy, wasn’t it? If you have any questions or if you are stuck somewhere, let me know in the comments and I’ll help you out.

Bonus: FFmpeg installation guide

How to Install FFmpeg on Windows?

On a Windows machine, it is a complex process to compile and install FFmpeg if you are not a programmer and don’t have access to coding tools. For almost all purposes, you can head over to OTTVerse’s FFmpeg builds and download compiled .exe FFmpeg files and use that. I suggest that you download the static versions, unless you are planning on building software using FFmpeg and need shared libraries.

How to Install FFmpeg on MacOS?

On a Mac, you can install FFmpeg using brew which is a superb package manager for the Mac ecosystem. Here is the one-liner to install FFmpeg on a Mac machine (testing and working on Cataline v 10.15.5)

brew install ffmpeg

How to Install FFmpeg on Ubuntu?

On Ubuntu, you can install FFmpeg in a couple of ways – using the apt package manager or compiling from source. To simply install it using apt, use the following commandline,

sudo apt install ffmpeg

If you want to compile FFmpeg from source, then you can use the procedure mentioned in the FFmpeg Official Installation Guide. Or, you can refer to this article that I wrote and I have described the FFmpeg installation procedure including missing steps.

krishna rao vijayanagar
Krishna Rao Vijayanagar

Krishna Rao Vijayanagar, Ph.D., is the Editor-in-Chief of OTTVerse, a news portal covering tech and business news in the OTT industry.

With extensive experience in video encoding, streaming, analytics, monetization, end-to-end streaming, and more, Krishna has held multiple leadership roles in R&D, Engineering, and Product at companies such as Harmonic Inc., MediaMelon, and Airtel Digital. Krishna has published numerous articles and research papers and speaks at industry events to share his insights and perspectives on the fundamentals and the future of OTT streaming.

Pallycon April NAB 2024

5 thoughts on “How to Convert AVI to MP4 using FFmpeg? Lossy and Lossless Conversion”

  1. Unfortunately, this does not explain what to do when copying without re-encoding doesn’t work. In my case I get this error:

    [mp4 @ 0x564d246f5700] Could not find tag for codec msmpeg4v3 in stream #0, codec not currently supported in container

    So the MP4 container is incapable of containing the codec which is in this particular AVI. Is the only solution to use re-encoding, ie. converting to a different codec?

    Chris

    1. Hi Chris,

      It looks like in your situation the “MPEG-4 Part 2 Microsoft variant version 3” codec is not supported in the mp4 container. See here and here.

      It appears that re-encoding would be the way forward. I found a similar question online – perhaps, this might help you. Good luck!

  2. Could you please help me? I need to convert some little avi files my granddaughter made on her toy camera but I want to conserve the metadata original creation date when converting to an h.264 mp4 file. Can the ffmpeg you are talking about do that or do you know of something that will?
    Thank You very much for your help!
    Mac (and yes I am a MacBook user).
    Please write me back via email as I only came across your post while searching for help on the internet and will loose this comment page.

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.