Sorting a music library by BPM

1

I'm using a tool called bpm-tag which takes an mp3 file ("myfile.mp3") as input and outputs "myfile.mp3: XX.XXX BPM". I'd like to run a script which goes through my music library, calculates the BPM of each song and moves it to a directory according to its BPM (e.g directory "Slow" for <80 BPM, etc.). I have a vague idea how to do it but I do not know how to parse the output of bpm-tag to get the value of the BPM.

Any suggestions ?

Adrien MORIN

Posted 2018-04-03T19:53:42.210

Reputation: 41

4SU is not a script writing service - but a number of us are willing and able to help if you get stuck writing your own. What do you have so far? If you want to get the "BPM" value for a file it would seem that you can run BPM=$( bpm-tag myfile.mp3 | cut -f2 -d" " ) which would put the BPM into a variable $BPM – davidgo – 2018-04-03T20:21:33.243

2I did not know about sed and cut until you mentioned them ; thank you. I have figured out how to write my script, should I include it as answer ? – Adrien MORIN – 2018-04-03T23:16:37.363

It certainly would be helpful if you did. =) – Anaksunaman – 2018-04-04T01:44:55.523

Answers

2

Here's what I've done. It seemed to work (but sadly bpm-tag was not accurate enough for lots of songs...).

#!/bin/bash

cd /path/to/my/library

while IFS= read -r -d '' FILE; do
    BPM=$(bpm-tag -f -n "$FILE" 2>&1 | sed "s/.mp3:/%/" | cut -d'%' -f2 | sed "s/ BPM//" | sed "s/^ //" | cut -d'.' -f1) 
#bpm-tag has its output in stderr, so I use 2>&1 to redirect it to stdout, then format it with sed and cut
    if [ "$BPM" -le 130 ]
        then cp "$FILE" /path/to/my/library/Slow/
    elif [ "$BPM" -le 180 ]
        then cp "$FILE" /path/to/my/library/Medium/
    else cp "$FILE" /path/to/my/library/Fast/
    fi
done < <(find . -type f -name '*.mp3' -print0)

Here doing

while IFS= read -r -d '' FILE; do
    echo "$FILE"
done < <(find . -type f -name '*.mp3' -print0)

prints all the files (-type f) ending in .mp3 (-name '*.mp3') that are in the folder or one of its subfolders. As I understand it, the -print0 and -r -d '' options are for formatting purposes, but I don't really get how it works.

Adrien MORIN

Posted 2018-04-03T19:53:42.210

Reputation: 41