Extracting mp3 data

Does anyone know how to get the artist and album data from an mp3 file? I have tried the following to read the last 500 characters of an mp3 file (I read that the ID tag is at the end of the file), but it keeps returning an End Of File Error.

tell application “Finder”
set filename to name of file 11 of disk diskname
set filelength to size of file 11 of disk diskname as integer
set taglength to filelength - 500
set f to open for access file 11 of disk diskname
set mp3data to read f from taglength as string
log filename
log mp3data
close access f
end tell
log filename
log mp3data

Thank you very much if anyone knows this.

Craig Smith

Model: Powermac dual G4
AppleScript: 1.9.3
Browser: Firefox 1.0
Operating System: Mac OS X (10.4)

Hello! Lets see if I get it. First of all, if you really have that mac you say you have :stuck_out_tongue: in your signature, you surely have iTunes, why not get all that info via it? It is scriptable and you can get anything you should need. Second, your mp3 might not have tags in the version you describe. TAG (id3v1) version is stored at the end of the file, as you say, searching the last 500 chars should be more than enough. But your file might have id3v2, so all that info is stored at the very beginning of the file (you’ll find “ID3” and following all the info about). Try:


set MyMp3 to choose file -- select any of your .mp3 files to get its alias.

set x to 200
-- x can be any number you like, and can also be 'eof'

set TheText to read MyMp3 from 1 to x
-- you can also use "read MyMp3 from -x to eof" if you want to check for TAG version.

For more detailed info you could check http://www.id3.org
There you can find anything related to all of this.

Hope it works! Chau!


Nikel, Argentina.

The Mp3 data in my files are exactly the first 500 characters


set the file_to_read to read (choose file with prompt "Pick a file to read:") from 1 to 500

You can change “500” to any number as Nikel suggested

Thank you to both Nikel and sitcom; I will try your suggestions and post my results in the next few days.

Craig

Your suggestions worked perfectly for reading the ID information of my files. Thank you again. Here is some more information about my project:

My music collection is much larger than my iPod. I used iTunes over the course of about 18 months to extract all my CDs to my hard drive. Once I had enough tracks to fill a DVD, I would burn the DVD, erase the library and start all over. I now have 10 DVDs filled with all my MP3 files and would like to have a program to catalog them. So, I am working on a searching loop that will find the final folders where the actual MP3s reside. Under most circumstances, the pathway gives enough data to organize the database for searchability and then finding what I am looking for. In many cases, however, the folder names are simply things like “Greatest Hits” or some such nebulous title, with no artist information.

With the assistance you rendered last time, I am now able to read the ID tags, but as you warned, the ID tags are not all the same. I have a loop that will return the artist name cleanly on about 70% of the tested tracks. I cannot instruct iTunes to read an MP3 from a disk and return any information.

My script for extracting the artist information is below. If you have any other suggestions, I would be grateful to hear them.

set MyMp3 to choose file – select any of your .mp3 files to get its alias.

set x to 500
– x can be any number you like, and can also be ‘eof’

set TheText to read MyMp3 from 1 to x
–set thetextb to read MyMp3 from -x to eof
– you can also use “read MyMp3 from -x to eof” if you want to check for TAG version.
log TheText
–log thetextb
if TheText contains “TPE1” then
set theartist_begin to offset of “TPE1” in TheText
else
set theartist_begin to offset of “TP1” in TheText

end if
if TheText contains “TIT2” then
set theartist_end to offset of “TIT2” in TheText
else
set theartist_end to offset of “TCO” in TheText
end if
set theartist to text from character (theartist_begin + 4) to character (theartist_end - 1) of TheText
log theartist

Craig Smith

Model: Powermac dual G4
AppleScript: 1.9.3
Browser: Firefox 1.0
Operating System: Mac OS X (10.4)

I should have posted this in Code Exchange, but it’s not complete, so it’s not so useful. It’s just to point an idea…

I added a “without invisibles” to the choose file line, it’s was a bit anoying… and I split the search in several modules so that you might reuse the handlers again somewhere else (as thaught in school lol). They are not complete (only two of them work ok) but from them you should be able to get all the others, it’s the same procedure. The only thing it does not cover is the posibility that the next field does not exist… in which case the

(sorry if posted a million times… I’m having huge problems with mi isp… nothing seems to work today)
I should have posted this in Code Exchange, but it’s not complete, so it’s not so useful. It’s just to point an idea…

I added a “without invisibles” to the choose file line, it’s was a bit anoying… and I split the search in several modules so that you might reuse the handlers again somewhere else (as thaught in school lol). They are not complete (only two of them work ok) but from them you should be able to get all the others, it’s the same procedure. The only thing it does not cover is the posibility that the next field does not exist… in which case the ‘repeat’ will search forever… I’m not quite sure how to solve that right now, but if I should find a way I’ll let you know. What I mean is, “TPE1” comes right after “TIT2”… so that’s how I get the track name… but what if the artist name doesn’t exist?? Then “TPE1” will not be in the file and I’ll be searching forever. An artist name is generally there, but it’s not always so with the album, comment, year, or whatever… You could add a few ‘or’ the the ‘repeat’ condition, but they’re going to be a lot.

Hope it helps! (and I’m pretty sure by the time you have it all worked out someone will come and say “Why didn’t you do…” :rolleyes: It’s inevitable…)


set MyMp3 to choose file without invisibles -- select any of your .mp3 files to get its alias.

set x to 500
-- x can be any number you like, and can also be 'eof'

