I'm going to try and extract the artist and album from the meta data of an mp3 file with python but I need a little help.

This is what I get on the tag line when I open an mp3 file in vim:
----> ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿTAGRooster^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@Alice In Chains^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@DIRT^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@1992

This is what I get when I tail the same mp3 file
---->
������������������������������������������������������������������TAGRoosterAlice In ChainsDIRT1992

I need to know the structure of the file but when I tail the file ir runs everything together, when I open it in vim the artist and the album each of up to 30 characters and thereamining characters are padded with ^@

Any ideas how the file is actually laid out?

Recommended Answers

All 5 Replies

Why not search for tools that can read the metadata directly and use that API? If not, look at the MP3 file format and write a program that will parse that. Any open source tool that plays MP3s will have the logic to extract the information you seek - you can look directly at that code for inspiration.

Opening a binary file in an editor is not going to be very helpful (as you've discovered).

I don't want to use an API because that would take all the work out of it. Looking at existing OSS code is over my head, I tried to take a look at the code for rhythmbox once, it was a sea of confusion.

You could try installing and using the Tagpy library to tag the mp3 files.

Tagpy should be available in the repos of most distros - I think the package is called python-tagpy on Debian/Ubuntu based distros. Not sure what it's called on others.

Below is what I have so far. I'm getting the TAG which is what I'm looking for but I don't understand why it has b'' wrapped around it. It's coming back like this b'TAG'

#!/usr/bin/env python3

import sys

file = sys.argv[1]
f = open(file, 'rb')

f.seek(-128, 2)

char = f.read(3)
    print(char)


f.close()

b is a byte string - I'm sure I mentioned these in one of your other python threads...I think it was one of your socket related threads.
Yup, here.

As long as you are happy using python 2 (tagpy isn't available for python 3 atm), the tagpy library will make your life a lot easier, you can just use the following syntax:

import tagpy

# open the file and read the tag
f = tagpy.FileRef("yourfile.mp3")
t = f.tag()

# print the existing tag info
print(t.artist)
print(t.title)
print(t.album)
print(t.year)

# change the info
t.artist = "Tone Deaf and the Eh?"
t.title = "Blah blah blah"
t.album = "You what?"
t.year = 2014

#print the new info
print(t.artist)
print(t.title)
print(t.album)
print(t.year)

# save the file with the new tag info
f.save()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.