How to convert .mkv file into .mp4 file losslessly?

339,668

Solution 1

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).

Solution 2

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.

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.

Solution 3

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).

  1. sudo apt-get install avidemux

  2. Open the mkv file in avidemux.

  3. Select safe mode if prompted.

  4. Leave the video as copy.

  5. Choose File-> Properties. Check if the audio codec is aac.

    1. If audio codec is NOT aac, select aac(faac).
    2. Otherwise, leave it as copy. (Sometimes using this option the audio may be out of sync or distorted, in that case select aac(faac) instead.)
  6. Select mp4 for format.

  7. 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

Solution 4

enter image description here

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

Share:
339,668

Related videos on Youtube

Seppo Erviälä
Author by

Seppo Erviälä

Updated on September 18, 2022

Comments

  • Seppo Erviälä
    Seppo Erviälä almost 2 years

    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.

    • Binarylife
      Binarylife about 13 years
      See this question.
    • knocte
      knocte over 8 years
      this is especially a solution for converting Matroska files that won't play in a Samsung TV, to files that are compatible with it
    • Seppo Erviälä
      Seppo Erviälä about 8 years
      Over the years I've found this very useful as Chromecast supports H.264 with AAC or MP3 in an MP4 container. It is very easy to stream videos from your computer to Chromecast if you have Chrome with the Google cast extension. Just drag and drop an MP4 with correct stream encodings to a browser window and start streaming.
  • Seppo Erviälä
    Seppo Erviälä about 13 years
    This seems to be a video transcoder. I do not want to compress the video track again as it is already in h.264 format.
  • CyberSkull
    CyberSkull over 12 years
    Handbrake is a great transcoder, but I would never use it for file format conversion.
  • CyberSkull
    CyberSkull over 12 years
    Will this work with subtitle tracks?
  • MestreLion
    MestreLion over 11 years
    I absolutely love Handbreak, but unfortunately it only works losslessly for audio (and subtitles). I couldn't find a way to copy the video without transcoding it, and the OP specifically requested lossless format conversion.
  • camh
    camh over 11 years
    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)
  • MestreLion
    MestreLion over 11 years
    @camh: What you said works, but please note 2 things: 1) It will leave video as-is, regardless if it is AVC or something else; 2) It will not only convert audio to AAC but also the container format from mkv to mp4
  • moraes
    moraes over 11 years
    Worked like a charm. I had sync problems following the accepted answer, but not with this one.
  • thomasrutter
    thomasrutter over 11 years
    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.
  • Jordan Georgiadis
    Jordan Georgiadis over 11 years
    Do you know if this will work for multiple sound tracks?
  • MestreLion
    MestreLion over 11 years
    Yes @BillO'Dwyer. -codec copy means all tracks (formally called "streams", which can be audio, video, subtitles, etc) will be losslessly copied to the new container. So it will change the container from mkv to mp4 without changing the content.
  • Jordan Georgiadis
    Jordan Georgiadis over 11 years
    Thanks @MestreLion, I tried it, but for some reason the subtitles didn't stick... I'll play around and see if I can't fix it :)
  • MestreLion
    MestreLion about 11 years
    @BillO'Dwyer: That's because mp4 container format does not support soft subtitles (.srt files)
  • Jordan Georgiadis
    Jordan Georgiadis about 11 years
    @MestreLion Fair enough... Final question: do you know if this can be done as a batch process?
  • MestreLion
    MestreLion about 11 years
    @BillO'Dwyer: sure, with a bit of bash scripting. something like for video in *.mkv; do avconv -i "$video" -c copy -sn "${video//.*}.mp4"; done
  • MestreLion
    MestreLion over 10 years
    @LordNeckbeard: amazing edit, looks so much better now!
  • MestreLion
    MestreLion almost 9 years
    @AquariusPower: just be aware that the OP does not want to remap the streams: he wants to convert the container only and keep all streams.
  • Aquarius Power
    Aquarius Power almost 9 years
    @MestreLion that's the point, I tried to use the simple command to convert, without the -map, I ended up in a video with only one audio stream (it didnt bring both audio streams), and the wrong one. So that command was what worked, by selecting the 2nd audio stream, having now only one but correct audio stream.
  • MestreLion
    MestreLion almost 9 years
    @AquariusPower: have you tried simply with -map 0 to get both audio streams? As I said in the answer: "Default stream selection behavior is to select only one stream per stream type"
  • Aquarius Power
    Aquarius Power almost 9 years
    @MestreLion I see now... thx!
  • Arda Xi
    Arda Xi almost 9 years
    It might support the codecs inside, but that doesn't mean it will work in an application that doesn't understand Matroska. This would only work if the application supports Matroska and looks at the magic number instead of the extension.
  • MestreLion
    MestreLion over 8 years
    Renaming the file will not convert the container format, and it might confuse the application (and users). As @ArdaXi said, the application would still need to support Matroska to play it.
  • psychok7
    psychok7 over 7 years
    works very well
  • Seppo Erviälä
    Seppo Erviälä over 7 years
    Use -c:s mov_text option if you need to keep eg. srt subtitle track.
  • chiappa
    chiappa over 4 years
    @camh: FFmpeg removed libfaac support in 2016, instead using native, built-in FFmpeg AAC encoder. "ffmpeg -i input.mkv -c:v copy -c:a aac output.mp4"
  • Jimmy Olano
    Jimmy Olano over 4 years
    My case: I had a .webm audio then first I converted to mp4: follow this question here askubuntu.com/questions/323944/convert-webm-to-other-formats
  • Ravi Anand
    Ravi Anand over 3 years
    ffmpeg does it very straight.