User Tools

Site Tools


technology:linux:mp3_organizer

MP3 Organizer script

I have purchased music from several online sites. There are many different ways for providers to organize the files they give me. Some create directories for the artist and albumnames, then name the files by song name. Personally, I like that way of doing things.

Others sites [quite annoyingly] concatenate all that info together into a long filename. Here's an example:

  artist_name___album_name___song_name___year.mp3

While that format might be visually clear, it becomes infinitely painful to manage hundreds of files in a single directory. So I wrote a simple shell script that parses the filename and rewrites it the artist/album/song.mp3 structure that I prefer. The first time I used this, it processed over 500 filenames correctly. And I modified it later to compare files and skip the copy process if the source and destination files are the same.

This script is a work in progress. Please let me know about any errors or improvements.

#! /bin/bash
 
#  ------------------------------
#  fixmp3.sh
#
#  This script relocates files on disk with long names into more meaningful
#  directory structures.  It can be used with any type of file, but is
#  intended for use with MP3 files.  It works as follows:
#  1. finds all mp3s whose names match a pattern
#  2. pulls out the respective parts of the file name:
#       artist, album, song, and year.
#  3. creates a suitable directory name:
#       /destination/artist/album/
#  4. copies the original file to the new directory using only the songname
#  5. deletes original file.
#       (not enabled, for safety)
#
#  Several lines of output are created as well.  These can be
#  captured by piping the scripts output to a file, like this:
#      fixmp3.sh > mylogfile.txt
#
#  Please send mods or enhancements to me.
#
#  Enjoy!
#  Chris Freyer
#  chris at the freyers dot net
#  June 21, 2007
#  ------------------------------
 
# variable declarations
separator=___
sourcedir=/Users/chris/downloads
destdir=/Music/mp3
counter=0
cnt_added=0
cnt_replaced=0
cnt_skipped=0
 
# main loop
for i in `ls *___*___*___*.mp3`
do
        #extract important elements of filename
        i=`basename $i`
        artist=`echo $i | sed 's/___/=/g' | cut -d = -f 1`
        album=`echo $i | sed 's/___/=/g' | cut -d = -f 2`
        song=`echo $i | sed 's/___/=/g' | cut -d = -f 3`
        year=`echo $i | sed 's/___/=/g' | sed 's/\.mp3//g' |cut -d = -f 4`
 
        #risk: need to check $artist, $album, $song, and $year
        #are not null here.  If so, we need to skip this song because it
        #might have a different filenaming convention
 
        #display info to screen
        echo current name :  $i
        echo new name     :  $destdir/$artist/$album/$song.mp3
 
        #create dir if needed
        if [ ! -e $destdir/$artist/$album ]; then
                mkdir -p $destdir/$artist/$album
        fi
 
        #if destination file exists
        if [ -f $destdir/$artist/$album/$song.mp3  ]; then
                # if files are identical
                #f [ 0 -eq `diff 1 2 | wc -l | awk '{ print $1 }' ` ] ;then
                if [  0 -eq `diff $destdir/$artist/$album/$song.mp3 $sourcedir/$i | wc -l | awk '{ print $1 }' ` ]; then
                        echo Found at destination...skipping $song.mp3
                        cnt_skipped=`expr $cnt_skipped + 1`
                else
                        echo "Overwriting file $destdir/$artist/$album/$song.mp3"
                        cp -r $sourcedir/$i $destdir/$artist/$album/$song.mp3
                        cnt_replaced=`expr $cnt_replaced + 1`
                fi
        else  #copy
                cp $sourcedir/$i $destdir/$artist/$album/$song.mp3
                cnt_added=`expr $cnt_added + 1`
        fi
        #accumulate stats
        counter=`expr $counter + 1`
done
 
echo Skipped     $cnt_skipped
echo Overwritten $cnt_replaced
echo Added       $cnt_added
echo -----------------------------------
echo Total:      $counter
/home/cfreyer/public_html/data/pages/technology/linux/mp3_organizer.txt · Last modified: 2007/07/11 14:40 by Chris Freyer