Jump to content

Tagging mp3's

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
In this tutorial, I'll show you how you can add tags to mp3 files. Mp3 files use ID3 layout to store information about artist, song name, genre, year, album...

To comprehend this tutorial, you'll need a basic understanding of slicing, files and ability to search throu Python documentation. :)

We'll need os module to make our lives easier.

import os


Next, a few variables:
dir - which directory to scan and update, . means directory where script is located
pad - to fill empty space in tags later on
year - use if you want
album - see 'year'
filescount - how many files were tagged
nodash - files not updated

The script is based on a certain syntax of mp3 files, which is

Some artist - Some song.mp3

The script works for files with single dash in, as a separator between artist and title. Now you see why nodash variable is there.


dir = "."

pad = "\x00"*40

year = "2011"

album = ""

filecount = 0

nodash = 0



Now, this is where magic starts. With os.listdir function we'll iterate throu every file in our directory.

for filename in os.listdir(dir):

	if os.path.isfile(filename):


Then we'll split file name (which is artist + title) with os.path.splitext function to get file extension. We'll only tag mp3 files.

name, ext = os.path.splitext(filename)

if ext.lower() == ".mp3":


Next comes a try block, which will prevent from our program to crash if there are more than 1 dashes in filename.
We increment nodash counter so when we're done we can tell how many files we're tagged (if any). print here is optional.

try:

	...

except ValueError:	# no dash in filename

	print "NO DASH    ", filename, "    NO DASH"

	nodash += 1


Inside try block we try to split name (which is filename without extension) to get artist and title. If there are more than 1 dashes, we'll get ValueError
which we catch in except block.
		

artist, title = name.strip().split("-")


Then we create the tag. If you've looked at ID3 layout you could see that tag starts with TAG and then song title, artist, album and year follow.
Since we're not interested in other tags, we'll skip them. Few important things about tags; tag is 128 characters long and is inserted at the end of mp3 file.

So, we take title and artist, strip any leading/trailing whitespace, add pad and take first 30 charaters (ID3 layout spec) to store in our tag.
We do the same with album, add year and take 128 charaters of our tag, filled with pads.

tag = ("TAG" + (title.strip() + pad)[:30] + (artist.strip() + pad)[:30] + (album + pad)[:30] + year + pad)[:128]


All we have to do now is add that tag to our mp3. We open file in binary mode, and then write to that file with inserted tag. Lastly we increment filecount
counter for later on.

f = file(filename, "rb").read()

file(filename, "wb").write(f + tag)

print filename, " updated."

filecount += 1


Now we're done and final summation follows which is optional. Those if's in print are just to make output more fancier.

print "Done, updated %d %s, %d %s missing dash" % (filecount, "files" if filecount > 1 else "file", nodash, "files" if nodash > 1 else "file")

print "Press any key to continue..."

import msvcrt

msvcrt.getch()


Here's code as whole

import os


dir = "."

pad = "\x00"*40

year = "2011"

album = ""

filecount = 0

nodash = 0


for filename in os.listdir(dir):

	if os.path.isfile(filename):

		name, ext = os.path.splitext(filename)

		if ext.lower() == ".mp3":

			try:

				artist, title = name.strip().split("-")

				tag = ("TAG" + (title.strip() + pad)[:30] + (artist.strip() + pad)[:30] + (album + pad)[:30] + year + pad)[:128]

				f = file(filename, "rb").read()

				file(filename, "wb").write(f + tag)

				print filename, " updated."

				filecount += 1

			except ValueError:	# no dash in filename

				print "NO DASH    ", filename, "    NO DASH"

				nodash += 1

		

print "Done, updated %d %s, %d %s missing dash" % (filecount, "files" if filecount > 1 else "file", nodash, "files" if nodash > 1 else "file")

print "Press any key to continue..."

import msvcrt

msvcrt.getch()

Feel free to post any questions, observations, critique, etc.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Very nice looking tutorial to show what Python can really do, thank you FlyingDutchman.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users