Ripping BluRay Audio discs to FLAC, part II

Here’s an update to the previous script, I have added some additional features and corrected a major issue with the previous approach.

The FLAC files created by the previous script have the wrong timecodes. All files have the length of the original source MKV file, with the track start time being at the chapter start point. This confuses some players, although they worked on my Squeezebox, they didn’t work on a friend’s Naim streamer.

Having reached out to the mkvtoolnix developer I was pointed to this issue: –

Cutting and appending FLAC results in wrong FLAC headers #953

Basically FLAC is far from ideal and this problem can’t be fixed! Bugger 🙂

The solution is to rip the raw LPCM stream from the BluRay without FLAC compression, split the resulting MKV at the chapter points, then encode to FLAC.

Having done a little more research I came across metaflac, which is capable of editing / adding tags to FLAC files via the command line, I’ve modified the previous script accordingly and added additional user prompts for Artist, Album, Year and Genre and then tagged the resulting FLAC file with this and the track names and numbers. For the background read the original article here.

Here’s the revised script: –


SCRIPT=`basename "$0"`
MKVMERGE=`which mkvmerge`
METAFLAC=`which metaflac`
FLAC=`which flac`
MKVEXTRACT=`which mkvextract`
startime=`date +%s`

#Set output directory to current

#Check if arguments given, offer help
if [ "$1" == "" ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
    echo "$SCRIPT: Give MKV to chapter-split as an argument"
    echo "$SCRIPT: e.g.:  title00.mkv"
    echo "$SCRIPT: e.g.:  title01.mkv"
    exit 1

#Check MKV file passed exists
if [ ! -f "$1" ]; then
    echo "$SCRIPT: MKV file not found [$1]"
    exit 1

#Set output directory to file name without extension
echo "$SCRIPT: Output folder is "${outdir}""

#Check if folder already exists, create if not
if [ ! -d "$outdir" ]; then
       echo "$SCRIPT: The output directory ($outdir) does not exist, creating"
       mkdir "$outdir"

#Check mkvtoonix is installed, offer help if not
if [ "$MKVMERGE" == "" ] || [ ! -x "$MKVMERGE" ]; then
    echo "$SCRIPT: 'mkvmerge' seems to be missing"
    echo "$SCRIPT: Is mkvtoolnix Installed?"
    echo "$SCRIPT: sudo apt-get install mkvtoolnix"
    exit 2

#Check if flac is installed, offer help if not
if [ "$METAFLAC" == "" ] || [ ! -x "$METAFLAC" ]; then
    echo "$SCRIPT: 'metaflac' seems to be missing"
    echo "$SCRIPT: Is flac installed?"
    echo "$SCRIPT: sudo apt-get install flac"
    exit 2


#Split MKV file into chapters
echo "$SCRIPT: Splitting video file into chapters"

"$MKVMERGE" --split chapters:all "${infile}" -o "${outdir}/${infile%.mkv}_%02d.mkv"

cd "${outdir}"

#Extract LPCM audio from renamed MKV chapter files

echo "$SCRIPT: Demuxing LPCM audio from MKV files"

for file in *.mkv
"$MKVEXTRACT" tracks "$file" 1:"${file%.*}".wav;

echo "$SCRIPT: Deleting source MKV file"

rm "${file}"

#Convert WAV files to FLAC
for file in *.wav
"$FLAC" --delete-input-file --best "$file";

echo "$SCRIPT: Converting WAV file to FLAC and deleting source file"

#Ask user for metadata for tagging with metaflac
echo "$SCRIPT: Please enter Artist name for tagging"
read artist
    while [ -z "$artist" ]; do
        echo "$SCRIPT: Entry was blank, please type an Artist name"
        read artist
echo "$SCRIPT: Please enter Album name for tagging"
read album
    while [ -z "$album" ]; do
        echo "$SCRIPT: Entry was blank, please type an Album name"
        read album
echo "$SCRIPT: Please enter Album year for tagging"
read year
    while [ -z "$year" ]; do
        echo "$SCRIPT: Entry was blank, please type an Album year"
        read year
echo "$SCRIPT: Please enter Genre for tagging"
read genre
    while [ -z "$genre" ]; do
        echo "$SCRIPT: Entry was blank, please type a Genre"
        read genre

#Reset track counter

#For each flac file, ask user for track name, rename file,
#add track number and add tags using metaflac
for file in *.flac
        echo "$SCRIPT: Current file name: "${file%.*}""
        echo "$SCRIPT: Please enter new name"
        read newname
            while [ -z "$newname" ]; do
                echo "$SCRIPT: Entry was blank, please type a new name"
                read newname
            echo "$SCRIPT: Renaming and tagging file"
            echo "$SCRIPT: $artist - $album - $track - $newname"
            mv "${file}" "$track - ${newname}.flac"
            #Remove all existing tags, just as a precaution, then add new ones
            "$METAFLAC" --remove-all-tags \
            --set-tag="ARTIST=$artist" \
            --set-tag="ALBUM=$album" \
            --set-tag="TITLE=$newname" \
            --set-tag="TRACKNUMBER=$track" \
            --set-tag="DATE=$year" \
            --set-tag="GENRE=$genre" \
            "$track - ${newname}.flac"

#Calculate and display script runtime 
endtime=`date +%s`
runtime=$((endtime - startime))
echo "$SCRIPT: All complete in $runtime seconds"
This entry was posted in Music, Streaming, Uncategorized. Bookmark the permalink.

5 Responses to Ripping BluRay Audio discs to FLAC, part II

  1. blipppy says:

    excellent!! thank you! ripping my XTC Black Sea blu-ray now!

  2. webmaster says:

    Published a minor update today:

  3. Twopaw Tarnished-Silver says:

    Question: I’m really new at this, and I wanted to ask- how do I modify your script so as not to delete the split MKVs (rather than delete them)?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.