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: –

#!/bin/bash

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


#Set output directory to current
outdir="./"

#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
fi

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

#Set output directory to file name without extension
outdir="$1"
outdir=${outdir%.*}
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"
fi

#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
fi 

#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
fi 

infile="$1"

#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
do
"$MKVEXTRACT" tracks "$file" 1:"${file%.*}".wav;

echo "$SCRIPT: Deleting source MKV file"

rm "${file}"
done

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

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

#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
    done
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
    done
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
    done
echo "$SCRIPT: Please enter Genre for tagging"
read genre
    while [ -z "$genre" ]; do
        echo "$SCRIPT: Entry was blank, please type a Genre"
        read genre
    done

#Reset track counter
track=0

#For each flac file, ask user for track name, rename file,
#add track number and add tags using metaflac
for file in *.flac
do
        ((track+=1))
        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
            done
            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"
done

#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.

Leave a Reply

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