BLOG

Extracting Surround Music Discs

Remko Tronçon ·

I own a couple of multi-channel Surround Sound music albums on DVD, Blu-Ray, and SACD discs. I wanted to extract them all to a hard drive to avoid the nuisance of putting discs in slow (and often stammering) CD/DVD/Blu-Ray players with unfriendly navigation menus, and wanted to have a backup of the audio in case the discs go bad. Each of the different formats requires different tools to convert them to a playable format, so it took me some time to figure out how to convert each one of them. I wrote up a summary of the tools and processes for future reference.

I use a Mac, so the tools described are focused on OS X. Most of them should work on Windows as well, although there may be easier to use tools on Windows.

MKV vs. MP4

I want to be able to stream the extracted albums on my second generation Apple TV. This limits the usable formats to basically only MP4 with a Dolby Digital (a.k.a. AC3) surround audio track. For albums that come in this format (AC3), the MP4 file can be used for both streaming and backup. However, many of the albums I have come in different (higher quality) formats such as DTS or L-PCM; in this case, I want to keep the album in the original format for backup. Since it can handle most media formats, I use Matroska (.mkv/.mka) as the container for the original audio stream(s), archive this for backup, and convert it to a separate MP4 copy for streaming to my Apple TV.

For simplicity, I also use MKV as intermediate format for the different steps of extracting discs below. Sometimes, you can combine the different steps into a single step, and skip the MKV completely, but I’ll leave this out.

Step 1: Extracting Discs to MKV

Extracting Blu-Ray Discs to MKV

Blu-Ray is becoming the most popular format to release multi-channel audio discs. This is also the easiest format to convert, although you will (obviously) need a Blu-Ray drive (I use an external Samsung SE-506CB). Extracting the media streams is simple with MakeMKV, a graphical program that extracts titles of Blu-Rays directly into an MKV (without any media format conversion). At the moment of writing, MakeMKV is in a free beta period, but this will probably soon be over.

Albums are typically contained in a single track on the Blu-Ray. In case the album is split over multiple tracks, convert each one of them to MKV; the next step explains how to combine them.

Extracting DVD-Video Discs to MKV

Before extracting audio from a DVD-Video, double-check that the disc doesn’t have a DVD-Audio section (in the AUDIO_TS folder); in that case, it probably contains a higher quality version of the album, and the tools described below won’t find it (as they don’t handle DVD-Audio), so skip to the next section.

Extracting audio from a DVD-Video disc can be done in the same way as Blu-Ray, using MakeMKV to create an MKV file. Alternatively, you can use the free Handbrake to convert the DVD to an MKV, but be sure to create a Passthrough stream in the Audio settings, so no conversion of the original stream happens (which would result in quality loss).

Extracting DVD-Audio Discs to MKV

DVD-Audio are DVD discs with data in the AUDIO_TS folder (instead of just the VIDEO_TS folder). Unfortunately, neither MakeMKV nor HandBrake detect the audio from DVD-Audio discs, so you have to resort to another tool to get to the data. I could only find DVD Audio Extractor, which is not free, but has a free trial period. Using this tool, you can extract the multi-channel audio into separate files per track (FLAC in case the format is a lossless format, or the original format in case it is a lossy format such as DTS or Dolby Digital (AC3)). You can then convert each separate track into an MKV file for further processing:

find out -name '*.flac' -exec ffmpeg -i {} -c copy {}.mkv \;

(-c copy makes sure that the media is copied bit by bit, without conversion)

Extracting Super Audio CD Discs to MKV

SACD discs are weird, and very annoying to extract.

The hardest part is creating an image from the SACD, since it’s very hard to find a player that can extract audio in addition to just playing it. If you’re lucky enough to have the right version of a PlayStation 3, you can use SACD Ripper to create an ISO image of the entire disc.

Once you have an ISO image, you can use the sacd_extract tool from SACD Ripper to convert the multi-channel audio in the ISO image into separate DSF files, one per track:

sacd_extract -m -s -i album.iso album/

DSF is one of the few formats that cannot be embedded in an MKV file (probably because of the different way they represent sound), so they have to be converted to a different audio format first. DSF files are encoded at a very high sample rate, but only with a single bit of depth; conversion to PCM-based formats seems to be best done by dividing the sample rate by a multiple of 8.

So, the first step is to figure out the sample rate of the original DSF file. You can use FFmpeg’s ffprobe tool to do this.

Once you have the sample rate of the DSF file (e.g. 352800 KHz), you can find a multiple of 8 that yields an acceptable sample rate (e.g. 352800 / 8 = 44100 Khz). You can then use ffmpeg to convert the DSF files to FLAC (a lossless format that can be embedded in MKV), by specifying the target sample rate as the -ar parameter:

find out -name '*.dsf' -exec ffmpeg -i {} -c:a flac -ar 44100 {}.mkv \;

Step 2: Combining MKV files

Usually, when extracting from DVD or Blu-Ray, the entire album is in a single title with chapter markers. In that case, you can skip this step. Sometimes, though, every song gets a title of its own. In this case, you will need to combine the different MKV files into a single file.

There are a few ways to combine MKV files, 2 of which are the following:

  • The graphical way, using the MKVToolNix GUI: in the Merge tab, add the first file, and right click on it to select Append files to add more files.

  • The manual way, using FFmpeg: Create a file files.txt, with the following contents

    file 'file01.mkv'
    file 'file02.mkv'
    file 'file03.mkv'
    

    and combine them with the following command:

    ffmpeg -f concat -i files.txt -c copy all_files.mkv
    

