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 ?

1 Answers1

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.