set TheID3Text to read MyMp3 from 1 to 500
set TheTAGText to read MyMp3 from -x to eof
-- you can also use "read MyMp3 from -x to eof" if you want to check for TAG version.

if "ID3" is in TheID3Text then
	set MyMp3Info to SearchID3Version(MyMp3)
else if "TAG" is in TheTAGText then
	set MyMp3Info to SearchTAGVersion(MyMp3)
else
	return "No tag info found."
end if

return MyMp3Info

on SearchID3Version(MyMp3)
	-- set TheID3Text to read MyMp3 from 1 to 500
	set IDVersion to "ID3"
	set theartist to SearchID3Artist(MyMp3)
	set TheAlbum to SearchID3Album(MyMp3)
	set TheTrack to SearchID3Track(MyMp3)
	
	return {IDVersion, theartist, TheAlbum, TheTrack}
end SearchID3Version

on SearchTAGVersion(MyMp3)
	-- set TheTAGText to read MyMp3 from -x to eof
	set IDVersion to "TAG"
	set theartist to SearchTAGArtist(MyMp3)
	set TheAlbum to SearchTAGAlbum(MyMp3)
	set TheTrack to SearchTAGTrack(MyMp3)
	
	return {IDVersion, theartist, TheAlbum, TheTrack}
end SearchTAGVersion

on SearchID3Artist(MyMp3)
	set i to 1
	set TheField to ""
	repeat until TheField is equal to "TPE1"
		set TheField to read MyMp3 from i to (i + 3)
		set i to (i + 1)
	end repeat
	set ArtistStart to (i + 4)
	
	repeat until TheField is equal to "TALB"
		set TheField to read MyMp3 from i to (i + 3)
		set i to (i + 1)
	end repeat
	set ArtistEnd to (i - 2)
	
	set theartist to read MyMp3 from ArtistStart to ArtistEnd
	
	return theartist
end SearchID3Artist

on SearchID3Album(MyMp3)
	return "Album"
end SearchID3Album

on SearchID3Track(MyMp3)
	set i to 1
	set TheField to ""
	repeat until TheField is equal to "TIT2"
		set TheField to read MyMp3 from i to (i + 3)
		set i to (i + 1)
	end repeat
	set TrackStart to (i + 4)
	
	repeat until TheField is equal to "TPE1"
		set TheField to read MyMp3 from i to (i + 3)
		set i to (i + 1)
	end repeat
	set TrackEnd to (i - 2)
	
	set TheTrack to read MyMp3 from TrackStart to TrackEnd
	
	return TheTrack
end SearchID3Track

on SearchTAGArtist(MyMp3)
	return "Artist"
end SearchTAGArtist

on SearchTAGAlbum(MyMp3)
	return "Album"
end SearchTAGAlbum

on SearchTAGTrack(MyMp3)
	return "Track Name"
end SearchTAGTrack

Model: PowerBook G4 17"
AppleScript: 1.9.3
Browser: Safari 1.3 (v312)
Operating System: Mac OS X (10.3.9)

Nikel:

Wow. This is quite a script you put together. I will try it our and dissect it over the next few days. Thank you again; I have been trying some other roundabout ways to do this, but have not met with total success yet.

Craig

Nikel

I spent some time with your script last night, but it seems to get stuck in the SearchID3Artist section and just goes and goes and never seems to find what it is looking for.

I am convinced that there is a simpler method to do this. From the ADVANCED screen in iTunes, you can have the ID tags altered for any selected files, but the script dictionary has no information on how to accesss that part of the program. It would be a simple thing for me to write a script that would upload an entire DVD of my music files. If I could also get the script to switch the ID tags and then re-write to a new DVD; that would be perfect. I am not going to spend any more time messing around with this tag line stuff.

Thank you for all your help, it has been a completely valuable exercise.

Craig

Yep, I knew it would happen… if you read the last (key) lines of my last post you’ll see that I pointed that I had not contemplated the fact that the field that I was searching for to set the end of the one I’m trying to get might not be there… in which case, as you certainly noticed :D, it never stopped…

Take a look at SearchID3Artist() and you’ll see that there are two repeats… the second one searches from “TPE1” until it finds “TALB”… but what if “TALB” doesn’t exist? → Crash!! So I’m sorry I didn’t point that out stronger…

Anyway, if you’re going to use iTunes it’s all much simplier. First, (if possible and useful) I’d suggest you convert all your .mp3’s to the last ID Tag… Select them all in your library, right click and you should see “Convert ID3 Tags”. This way you can be sure that any files you might have (no matter where they came from) will have it’s info in the same format as all the others.
Secondly, if you’re planing to use iTunes via ScriptEditor, you should get by pretty easy. But, here’s some examples…


tell application "iTunes"
	set ThePlaylist to user playlist "Any weird name you might have" -- library playlist "Library" (or whatever it's name is)
	
	repeat with i from 1 to the count of tracks in user playlist ThePlaylist
		set TrackName to name of track i in user playlist ThePlaylist
		set TrackArtist to artist of track i in user playlist ThePlaylist
		set TrackAlbum to album of track i in user playlist ThePlaylist
		
		set the clipboard to TrackArtist & " - " & TrackName & " - " & TrackAlbum -- now you can paste this wherever you want.
	end repeat
end tell

Just remember to add the <in user playlist “xxx”> part! These few lines should cover all you’ve been trying to do lol!! Hope it does the trick!

Nikel:

You are a gem. I should have noticed that myself, and probably would have if I had read everything!! I have been thinking all day, and I believe you are correct. I will just import each DVD and go from there to build my catalog database. I am still going to try to contact Apple about the ID tag and burn options via Applescript.

Craig