Convert video containers

Using FFMPEG

Perhaps the easiest tool for that is ffmpeg, or avconv from the libav-tools package. Libav is a fork of FFmpeg, which Ubuntu switched to for a few years until Ubuntu 15.04. It is one of the backends for many of the GUI tools mentioned in other answers.

Changing container without re-enconding content could not be simpler:

ffmpeg -i input.mkv -codec copy output.mp4

    It auto-detects a Matroska to MP4 container conversion based on input/output filenames.

    -codec copy stream copies, or "re-muxes", the streams from the input to the output without re-encoding. Think of it like a copy and paste.

    Default stream selection behavior is to select only one stream per stream type. For example, if your input has two video streams and one audio stream then only the video stream with the largest frame size will be selected. Add -map 0 if you want to select all streams from the input.

    Some containers may not support some formats. So check if your chosen container format, be it mkv, mp4 or even avi has support for all the content in your files (video, audio, subtitles, data, etc). For example, mp4 does not support SubRip subtitles (.srt files).

To transcode the audio to AAC, leaving the video as AVC: avconv -i input.mkv -c:v copy -c:a libfaac output.mp4 (I used ffmpeg, I assume avconv has the same command line args here) – 

If you want to transfer both the video and audio (both losslessly) and for it not to choke on subtitles then: avconv -i input.mkv -c:v copy -c:a copy -sn output.mp4

MP4 supports the most of the common audio formats used in MKVs including MP3 and AAC. – 

You can also use ffmpeg to cut off a piece of a file without the need te re encode the stream, for example if you stopped the recording too late and want a file for the first second  till 50 minutes and 10 seconds:

ffmpeg -i INFILE.mp4 -vcodec copy -acodec copy -ss 00:00:00.000 -t 00:50:10.000 OUTFILE.mp4

or for mkv files:

ffmpeg -i INFILE.mkv -vcodec copy -acodec copy -ss 00:00:00.000 -t 00:50:10.000 OUTFILE.mkv

Remove or extract Audio

Remove a specific audio stream / track

ffmpeg -i inputfile.mp4 -map 0 -map -0:a:2 -c copy outputfile.mp4


Remove all audio streams / tracks

ffmpeg -i inputfile.mp4 -map 0 -map -0:a -c copy outputfile.mp4


Remove specific audio streams / tracks

Keep everything except audio streams #2 (at offset 1) and #6 (at offset 5):

ffmpeg -i inputfile.mp4 -map 0 -map -0:a:1 -map -0:a:5 -c copy outputfile.mp4


Remove all subtitles and data

ffmpeg -i input -map 0 -map -0:s -map -0:d -c copy output


Only include video and audio

This example does not need to use any negative mapping.

ffmpeg -i inputfile.mp4 -map 0:v -map 0:a -c copy outputfile.mp4


Removing other stream / track types

If you want to remove other stream types you can use the appropriate stream specifier.

v - video, such as -map -0:v

a - audio, such as -map -0:a (as shown above)

s - subtitles, such as -map -0:s

d - data, such as -map -0:d

t - attachments, such as -map -0:t

Extract or remove a specific audio channel

Using a stereo input and channelsplit filter. Example to get the right channel only and output a mono audio file:

ffmpeg -i stereo.wav -filter_complex "[0:a]channelsplit=channel_layout=stereo:channels=FR[right]" -map "[right]" front_right.wav


More info

Using MKVtoolnix


Get needed software:

sudo apt-get install gpac mkvtoolnix

Extract video and audio from matroska file. First you need to check what tracks the matroska file contains with

mkvmerge --identify video.mkv


File 'video.mkv': container: Matroska

Track ID 1: video (V_MPEG4/ISO/AVC)

Track ID 2: audio (A_AC3)


Then extract video and audio according to their tracks with:


mkvextract tracks video.mkv 1:video.h264 2:audio.ac3


Extracting track 1 with the CodecID 'V_MPEG4/ISO/AVC' to the file 'video.h264'. Container format: AVC/h.264 elementary stream

Extracting track 2 with the CodecID 'A_AC3' to the file 'audio.ac3'. Container format: Dolby Digital (AC3)

Progress: 100%


Mux audio and video into mp4 containter


MP4Box -fps 24 -add video.h264 -add audio.ac3 video.mp4


AVC-H264 import - frame size 1280 x 720 at 24.000 FPS

Import results: 31671 samples - Slices: 5672 I 125048 P 122648 B - 32288 SEI - 4936 IDR

AC3 import - sample rate 48000 - 6 channels

Saving to video.mp4: 0.500 secs Interleaving


I had to add -fps 24 option because MP4Box didn't detect video fps correctly. Depending on usage AC3 audio track should be re-encoded into something else before muxing. Now you have remuxed streams from matroska container into mp4 container without losing any quality.

MP4 doesn't officially support AC3 audio so the audio track should be converted into a supported format (eg. AAC, MP3) if you want the file to be playable with something else than VLC.


Using Avidemux

It has good GUI interface. When converting from mkv to mp4 it is not re-encoded. The convertion is done within a minute (for a 60-min video).

Note: Sometimes (~10% chance) it may fail. Then use handbrake(re-encode).

Note2: there are two links which help you:

http://www.ubuntugeek.com/avidemux-free-video-editor-designed-for-simple-cutting-filtering-and-encoding-tasks.html

http://www.ubuntugeek.com/how-to-convert-mkv-file-into-mp4-file-using-avidemux.html