Extract Blu-ray audio with FFmpeg

From Gentoo Wiki
(Redirected from FFmpeg - Extract Blu-Ray Audio)
Jump to:navigation Jump to:search

This article covers extracting Blu-Ray audio with FFmpeg.

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.

Important
Copying copyrighted material which you have purchased or material to which 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 when tinker with encrypted Blu-Rays, such as blacklisting. ;-)
Note
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.
Note
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!

Installation

USE flags

USE flags for media-video/ffmpeg Complete solution to record/convert/stream audio and video. Includes libavcodec

+bzip2 Enable bzip2 compression support
+dav1d Enables AV1 decoding via media-libs/dav1d.
+encode Add support for encoding of audio or video files
+gnutls Prefer net-libs/gnutls as SSL/TLS provider (ineffective with USE=-ssl)
+gpl Build GPL code. Should be enabled unless you require LGPL binaries.
+iconv Enable support for the iconv character set conversion library
+network Enables network streaming support
+pic Force shared libraries to be built as PIC (this is slower)
+postproc Build and install libpostproc.
+threads Add threads support for various packages. Usually pthreads
+zlib Add support for zlib compression
X Add support for X11
alsa Add support for media-libs/alsa-lib (Advanced Linux Sound Architecture)
amf Enables support for AMD's Advanced Media Framework (AMF)
amr Enables Adaptive Multi-Rate Audio support
amrenc Enables Adaptive Multi-Rate Audio encoding support with media-libs/vo-amrwbenc.
appkit Enables Apple AppKit framework
bluray Enable playback of Blu-ray filesystems
bs2b Enables media-libs/libbs2b based Bauer stereo-to-binaural filter.
cdio Enables audio CD grabbing with dev-libs/libcdio.
chromaprint Enables audio fingerprinting support with media-libs/chromaprint.
chromium Builds libffmpeg.so to enable media playback in Chromium-based browsers like Opera and Vivaldi.
codec2 Enables codec2 low bit rate speech codec support via media-libs/codec2.
cpudetection Enables runtime CPU detection (useful for bindist, compatibility on other CPUs)
cuda Enables CUDA-based acceleration. Mostly used for specific filters.
debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
fdk Use external fdk-aac library for AAC encoding
flite Adds a text-to-speech filter based on app-accessibility/flite.
fontconfig Support for configuring and customizing font access via media-libs/fontconfig
frei0r Enable frei0r wrapping in libavfilter
fribidi Enables fribidi support in the drawtext filter.
gcrypt Enables gcrypt support: Needed for rtmp(t)e support if openssl, librtmp or gmp is not used.
glslang Use dev-util/glslang to compile GLSL
gme Enables support for media-libs/game-music-emu for playing various video game music formats.
gmp Add support for dev-libs/gmp (GNU MP library)
gsm Add support for the gsm lossy speech compression codec
hardcoded-tables Use pre-calculated tables rather than calculating them on the fly. It results in an increase of approximately 15% in the size of libavcodec, the main library impacted by this change. It enables savings in table generation time, done once at codec initialization, since by hardcoding the tables, they do not need to be computed at runtime. However, the savings are often negligible (~100k cycles is a typical number) especially when amortized over the entire encoding/decoding operation. Improvements are being made to the runtime initialization, and so over time, this option will have an impact on fewer and fewer codecs.
iec61883 Support for FireWire DV/HDV input device using media-libs/libiec61883.
ieee1394 Enable FireWire/iLink IEEE1394 support (dv, camera, ...)
jack Add support for the JACK Audio Connection Kit
jpeg2k Support for JPEG 2000, a wavelet-based image compression format
jpegxl Add JPEG XL image support
kvazaar Enables media-libs/kvazaar based HEVC encoder.
ladspa Enable the ability to support ladspa plugins
lcms Enable ICC profile support via media-libs/lcms.
libaom Enables media-libs/libaom based AV1 codec support.
libaribb24 Enables ARIB text and caption decoding via media-libs/aribb24.
libass SRT/SSA/ASS (SubRip / SubStation Alpha) subtitle support
libcaca Add support for colored ASCII-art graphics
libdrm Enables x11-libs/libdrm support for better screen grabbing and hardware accelerated codecs.
libdvdnav Uses media-libs/libdvdnav to enable DVD demuxing
libdvdread Uses media-libs/libdvdread to enable DVD demuxing
libilbc Enables iLBC de/encoding via media-libs/libilbc.
liblc3 Enables LC3 de/encoding via media-sound/liblc3.
libplacebo Adds flexible GPU-accelerated processing filter based on media-libs/libplacebo.
librtmp Enables Real Time Messaging Protocol using librtmp (media-video/rtmpdump) in addition to FFmpeg's native implementation.
libsoxr Enables audio resampling through media-libs/soxr.
libtesseract Enables the OCR filter via app-text/tesseract.
libv4l Uses media-libs/libv4l for video4linux instead of direct calls. Adds support for more devices via the userspace library.
libxml2 Uses dev-libs/libxml2 to enable dash demuxing support.
lv2 Enables lv2 audio filter wrapper.
lzma Support for LZMA compression algorithm
mipsdspr1 Enables MIPS DSP ASE R1 optimizations.
mipsdspr2 Enables MIPS DSP ASE R2 optimizations.
mipsfpu Enables floating point MIPS optimizations.
mmal Enables Multi-Media Abstraction Layer (MMAL) decoding support: Available e.g. on the Raspberry Pi.
modplug Add libmodplug support for playing SoundTracker-style music files
mp3 Add support for reading mp3 files
nvenc Add support for NVIDIA Encoder/Decoder (NVENC/NVDEC) API for hardware accelerated encoding and decoding on NVIDIA cards (requires x11-drivers/nvidia-drivers)
openal Add support for the Open Audio Library
opencl Enable OpenCL support (computation on GPU)
opengl Add support for OpenGL (3D graphics)
openh264 Enables H.264 encoding suppoprt via media-libs/openh264.
openssl Enables dev-libs/openssl support. Adds support for encrypted network protocols (TLS/HTTPS).
opus Enable Opus audio codec support
oss Add support for OSS (Open Sound System)
pulseaudio Add sound server support via media-libs/libpulse (may be PulseAudio or PipeWire)
qrcode Enables QR encode generation via media-gfx/qrencode
qsv Enable Intel Quick Sync Video via media-libs/intel-mediasdk (ffmpeg versions older than, and including, 5.1) or media-libs/libvpl (ffmpeg version newer than 5.1).
quirc Enables QR decoding via media-libs/quirc
rav1e Enables AV1 encoding support via media-video/rav1e.
rubberband Adds time-stretching and pitch-shifting audio filter based on media-libs/rubberband.
samba Add support for SAMBA (Windows File and Printer sharing)
sdl Add support for Simple Direct Layer (media library)
shaderc Use media-libs/shaderc to compile GLSL
snappy Enable app-arch/snappy support. Required for e.g. Vidvox Hap encoder.
sndio Enable support for the media-sound/sndio backend
soc Apply additional patches for efficient playback on some SoCs (e.g. ARM, RISC-V).
speex Add support for the speex audio codec (used for speech)
srt Enable support for Secure Reliable Transport (SRT) via net-libs/srt
ssh Enable SSH/sftp support via net-libs/libssh.
static-libs Build static versions of dynamic libraries as well
svg Add support for SVG (Scalable Vector Graphics)
svt-av1 Enables AV1 encoding support via media-libs/svt-av1.
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
theora Add support for the Theora Video Compression Codec
truetype Enables drawtext filter via media-libs/freetype and media-libs/harfbuzz.
twolame Enables MP2 encoding via media-sound/twolame as an alternative to the internal encoder.
v4l Enable support for video4linux (using linux-headers or userspace libv4l libraries)
vaapi Enable Video Acceleration API for hardware decoding
vdpau Enable the Video Decode and Presentation API for Unix acceleration interface
verify-sig Verify upstream signatures on distfiles
vidstab Enables video stabilization filter using vid.stab library (media-libs/vidstab).
vmaf Enables Netflix's perceptual video quality assessment filter using the library (media-libs/libvmaf).
vorbis Add support for the OggVorbis audio codec
vpx Enables VP8 and VP9 codec support using libvpx: Decoding does not require this to be enabled but libvpx can also be used for decoding; encoding requires this useflag to be enabled though.
vulkan Add support for 3D graphics and computing via the Vulkan cross-platform API
webp Add support for the WebP image format
x264 Enable h264 encoding using x264
x265 Enables HEVC encoding with media-libs/x265.
xvid Add support for xvid.org's open-source mpeg-4 codec
zeromq Enables net-libs/zeromq support with the zmq/azmq filters.
zimg Enables media-libs/zimg based scale filter.
zvbi Enables media-libs/zvbi based teletext decoder.

Emerge

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 beginning chapter and, not recognizing or stopping at the specified end chapter. ie.

user $mplayer -ao pcm:fast:file=audio.wav -chapter 2-2 -vo null -vc null input_file

Guide

Mount Blu-Ray disc

Blu-Rays use UDF filesystem format, and require to be mounted as such. Probably best to edit the following file to provide mount points as such. AutoFS can be incorporated 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 defined previously in the /etc/fstab example file:

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

Mount the disc:

root #mount /mnt/dvd-udf

Find available stream types

The main large media stream file on the Blu-Ray disk, is typically something similar to ./BDMV/STREAM/0000.m2ts.

When using ffplay, something similar should be seen 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 the streams have been extracted 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, it will be likely necessary to specify ac=hwdts to MPlayer for passing through DTS to the HDMI/SPDIF audio receiver. MPlayer uses the following for specifying streams:

user $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 aforementioned conversion provides 24 bit PCM Riff/Aiff files, while the below ffmpeg incantation 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!

Note
If this section applies to you, then you will need to augment the further FFmpeg incantations 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)

user $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
Note
When using MakeMKV, make sure to 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 incantation 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.

Tips

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 24-bit decoding is 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

References

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