What’s still missing in the resulting file are chapter markers for each song, so we can skip between songs. Although MKVToolNix allows you to create a chapter file and add it to your MKV, there’s no automatic way to determine the offsets: you have to look at the duration of the individual tracks, and enter the chapters in the GUI, which is a slow and annoying process. Instead, I wrote a small to_metadata.rb script around FFmpeg’s ffprobe command to detect the length of the individual files, and dump this information into an ffmetadata format text file that FFmpeg can use to create chapter markers:

to_metadata.rb file*.mkv > metadata.txt
ffmpeg -i all_files.mkv -i metadata.txt -c copy -map_chapters 1 all_files_with_chapters.mkv

The result is an exact copy of the initial file, only with chapter markers at the right times.

Step 3: Adding chapter titles

The current MKV file has the entire albums with chapter markers before each song. To navigate between the different songs of the album more easily, it is convenient to add the song titles as the chapter names.

This can be done in multiple ways as well:

To do it manually, extract the chapters and metadata using ffmpeg:

ffmpeg -i file_with_chapters.mkv -f ffmetadata metadata.txt

This creates a file metadata.txt with a section for each chapter:

...
[CHAPTER]
TIMEBASE=1/1000000000
START=218554000000
END=505053000000
...

Add a title field to each chapter section, and save the file:

...
[CHAPTER]
TIMEBASE=1/1000000000
START=218554000000
END=505053000000
title=My Song
...

Then, merge the metadata with the original MKV file into a new file:

ffmpeg -i file_with_chapters.mkv -i metadata.txt -c copy -map_chapters 1 -map_metadata 1 file_with_chapters_and_titles.mkv

Step 4: Adding static video

At this point, you should have an MKV file with either only an audio stream, or with both an audio stream and a video stream from the original disc (in case your source was a DVD or Blu-Ray disc). For compatibility with most devices, you can add a simple video stream to your MKV file (or replace the existing one). You can either add a static image (e.g. of the album cover), or just a black screen (to save power in case you’re listening to albums on your TV).

First, you need to figure out the duration and index of your audio stream in the MKV file. You can use FFmpeg’s ffprobe on your file, and look for the following line:

...
Duration: 00:26:45.24
  ...
  Stream #0:1: Audio: ac3 (ac-3 / 0x332D6361), 48000 Hz, 5.1(side), fltp, 448 kb/s (default)
  ...

In this case, the 0:1 says that my audio stream is at index 1 (the second stream, 0 included) of file 0 (the first (and only) file I passed on the command line). In case your MKV only has an audio stream, the audio will be at index 0.

When you know the index and stream duration:

  • To add a video with a static image to your MKV file, run

    ffmpeg -t <stream_duration> -loop 1 -i cover.jpg -i file_with_chapters_and_titles.mkv -c:a copy -c:v libx264 -pix_fmt yuv420p -map 0:0 -map 1:<index_of_audio_stream> file_with_video.mkv
    

    where stream_duration and index_of_audio_stream are replaced. E.g., for the example above:

    ffmpeg -t 00:26:45.24 -loop 1 -i cover.jpg -i file_with_chapters_and_titles.mkv -c:a copy -c:v libx264 -pix_fmt yuv420p -map 0:0 -map 1:1 file_with_video.mkv
    
  • To add a blank video to your MKV file, run

    ffmpeg -t <stream_duration> -s 352x288 -f rawvideo -r 25 -pix_fmt rgb24 -i /dev/zero -i file_with_chapters_and_titles -c:a copy -c:v libx264 -pix_fmt yuv420p -map 0:0 -map 1:<index_of_audio_stream> file_with_video.mkv
    

Step 5: Converting to a storage-friendly format

At this point, you should have an MKV file in the original audio format, with all the required metadata to make it convenient to play. Most of the time, this is what you want to save as backup. In the case the album is encoded in uncompressed L-PCM format (which ffprobe or ffmpeg can tell you), you can save some space by converting the stream to FLAC:

ffmpeg -i file_with_video.mkv -c:v copy -c:a flac optimized_file_with_video.mkv

For compressed formats such as Dolby Digital (AC3) or DTS, FLAC will take more space, and it doesn’t make sense to do this conversion; for DTS-HD Master Audio, the file size will approximately be the same, so I don’t convert it either (although it wouldn’t hurt).

Step 6: Converting to an Apple TV format

As mentioned before, since my Apple TV doesn’t support media formats such as DTS, FLAC or MKV containers, I always create a separate MP4 copy with an AC3 audio stream for streaming to my Apple TV.

If the MKV file already contains a Dolby Digital (AC3) stream (which ffprobe or ffmpeg can tell you), you can directly copy both the video and the audio stream into an MP4 file:

ffmpeg -i file_with_video.mkv -c copy file_with_video.mp4

(if there are multiple streams in the input file, use -map to select the right stream).

If the MKV file doesn’t contain an AC3 stream, you can transcode the audio:

ffmpeg -i file_with_video.mkv -c:v copy -c:a ac3 -ar 48000 -ab 600k file_with_video.mp4

(you can choose a different bitrate with -ab; the Apple TV only supports a 48Khz sample rate, though)

Step 7: Adding a cover image

If you want, you can add a cover image to your resulting file, which some media players support in their file listing.

For an MKV file, you can either use the Header Editor of the MKVToolNix GUI to add a cover image as an attachment, or use FFmpeg to do the same:

ffmpeg -i file_with_video.mkv -c copy -attach cover.jpg -metadata:s:t mimetype=image/jpeg file_with_cover.mkv

For maximum compatibility, the file should be named cover.jpg.

For an MP4 file, you can add the cover image from within iTunes, or use tools such as AtomicParsley or mp4box to do the same:

AtomicParsley file_with_video.mp4 --artwork cover.jpg
mp4box -itags cover=cover.jpg file_with_video.mp4