how to convert avi (xvid) to mkv or mp4 (h264)

The standard command from http://manpages.ubuntu.com/manpages/precise/man1/avconv.1.html shows this:

avconv -i test.avi -c:v libx264 -c:a copy outputfile.mp4

This will re-encode your video to h264 (=AVC) and keep your sound unchanged which is mp3 and completely legal to the standard.

A few other options to consider with this commandline are:

  • Tweak the quality of the output video by using the -b parameter (bitrate) or use Constant Rate Factor (CRF) with something like -crf 22
  • Use a libx264 preset such as -preset veryslow for improved video

If you use these the options should be placed after -c:v libx264 option...

You may also look at the GUI programs winff and avidemux which will make things less complicated. (also handbrake is a good one but i never came to terms with it )


In case you are looking for something graphical, you can consider using mkvtoolnix-gui package. It actually depends on the command line utility, but it will execute the command with all the needed options for you.

mkvmerge gui 1

The first thing that you'll need to do is to add the files containing all the needed streams. You do that (obviously) by pressing add button and selecting the file or by dragging your files into the "Input files" area. You should not use append button, unless your intention is to place the contents of one file after the contents of another (making the final file's duration the sum of all the appended ones).

Since you are saying you are planning to just convert .avi files, then you should simply add one file at the time. Note that the first file that you will add will automatically set the name of the output file, it will match the input name, but the extension will be changed to .mkv. You can of course change that if you wish.

After you add something you will be able to change the parameters of the streams (such as the assigned language, whether the track is "on" by default, and whether a user can chose to disable this stream all together if one wishes.

You can give the title for the stream. I suggest that if you have multi-audio video that you use that together with the language. For example you can have "original" or "single-voice dub" or "director's commentary" for the track names. Many players will show you these when you select the stream to play, and it is very useful. Try not to repeat the information there, however. There is no reason to write "English language" when you can just set the track's language code.

There are also some specific options, which will depend upon which stream you are working on.

mkvmerge gui 2

Sometimes you may wish to add some attachments to your file. If you add an image, many file managers will display this picture as the icon for the final video file. If you are muxing some sort of film, and you happen to have the cover for it, it may be a good choice to put it in as the attachment.

mkvmerge gui 3

Finally in the Global tab you are able to set the metadata for the whole file. The most common thing is to set the title. It will be prefilled by the data taken from the first file that you have added to the "Input files".

You can also add chapter information. Mkvmerge is able to read ogm chapter file as well as xml chapters. It also allows you to edit chapter files, but keep in mind that this tab functions as a separate application, so you won't be editing chapters that are added to your output file, you will need to save them in that tab, and then add them as the input to the file you are now muxing.

The interesting feature of mkvmerge gui is the "Copy to clipboard" which copies the exact terminal command that the application is planning to run. If you intend to mux many files you can set one up, just the way you like it, then copy the command to clipboard and make a bash script that does all the files you want at once.


Avidemux is a free video editor designed for simple cutting, filtering and encoding tasks. It supports many file types, including AVI, DVD compatible MPEG files, MP4 and ASF, using a variety of codecs. Tasks can be automated using projects, job queue and powerful scripting capabilities.Avidemux is available for Ubuntu.

sudo apt-get update
sudo apt-get install avidemux