Jul 032012
 

So I wrote this script because I had a bunch of files I copied from my ipod touch that were named like ABCD.mp3 and I wanted them to be like ARTIST-SONG_TITLE.mp3 There was also another issue where I had some songs named ARTIST-SONG_TITLE.mp3 but they did not have the id3 information. So this script does both. Also, if the file is an itunes file like ABCD.mp3 and it doesn’t have embedded id3 information, it skips it. Here is the script:

#!/bin/bash
 
# By Ed Wiget
# This script writes mp3 files by the info stored in the mp3
# 20120703 - original script
 
###############################################################################
###############################################################################
# *** Tag information for /home/ewiget/Music/ZZLG.mp3
# === TYER (Year): 2010
# === TRCK (Track number/Position in set): 11
# === TXXX (User defined text information): (Encoder): nos
# === TENC (Encoded by): nos @ [NoFS]
# === TXXX (User defined text information): (nos @ [NoFS]): nos @ [NoFS]
# === TCOP (Copyright message): [NoFS]
# === TXXX (User defined text information): (nos): nos
# === TXXX (User defined text information): ([NoFS]): [NoFS]
# === TPE2 (Band/orchestra/accompaniment): Lil Wayne
# === TIT2 (Title/songname/content description): Knockout 
# === TALB (Album/Movie/Show title): Lil.Wayne-Rebirth-Retail.Deluxe.Edition)-2010-[NoFS]
# === TCON (Content type): Rap/Hip-Hop
# === APIC (Attached picture): ()[, 3]: image/jpeg, 43356 bytes
# === COMM (Comments): ()[eng]: [NoFS]
# === USLT (Unsynchronized lyric/text transcription): ()[eng]: [NoFS]
# === TPE1 (Lead performer(s)/Soloist(s)): Lil' Wayne Ft. Nicki Minaj
# === COMM (Comments): (ID3v1 Comment)[XXX]: [NoFS]
# *** mp3 info
###############################################################################
###############################################################################
 
# requires libid3-dev
if [ "`which id3info | head -1 | wc -l`" = "0" ]; then
	echo "you need to install libid3-dev"
	exit
fi
 
# this handles files with spaces
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
 
# the directory containing mp3 files
MDIR="`ls -1 ~/Music/*.mp3`"
 
for files in ${MDIR}; do
	echo -e "\n\n"
	#echo "we are working on ${files}"
	TITLE="`id3info "${files}" | grep '^=== TIT2' | sed -e 's/.*: //g'`"
	ARTIST="`id3info "${files}" | grep '^=== TPE1' | sed -e 's/.*: //g'`"
	ALBUM="`id3info "${files}" | grep '^=== TALB' | sed -e 's/.*: //g'`"
	YEAR="`id3info "${files}" | grep '^=== TYER' | sed -e 's/.*: //g'`"
	TRACKNUM="`id3info "${files}" | grep '=== TRCK' | sed -e 's/.*: //g'`"
 
		# this is where we decide if the track needs modified or not
		# if the id3 info tag artist field is blank, we can assume title is too
		if [ "${ARTIST}" = "" ]; then
			echo "${files} needs id3 information modified"
			# first we need to make sure its not an itunes style file ABCD.mp3
			TEST=`echo ${files} | awk -F/ '{print $5}' | wc -m`
			if [ "${TEST}" = "9" ]; then
				echo "this is a itunes file...skipping"
			else
				# if its not an itunes filename, we can get the id3 info from the filename
				# this gets artist name and cuts everything after the first dash
				ARTIST=`echo ${files} | awk -F/ '{print $5}' | cut -f1 -d-`
				# this gets the title and cuts any blank space from left to start of words
				TITLE=`echo ${files} | awk -F/ '{print $5}'| cut -f2- -d- | sed -e 's/^[ \t]*//' | cut -f1 -d.`
				# remove echo from below when done testing
				id3tool -t "${TITLE}" -r "${ARTIST}"  "${files}"
			fi
		else
			# we have artist information available in id3 info tag
			# so now we want to make sure the tag info matches the file name
				# we check to see if id3 info matches file name
				if [ "`echo ${files} | awk -F/ '{print $5}'`" = "${ARTIST}-${TITLE}.mp3" ]; then
					# if it matches, we do nothing
					echo "`echo ${files} | awk -F/ '{print $5}'` does equal ${ARTIST}-${TITLE}.mp3" > /dev/null
				else
					# we rename the file to match the id3 tag info
					echo "`echo ${files} | awk -F/ '{print $5}'` does not equal ${ARTIST}-${TITLE}.mp3"
					echo -e "\n\nrenaming file"
					# we need this because somehow a line break got added into this
					FILES2=`echo ${ARTIST}-${TITLE} | tr -d '\n'`
					# and if we also wanted to remove all spaces from the file name, we would do this:
					FILES3=`echo ${FILES2} | sed "s/ *//g"`
					mv "${files}" ~/Music/${FILES2}.mp3
				fi
		fi
 
done
 
# this restores the files with spaces trick
IFS=$SAVEIFS

This site uses Akismet to reduce spam. Learn how your comment data is processed.