Ripping BluRay Audio discs to FLAC, part III

Just a quick update to the previous script, I’ve added a user input to select the audio track you want to rip, in the event you want to rip multiple audio tracks.

The workflow is to rip using MakeMKV as before, but now you can select mutliple audio tracks to embed into the output file.

The script will then display the audio tracks available and you can choose the track by it’s number.

If anyone can show me how to parse the output of the section that shows the tracks so that the user input can be validated better (at present it just checks it’s a number, it is posible to input a number outside of the number of tracks available) I’d be grateful for the tutorial and can publish the update here.

#!/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"

#List audio streams from input file
"$MKVMERGE" -i "${infile}" | grep Track

#Ask user for track number to rip
unset get_track

while [[ ! ${get_track} =~ ^[0-9]+$ ]]; do
    echo "$SCRIPT: Enter number of track ID to rip"
    read get_track
done
    
echo "$SCRIPT: You selected track" ${get_track}

#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" $get_track:"${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" 
BluRay_AudioRip.sh

Posted in Uncategorized | Leave a comment

Streaming Subsonic in higher quality at lower bitrates on Vortexbox

My primary music library is in lossless FLAC format, which Subsonic, by default, transcodes to mp3 for streaming to PC’s and mobile devices.

I’ve always used a two-step transcode for this as FFmpeg doesn’t always handle FLAC files as well as other formats, which can result in streaming problems.

Continue reading

Posted in Android, Google, Linux, Music, Streaming | Leave a comment

BBC micro:bit Modified tilt sensor in Python

This is a modified version of the previous tilt sensor application, this one tidies the code a little for readability, adding more comments and adds the ability to adjust the sensitivity.

Enjoy!

from microbit import *

#Program to read accelerometer and show tilt direction

#Sensitivity of readings (lower value to make more sensitive)
#Suggested range 20 - 240
sensitivity = 20

#Create image for when micro:bit is level
centred = Image("00400:"
                "00400:"
                "44944:"
                "00400:"
                "00400")

#Main loop
while True:
    #Get tilt readings for x and y axes
    tilt_x = accelerometer.get_x()
    tilt_y = accelerometer.get_y()
    #Use sensitivity value to adjust size of null window
    #If tilt_x or tilt_y are < sensitivity, register as level
    x_null = -sensitivity < tilt_x < sensitivity
    y_null = -sensitivity < tilt_y < sensitivity
    
	#Allow sensitivity adjustment: button a to increase, button b to decrease
    if button_a.is_pressed() and sensitivity >= 40:
        #Increase sensitivity by 20 (reduce size of null window)
        sensitivity -= 20
        #Pause 100ms so single click doesn't register as multiple presses
        sleep(100)
    elif button_b.is_pressed() and sensitivity <= 220:
        #Decrease sensitivity (increase size of null window)
        sensitivity += 20
        #Pause 100ms so single click doesn't register as multiple presses
        sleep(100)
        
    #Check if micro:bit is tilted
    #If tilted forward and x-axis is within null window point N
    if tilt_y < -sensitivity and x_null:
        display.show(Image.ARROW_N)
	#If tilted forward and right point NE
    elif tilt_x > sensitivity and tilt_y < -sensitivity:
        display.show(Image.ARROW_NE)
    #If tilted right and y-axis is within null window point E
    elif tilt_x > sensitivity and y_null:
        display.show(Image.ARROW_E)
    #If tilted back and to the right point SE
    elif tilt_x > sensitivity and tilt_y >sensitivity:
        display.show(Image.ARROW_SE)
    #If tilted back and x-axis is within null window point S
    elif tilt_y > sensitivity and x_null:
        display.show(Image.ARROW_S)
    #If tilted back and to the left point SW
    elif tilt_x < -sensitivity and tilt_y > sensitivity:
        display.show(Image.ARROW_SW)
    #If tilted left and y-axis is within null window point W
    elif tilt_x < -sensitivity and y_null:
        display.show(Image.ARROW_W)
    #If tilted forward and left point NW
    elif tilt_x < -sensitivity and tilt_y < -sensitivity:
        display.show(Image.ARROW_NW)
    
    else:
        #If level display our custom cross
        display.show(centred)
adjustable_tilt_sensor.py

 

Posted in Uncategorized | Leave a comment