Trying to convert MKV to MP4 with avconv, failing

13,610

Solution 1

I believe that

avconv -i file.mkv -c:v copy -c:a copy file.mp4

is the correct command. You need to specify the codec copy property for the streams you want to copy separately.

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.

Share:
13,610

Related videos on Youtube

ColLeslieHapHapablap
Author by

ColLeslieHapHapablap

Updated on September 18, 2022

Comments

  • ColLeslieHapHapablap
    ColLeslieHapHapablap almost 2 years

    I am trying to convert an MKV file to MP4 to be compatible with my PS3. I am using:

    avconv -i file.mkv -c copy file.mp4
    

    It fails and I get the following error:

    [mp4 @ 0x8a17c00] pts < dts in stream 0
    av_interleaved_write_frame(): Invalid argument
    

    The file is h264 video and ac3 audio.

    • Admin
      Admin over 11 years
      This question may give you some pointers. See answer #2
    • Admin
      Admin over 11 years
      That question is where I originally found how to do the conversion. There is no mention of errors though. Thanks anyway.
    • Admin
      Admin over 11 years
      After much trial and error i got it to work using: avconv -i file.mkv -c:v copy -c:a copy file.mp4
  • kanehekili
    kanehekili almost 4 years
    avconf is dead for a very long time. Still, winff is based on ffmpeg , not on avconf. And the question was about avconf (which never did it get quiet right) . ffmpeg is back for long time now...