tirsdag den 11. november 2014

Converting MKV to m4v for iTunes on Linux

Often when lookign for video, you might find MKV files on the Internet. Getting these into a format usable in iTunes / AppleTV can be a time consuming process, especially if you consider using HandBrake to convert the file.

Often you can use simple tools in Linux to convert the file from an MKV container to an MPEG-4 container, without actually reencoding the video. Often you would just need to ensure that there is a 2-channel AAC sound track.

when you get the MKV file, the first step is to see what is inside the file. For this we use mkvinfo:

mkvinfo myfile.mkv

and you will get track information about the contents:
|+ Segment tracks
| + A track
|  + Track number: 1 (track ID for mkvmerge & mkvextract: 0)
|  + Track UID: 12345
|  + Track type: video
|  + Lacing flag: 0
|  + MinCache: 1
|  + Codec ID: V_MPEG4/ISO/AVC
|  + CodecPrivate, length 42 (h.264 profile: High @L4.1)
|  + Default duration: 41.708ms (23.976 frames/fields per second for a video track)
|  + Language: swe
|  + Name: A Good Movie
|  + Video track
|   + Pixel width: 1920
|   + Pixel height: 816
|   + Display width: 1920
|   + Display height: 816
| + A track
|  + Track number: 4 (track ID for mkvmerge & mkvextract: 3)
|  + Track UID: 16248958973911107725
|  + Track type: audio
|  + Default flag: 0
|  + Codec ID: A_DTS
|  + Default duration: 10.667ms (93.750 frames/fields per second for a video track)
|  + Language: dan
|  + Name: DTS
|  + Audio track
|   + Sampling frequency: 48000
|   + Channels: 6

Here we can see, that it is a movie already encoded in H.264 High Profile Level 4.1. AppleTV 3 only supports High Profile Level 4.0 or lower. Don't worry. My AppleTV 3 played the above stream without any trouble, so it might have been compressed in 4.0 anyway. iPhone 4S / iPad 2 supports High @L4.1. If there is any trouble with the video, or the video is too large (like 5GB/hour), you can recompress using Handbrake from https://handbrake.fr/. It runs on Linux, Windows and Mac, and has good defaults.

We also have to look for the sound track, and here we find a danish soundtrack as track 4 (3 for mkvextract), in DTS format only.

DTS is not supported by AppleTV, so we need to convert that to the needed 2 channel AAC, plus we can convert it to AC3 for better surround. Other videos you find will already have AC3, and you will not need to convert the streams, just extract them.

First we need to extract the tracks we need

mkvextract tracks myfile.mkv 0:video.h264 3:audio.dts

which extracts stream 0 in video.h264 and stream 3 in audio.dts.

Now we need to convert the sound tracks. This is a lossy operation, and it takes time:

avconv -i audio.dts -acodec ac3 -ac 6 -ab 640k audio.ac3 # can be skipped if already ac3
avconv -strict experimental -i audio.dts -dmix_mode 1 -ac 2 -b:a 160k -strict experimental audio.aac

The second line should use Dolby Surround mode for the downmix.

If you found chapter markers you would like to keep, you can extract them like this:
mkvextract chapters -s myfile.mkv >chapters.txt

Now that we have all the streams we need, we can assemble (called mux) them into an MPEG-4 container.

This is done by MP4Box. Please use the FPS from the mkvinfo output, as MP4Box defaults to 25 FPS, and this would give video/audio out of sync.

MP4Box -fps 23.976 -add video.h264 -add "audio.aac:lang=dan" -add "audio.ac3:lang=dan" -chap "chapters.txt" myfile.m4v

If you want to add a subtitle for use with Apple devices, just add them like this (the hdlr option is important):

MP4Box -add myfile.srt:lang=eng:hdlr="sbtl:tx3g" myfile.m4v

And now you are done. Remember, with MP4Box you can always add missing tracks later.

If you have an MKV and wants to add subtitles to that, you need to create a new file with the tracks added, so it does not really merge:

mkvmerge  -o Subbed-Movie.mkv Unsubbed-movie.mkv --sub-charset 0:UTF8  --language 0:dan --track-name 0:Dansk Mysubtitles-utf8.srt

Ingen kommentarer:

Send en kommentar