FFmpeg - Extract Blu-Ray Audio

From Gentoo Wiki
Jump to:navigation Jump to:search

Some music audio only titles are just becoming available on Blu-Ray, and music lovers may need to extract the audio to another portable medium. Since the Blu-Ray audio is usually one big file, the file chapters need to be found and split. Most portable media requires VFAT filesystem preventing larger files sizes, and further splitting is essential.

This article will only discuss unencrypted Blu-Ray Audio media, and merrily point users to MakeMKV (media-video/makemkv) for handling encrypted Blu-Ray media.

Copying copy righted material not purchased by you or material you do not hold a license is a crime. Some Countries may be more strict, and consider simply tampering with encrypted material a crime. Funny things may also occur if you tinker with encrypted Blu-Rays, such as blacklisting. ;-)
Audio ranges greater than 24 bits or 480000 Hz are frequently stated as being indistinguishable to the human ear. As for me, I notice little difference between 16 and 24 bit, or 44100 and 48000 Hz of noisy music.
Encrypted (or commercial) Blu-ray media is extremely difficult to enjoy within Linux, as MPlayer's libaacs rarely works when playing encrypted Blu-ray media. Also anything higher than the common 16 bit 44100 Hz (CD Audio Quality), and you will require a Home Theater or 5.1/7.1 Dolby/DTS Audio Receiver with more than five quality speakers to decode and be able to notice a difference above CD audio quality. Receivers also have the ability to copy stereo two channel into five or more channel audio, sounding extremely similar to 5.1 PCM. If you're an older person and dislike having to always buy additional equipment to be able to enjoy these newer proprietary formats, it has been quite common when considering Blu-ray media, to continue using only the tried and well tested CD and DVD media!

Install Required Packages

Although MPlayer can also be used, FFmpeg seems more refined when dumping or clipping specific audio chapters from DVD or Blu-Ray media.

root #emerge --ask media-video/ffmpeg

(If somebody successfully uses MPlayer/MPlayer2 to dump PCM specified chapters, feel free to add it to this Wiki page and retitle appropriately. I've only experienced MPlayer seeking to the beginnning chapter and, not recognizing or stopping at the specified end chapter. ie. "mplayer -ao pcm:fast:file=audio.wav -chapter 2-2 -vo null -vc null input_file")

Mount Blu-Ray Disc

Blu-Rays use UDF, and require to be mounted as such. Probably best to edit the following file to provide mount points as such. (I use AutoFS, so incorporate as needed.)

FILE /etc/fstab
/dev/sr0       /mnt/dvd        iso9660         noauto,user,ro  0 0
/dev/sr0       /mnt/dvd-udf    udf             noauto,user,rw  0 0

Or the following will automatically decide with little to no additional access time difference,

FILE /etc/fstab
/dev/sr0       /mnt/dvd        auto            noauto,user,ro  0 0

Create the mount folders if you don't have them already,

root #mkdir /mnt/dvd /mnt/dvd-udf

Mount the disc,

user $sudo mount /mnt/dvd-udf

Find Available Stream Types

You've likely found your main large media stream file on your Blu-Ray, something similar to ./BDMV/STREAM/0000.m2ts.

Using ffplay, you'll likely see something like this within stdout,

user $ffplay ...
Stream #0:0[0x1011]: Video: h264 (High) (HDMV / 0x564D4448), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x1100]: Audio: pcm_bluray (HDMV / 0x564D4448), 48000 Hz, stereo, s32, 2304 kb/s
Stream #0:2[0x1101]: Audio: pcm_bluray (HDMV / 0x564D4448), 48000 Hz, 5.1(side), s32, 6912 kb/s
Stream #0:3[0x1102]: Audio: dts (DTS-HD MA) ([134][0][0][0] / 0x0086), 48000 Hz, 5.1(side), s16, 1536 kb/s
Stream #0 on this audio only Blu-Ray is only a black screen with song titles.  We'll skip this stream since we want audio only PCM WAV
Stream #1 is the PCM two channel stereo mix.
Stream #2 is the PCM 5.1 high resolution mix.
Stream #2 is the DTS mix.

Keep an eye on the Hz, s16/s24/s32 and kb/s, as they're indicators of audio quality.

Extract Audio Streams

Extract Full Audio Streams

To extract the three individual stream types into one large file, you can use FFmpeg. (Although this is likely undesirable due to file size limitations on VFAT filesystems.)

user $ffmpeg -i ./BDMV/STREAM/00000.m2ts -map 0:1 -acodec pcm_s24le music.wav
user $ffmpeg -i ./BDMV/STREAM/00000.m2ts -map 0:2 -acodec pcm_s24le music-pcm51.wav
user $ffmpeg -i ./BDMV/STREAM/00000.m2ts -map 0:3 -acodec copy music.dts

Verify you have successfully extracted the streams using ffplay or mplayer. Monitor the stdout messages to ensure proper drivers and codecs are used for the stream types specified.

(For DTS playback using MPlayer, you'll likely need to specify ac=hwdts for MPlayer for passing through DTS to your HDMI/SPDIF audio receiver. MPlayer uses the following for specifying streams, "mplayer -aid 1 -demuxer lavf ./BDMV/STREAM/00000.m2ts".)

Devices with only 16 Bit Microsoft PCM Audio Support

Some audio receivers and devices will only play 16 bit Microsoft PCM WAV files! If you have 24 bit audio files as indicated above and such hindered devices, you will need to unfortunately down mix in order for the files to be playable on those devices. The above formentioned conversion provides 24 bit PCM Riff/Aiff files, while the below ffmpeg incanatation will provide 16 bit Microsoft PCM WAV files.

user $ffmpeg -i ./BDMV/STREAM/00000.m2ts -map 0:1 -acodec pcm_s16le music.wav

Another work around, is to play the 24bit PCM WAV files using a software media player such as FFplay and MPlayer, and route the sound to your audio receiver using HDMI or S/PDIF. One other option, ensure you buy a receiver capable of playing the 24 bit PCM files via USB media!

If this section applies to you, then you will need to augment the further FFmpeg incanatations below with the "-acodec pcm_s16le" option.

Extract Individual Chapters

Find Chapters

1) The package media-video/bluray_info will display chapters.

user $bluray_info -x /dev/sr0

2) FFprobe provided by media-video/ffmpeg package will display the chapters to stdout, if they are preserved within the media file.

