Extracting Audio Tracks From MKV Files

I’ve been ripping all my DVDs to MKV files, so I can serve them up via XBMC running on a Raspberry Pi. I was up in the loft at the weekend, mainly trying to sort out the mess into something more logical. I suddenly remembered that I had a number of music DVDs that I bought with the express intention of ripping the audio from, so I dug them out.

My normal DVD ripping process is to use MakeMKV to get a raw MKV file and then use Handbrake to re-encode the contents it into something slightly smaller. I want to be able to listen to these DVD tracks on a mobile player as well as at work, so I don’t just want them as MKV files with video and audio, I want a separate audio file as well. One of the features of Handbrake, is the ability to add more audio tracks with different encodings etc, so it’s really easy to have your DD or DTS passed through and have it re-encoded into FLAC, Vorbis or MP3 on a separate track.

Armed with a MKV file that contains a FLAC/Vorbis/MP3 audio track, the only thing left is to extract this track into a separate file. If you are using Linux or Windows, then there are a couple of command line utilities that make doing this quite easy, they’re part of the MKVToolNix package, which you should be able to get out of your distributions repositories or download from here. I had four DVDs to extract audio tracks from, one of the DVDs had two different live concerts, so you can imagine there was a fair few files to extract audio from once each song had been ripped to its own MKV file.

It would have taken a while to manually extract the audio track from each MKV file, so I needed a way to script it, so it could all be done automatically. Firstly, I needed to find out the track number that the audio was on, I used mkvmerge for this:

~/scrap/music/mkv/Franz Ferdinand/Live at Brixton$ mkvmerge -i 08\ -\ I\'m\ Your\ Villain.mkv
File '08 - I'm Your Villain.mkv': container: Matroska
Track ID 1: video (V_MPEG4/ISO/AVC)
Track ID 2: audio (A_AC3)
Track ID 3: audio (A_FLAC)
view raw mkvmerge hosted with ❤ by GitHub

All the MKV files I’d created had the FLAC audio track on ID 3, which made life slightly easier, so then it was just a case of finding all MKV files in the current folder tree and extracting track three from them. I used mkvextract in the following script:

#!/bin/bash
find . -type f -name *.mkv | while read filename; do mkvextract tracks "$filename" 3:"${filename%.*}".flac; done
view raw extract-flac.sh hosted with ❤ by GitHub

It would be cool to call the script with the output format you want to extract (A_FLAC in this instance) and parse the output of mkvmerge for each MKV file to find the desired track id to pass to mkvextract. Doing it this way, with a fixed track id, was all a bit quick and dirty, but it worked for me. Maybe I’ll modifying it in the future, but as I’ve no more music DVDs needing audio ripped from them, I’m in no rush…