MKV to MP4 with mkvtoolnix

Install the gpac tools 


sudo apt-get install gpac

Check that your version of MP4Box supports H.265

MP4Box -h format

 ...
 MPEG-4 Video         .cmp .m4v
 H263 Video           .263 .h263
 AVC/H264 Video       .h264 .h26L .264 .26L .x264 .svc
 HEVC Video          .hevc .h265 .265 .hvc .shvc
 JPEG Images          .jpg .jpeg
 PNG Images           .png
 ...

If not, you're going to need to build. Simply grabbing the most recent libgpac wont do it as it doesn't contain the utilities.



Install mkvtools

Install mkvtools

sudo apt-get install mkvtoolnix

Check that your version supports H.265. Version 6.8 and beyond support this, so you probably don't need to get the newest.

mkvextract --version

It's a good idea to install the latest version though;

https://www.bunkus.org/videotools/mkvtoolnix/downloads.html

Demux The File

Take a look at your streams

mkvinfo test.mkv

...
|+ Segment tracks
| + A track
|  + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
|  + Track UID: 1
|  + Lacing flag: 0
|  + Language: und
|  + Codec ID: V_MPEGH/ISO/HEVC
|  + Track type: video
...
| + A track
|  + Track number: 2 (track ID for mkvmerge & mkvextract: 1)
|  + Track UID: 2
|  + Lacing flag: 0
|  + Name: Surround
|  + Language: eng
|  + Codec ID: A_AAC
|  + Track type: audio
|  + Audio track
|   + Channels: 6
...
 + A track
|  + Track number: 3 (track ID for mkvmerge & mkvextract: 2)
|  + Track UID: 3
|  + Lacing flag: 0
|  + Language: eng
|  + Default flag: 0
|  + Codec ID: S_TEXT/ASS
|  + Track type: subtitles

Extract the tracks according to the types

mkvextract tracks test.mkv 0:test.h265 1:test.aac 2:test.ass

Create a MP4

MP4Box -add test.h265#video -add test.aac -new test.mp4

Bonus - convert the 6 channel to stereo first

avconv -i test.aac -c:a libfdk_aac -ac 2 test2.aac
MP4Box -add test.h265#video -add test2.aac -new test.mp4

Bonus - batch conversion

for X in `ls *.mkv`; do Y=`echo $X | sed 's/.mkv//'`;mkvextract tracks "$X" "0:$Y.h265" "1:$Y.aac" "2:$Y.ass";  done

for X in `ls *.aac`; do Y=`echo $X | sed 's/.aac//'`;avconv -i "$X" -c:a libfdk_aac -ac 2 "$Y.stereo.aac"; done

for X in `ls *.mkv`; do Y=`echo $X | sed 's/.mkv//'`;MP4Box -add "$Y.h265#video" -add "$Y.stereo.aac#audio:lang=en-US" -isma -new "$Y.mp4"; done

For vo


Comments