user $ffprobe ./music.mkv

More elaborate or useful examples using ffprobe,

user $ffprobe -i 00.mkv -print_format default -show_chapters -loglevel error > 00.mkv.txt 2>&1
user $ffprobe -i 00.mkv -print_format flat -show_chapters -loglevel error > 00.mkv.txt 2>&1
If you use MakeMKV, make sure you extract to a format preserving Chapters with using "makemkvcon mkv". Using "makemkvcon backup" does not preserve chapter information as of this writing!

For extracting streams including chapter info for use with ffprobe:

user $makemkvcon -r mkv disc:0 all ./foo_folder/

(MPlayer can also identify chapters using "mindentify", however the chapter times do not appear comptabile with FFmpeg.)

Extract a Chapter

At this point, we'll assume we want Stream #1 for standard two stereo PCM WAV files (ie. map 0:1) and the second (#0.2) chapter.

FFprobe's snipped output:

user $ffprobe ...
Chapter #0.2: start 534.934400, end 888.087200

The incanatation of FFmpeg we'll use for exacting this individual chapter, using seconds for start and duration indicators.

user $ffmpeg -ss [start] -i in.dts -t [duration] -c:v copy -c:a copy out.wav

With this example, the start time will be 534.934400 and duration will be 888.087200 minus 534.934400.

For example,

user $ffmpeg -ss 534.934400 -i ./BDMV/STREAM/00000.m2ts -t 353.152800 -c:v copy -c:a copy out.wav
Extract Multiple Chapters

I have only piped the message stdout of the CLI tools to a series of text files, utilizing grep and bc (CLI Calculator), along side VI/VIM for line duplication and clipping for creating one time scripts for extracting multiple files at once.

Someday, this will likely be automated and integrated into abcde.sh.


Cover Art

Cover art is usually found within the /mnt/dvd/BDMV/META/DL folder. For example:

user $cp /mnt/dvd/BDMV/META/DL/discinfo_640x360.jpg ${HOME}/Music/My_Album/cover.jpg

MPlayer Upmix When 24bit Decoding Not Available

My receiver is apparently not capable of decoding 24 bit PCM WAV, but will decode 16 and 32 bit PCM WAV through HDMI.

The PCM 5.1 WAV files are encoded at 24 bit PCM 5.1 WAV 48000 Hz.

The work around here is to upmix to 32 bit using sb32le or floatle, since MPlayer by default down mixes to 16 bit or s16le. MPlayer also by default cuts channels to two channels.

user $mplayer -af format=s32le,channels=8 PCM51-24bit/01.my_music_track.dts
user $mplayer2 -af format=s32le,channels=8 PCM51-24bit/01.my_music_track.dts

No DTS-H Master?

My receiver shows it's decoding DTS-HD Master stream when bit perfect or high definition audio decoding is selected within my Window's player, but my receiver only says it's decoding the usual "DTS" decoding while playing streams within Linux. From reports on the web, bit perfect or high definition streaming to the receiver isn't possible within Linux. Other reports state it is possible using Intel's HDMI. (NVidia's video card HDMI using Linux binary drivers isn't performing DTS-HD Master here.)

Gapless Playback

Split tracks of long streams, it's nice to have gapless playback for preventing interruptions between tracks.

FIXME: The following is from Snipplr, but doesn't work for me. :-/

user $mkfifo /tmp/aufifo
user $aplay -t raw -c 2 -f S16_LE -r 44100 /tmp/aufifo &> /tmp/aplayfifo.log &
user $mplayer -ao pcm:nowaveheader:file=/tmp/aufifo 01.track.wav 02.track.wav 03.track.wav &

Or use MPlayer2:

user $mplayer2 -ac hwdts -af channels=8 -ao alsa:device=hw=1.3 -gapless-audio DTS/*.dts

Additional Tools

Additional tools which might be useful, but not utilized within this Wiki:

  • media-sound/shntool - A multi-purpose WAVE data processing and reporting utility, ie. splitting WAV files.
  • MPlayer - Media Player for Linux, as an option to FFmpeg
  • media-video/tsmuxer - Utility to create and demux TS and M2TS files


Properly configure ALSA for pass-through digital audio, including specifying default decoding codecs for hardware digital decoders when using MPlayer.