Rename a bunch off ogg vorbis files according to their meta information

Nearly every software music player I know has a neat button which – when pressed – automatically figures out meta-information from the filename. But what about the other way around?

Today, I happened to have a large ape-file with an according cue-file. After converting and splitting it with cueape.sh, I realized that the cue-file consisted of a bunch of crap that started like this:

PERFORMER "Unknown Artist"
TITLE "Unknown Title"
FILE "CDImage.wav" WAVE
 TRACK 01 AUDIO
 TITLE "Track01"
 PERFORMER "Unknown Artist"
 INDEX 01 00:00:00
 TRACK 02 AUDIO
 TITLE "Track02"
 PERFORMER "Unknown Artist"
 INDEX 01 03:35:30
[...]

Great… while the timestamps are correct, there isn’t a single piece of meta information so I ended up with

$> l
01 - Track01.ogg  04 - Track04.ogg  07 - Track07.ogg  10 - Track10.ogg  13 - Track13.ogg  16 - Track16.ogg  19 - Track19.ogg  22 - Track22.ogg  25 - Track25.ogg
02 - Track02.ogg  05 - Track05.ogg  08 - Track08.ogg  11 - Track11.ogg  14 - Track14.ogg  17 - Track17.ogg  20 - Track20.ogg  23 - Track23.ogg
03 - Track03.ogg  06 - Track06.ogg  09 - Track09.ogg  12 - Track12.ogg  15 - Track15.ogg  18 - Track18.ogg  21 - Track21.ogg  24 - Track24.ogg
$>

Of course, meta information of the created ogg-files was equally as helpful… So I ended up filling out the meatinformation manually. But still, there were these file-names bothering me in not saying anything. Since I already provided meta-information I wanted to use it for renaming these files. A quick and unpatient google search didn’t reveal anything, so I wrote a bash-script. Maybe this one comes in handy for some one of you out there.

#!/bin/bash
#
# rename_ogg.sh
#
# Rename all ogg files in a directory according to their metainformation
#
# v 0.1 - fixed string, $ARTIST - $ALBUM - $TITLE.ogg
#
# use arg -v for producing verbose output
#
# simply invoke this script from the directory containing the
# files to be renamed, e.g.
# ~/path/to/my/oggs$ /usr/local/bin/rename_ogg.sh -v
#

if [ "$1" = "-v" ]
then
 echo "Verbose Output"
 verbose=1
else
 verbose=0
fi;

