I need to convert a video file from Matroska container into mp4 container. Matroska file contains one h.264 video track and one AC3 sound track. It should be possible to do this losslessly, but how to do it with native Ubuntu tools?
Transcoding is not an option.
Perhaps the easiest tool for that is
ffmpeg
, oravconv
from thelibav-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:
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 evenavi
has support for all the content in your files (video, audio, subtitles, data, etc). For example,mp4
does not support SubRip subtitles (.srt
files).Get needed software
Extract video and audio from matroska file
First you need to check what tracks the matroska file contains with
mkvmerge --identify video.mkv
Then extract video and audio according to their tracks with:
mkvextract tracks video.mkv 1:video.h264 2:audio.ac3
Mux audio and video into mp4 containter
MP4Box -fps 24 -add video.h264 -add audio.ac3 video.mp4
I had to add
-fps 24
option becauseMP4Box
didn't detect video fps correctly. Depending on usageAC3
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.
EDIT: 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.
You can use 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).
sudo apt-get install avidemux
Open the mkv file in avidemux.
Select safe mode if prompted.
Leave the video as copy.
Choose File-> Properties. Check if the audio codec is
aac
.aac(faac)
.copy
. (Sometimes using this option the audio may be out of sync or distorted, in that case selectaac(faac)
instead.)Select
mp4
for format.Select save, choose location and type a filename with
.mp4
as the extension.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
You can use my application, dmMediaConverter. Just leave "copy" for both streams checked (no transcoding) and put .mp4 to the output file name. That's it. Very simple. http://dmsimpleapps.blogspot.ro/2014/04/dmmediaconverter.html
Using ffmpeg i write a python script that will convert all the mkv files of the current directory to mp4 files as converting them one by is annoying.
Can use it if needed.
Convert MKV to MP4
You can try HANDBRAKE by adding its PPA by typing this code into the terminal:
Since MPEG Part 14 supports h.264 and AC3 natively without transcoding, you can simply rename the file so it has an
.mp4
extension instead of a.mkv
extension.