for current_file in ./*
do
 TITLE=`ogginfo "$current_file" | tr -d "\t" | grep TITLE  | cut -d "=" -f 2`
 ALBUM=`ogginfo "$current_file" | tr -d "\t" | grep ALBUM  | cut -d "=" -f 2`
 ARTIST=`ogginfo "$current_file" | tr -d "\t" | grep ARTIST  | cut -d "=" -f 2`
 new_name="$ARTIST - $ALBUM - $TITLE.ogg"
 if [ $verbose -eq 1 ]
 then
 echo "$current_file --> $new_name"
 fi;

 mv "$current_file" "$new_name"
done;

So, this is what it looks like, when you use it:

$> ls
01 - Track01.ogg  04 - Track04.ogg  07 - Track07.ogg  10 - Track10.ogg  13 - Track13.ogg  16 - Track16.ogg  19 - Track19.ogg  22 - Track22.ogg  25 - Track25.ogg
02 - Track02.ogg  05 - Track05.ogg  08 - Track08.ogg  11 - Track11.ogg  14 - Track14.ogg  17 - Track17.ogg  20 - Track20.ogg  23 - Track23.ogg
03 - Track03.ogg  06 - Track06.ogg  09 - Track09.ogg  12 - Track12.ogg  15 - Track15.ogg  18 - Track18.ogg  21 - Track21.ogg  24 - Track24.ogg
$> ../rename_ogg.sh -v
Verbose Output
./01 - Track01.ogg --> Léo Delibes - Lakmé - Akt 0 - 01 - Prelude.ogg
./02 - Track02.ogg --> Léo Delibes - Lakmé - Akt 1 - 01 - No. 1 Introduction: "À l'heure accoutumée" (Nilakantha).ogg
./03 - Track03.ogg --> Léo Delibes - Lakmé - Akt 1 - 02 - Prière: "Blanche Dourga" (Lakmé, Nilakantha).ogg
./04 - Track04.ogg --> Léo Delibes - Lakmé - Akt 1 - 03 - No. 1 Bis - Scène: "Lakmé, c'est toi qui nous protégeons!" (Nilakantha, Lakmé).ogg
./05 - Track05.ogg --> Léo Delibes - Lakmé - Akt 1 - 04 - No. 2 - Duetto (The Flower Duet): "Viens, Mallika, les lianes en fleurs ... Dôme épais, le jasmin" (Lakmé, Mallika).ogg
./06 - Track06.ogg --> Léo Delibes - Lakmé - Akt 1 - 05 - Scène: "Miss Rose, Miss Ellen" (Gérald).ogg
./07 - Track07.ogg --> Léo Delibes - Lakmé - Akt 1 - 06 - No. 3 - Quintette & couplets: "Quand une femme est si jolie" (Gérald).ogg
./08 - Track08.ogg --> Léo Delibes - Lakmé - Akt 1 - 07 - Récitatif: "Nous commettons un sacrilège" (Gérald).ogg
./09 - Track09.ogg --> Léo Delibes - Lakmé - Akt 1 - 08 - No. 4 - Air: "Prendre le dessin d'un bijou" (Gérald).ogg
./10 - Track10.ogg --> Léo Delibes - Lakmé - Akt 1 - 09 - No. 4 Bis - Scène: "Non! Je ne veux pas toucher" (Gérald, Lakmé).ogg
./11 - Track11.ogg --> Léo Delibes - Lakmé - Akt 1 - 10 - No. 5 - Récitatif & Strophes: "Les fleurs me paraissent plus belles" (Lakmé).ogg
./12 - Track12.ogg --> Léo Delibes - Lakmé - Akt 1 - 11 - No. 5 Bis - Récitatif: "Ah! Mallika! Mallika!" (Lakmé).ogg
./13 - Track13.ogg --> Léo Delibes - Lakmé - Akt 1 - 12 - No. 6 - Duo: "D'où viens-tu? Que veux-tu?" (Lakmé, Gérald).ogg
./14 - Track14.ogg --> Léo Delibes - Lakmé - Akt 1 - 13 - No. 6 Bis - Scène: "Viens! Là! Là!" (Nilakantha, Lakmé).ogg
./15 - Track15.ogg --> Léo Delibes - Lakmé - Akt 2 - 01 - Entr'acte.ogg
./16 - Track16.ogg --> Léo Delibes - Lakmé - Akt 2 - 02 - No. 7 - Choeur & Scène du marche: "Allons, avant que midi sonne".ogg
./17 - Track17.ogg --> Léo Delibes - Lakmé - Akt 2 - 03 - No. 7 Bis - Récitatif: "Enfin! Nous aurons du silence!".ogg
./18 - Track18.ogg --> Léo Delibes - Lakmé - Akt 2 - 04 - No. 8 - Airs de danse: Introduction.ogg
./19 - Track19.ogg --> Léo Delibes - Lakmé - Akt 2 - 05 - No. 8 - Airs de danse: Terana.ogg
./20 - Track20.ogg --> Léo Delibes - Lakmé - Akt 2 - 06 - No. 8 - Airs de danse: Rektah.ogg
./21 - Track21.ogg --> Léo Delibes - Lakmé - Akt 2 - 07 - No. 8 - Airs de danse: Persian.ogg
./22 - Track22.ogg --> Léo Delibes - Lakmé - Akt 2 - 08 - No. 8 - Airs de danse: Coda avec Choeurs.ogg
./23 - Track23.ogg --> Léo Delibes - Lakmé - Akt 2 - 09 - No. 8 - Airs de danse: Sortie.ogg
./24 - Track24.ogg --> Léo Delibes - Lakmé - Akt 2 - 10 - Récitatif: "Voyez donc ce vieillard".ogg
./25 - Track25.ogg --> Léo Delibes - Lakmé - Akt 2 - 11 - No. 9 - Scène & Stances: "Ah! Ce vieillard encore!" (Nilankantha, Lakmé).ogg
$> ls
Léo Delibes - Lakmé - Akt 0 - 01 - Prelude.ogg
Léo Delibes - Lakmé - Akt 1 - 01 - No. 1 Introduction: "À l'heure accoutumée" (Nilakantha).ogg
Léo Delibes - Lakmé - Akt 1 - 02 - Prière: "Blanche Dourga" (Lakmé, Nilakantha).ogg
Léo Delibes - Lakmé - Akt 1 - 03 - No. 1 Bis - Scène: "Lakmé, c'est toi qui nous protégeons!" (Nilakantha, Lakmé).ogg
Léo Delibes - Lakmé - Akt 1 - 04 - No. 2 - Duetto (The Flower Duet): "Viens, Mallika, les lianes en fleurs ... Dôme épais, le jasmin" (Lakmé, Mallika).ogg
Léo Delibes - Lakmé - Akt 1 - 05 - Scène: "Miss Rose, Miss Ellen" (Gérald).ogg
Léo Delibes - Lakmé - Akt 1 - 06 - No. 3 - Quintette & couplets: "Quand une femme est si jolie" (Gérald).ogg
Léo Delibes - Lakmé - Akt 1 - 07 - Récitatif: "Nous commettons un sacrilège" (Gérald).ogg
Léo Delibes - Lakmé - Akt 1 - 08 - No. 4 - Air: "Prendre le dessin d'un bijou" (Gérald).ogg
Léo Delibes - Lakmé - Akt 1 - 09 - No. 4 Bis - Scène: "Non! Je ne veux pas toucher" (Gérald, Lakmé).ogg
Léo Delibes - Lakmé - Akt 1 - 10 - No. 5 - Récitatif & Strophes: "Les fleurs me paraissent plus belles" (Lakmé).ogg
Léo Delibes - Lakmé - Akt 1 - 11 - No. 5 Bis - Récitatif: "Ah! Mallika! Mallika!" (Lakmé).ogg
Léo Delibes - Lakmé - Akt 1 - 12 - No. 6 - Duo: "D'où viens-tu? Que veux-tu?" (Lakmé, Gérald).ogg
Léo Delibes - Lakmé - Akt 1 - 13 - No. 6 Bis - Scène: "Viens! Là! Là!" (Nilakantha, Lakmé).ogg
Léo Delibes - Lakmé - Akt 2 - 01 - Entr'acte.ogg
Léo Delibes - Lakmé - Akt 2 - 02 - No. 7 - Choeur & Scène du marche: "Allons, avant que midi sonne".ogg
Léo Delibes - Lakmé - Akt 2 - 03 - No. 7 Bis - Récitatif: "Enfin! Nous aurons du silence!".ogg
Léo Delibes - Lakmé - Akt 2 - 04 - No. 8 - Airs de danse: Introduction.ogg
Léo Delibes - Lakmé - Akt 2 - 05 - No. 8 - Airs de danse: Terana.ogg
Léo Delibes - Lakmé - Akt 2 - 06 - No. 8 - Airs de danse: Rektah.ogg
Léo Delibes - Lakmé - Akt 2 - 07 - No. 8 - Airs de danse: Persian.ogg
Léo Delibes - Lakmé - Akt 2 - 08 - No. 8 - Airs de danse: Coda avec Choeurs.ogg
Léo Delibes - Lakmé - Akt 2 - 09 - No. 8 - Airs de danse: Sortie.ogg
Léo Delibes - Lakmé - Akt 2 - 10 - Récitatif: "Voyez donc ce vieillard".ogg
Léo Delibes - Lakmé - Akt 2 - 11 - No. 9 - Scène & Stances: "Ah! Ce vieillard encore!" (Nilankantha, Lakmé).ogg
$>

P.S.

Known Bugs:

  • renaming fails if supposed filename contains a /

To Do:

  • catch this error
  • support more file types
  • make resulting file name configurable
Advertisement

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.