Efficiently Reading ID3v2 Tags Over HTTP?
Paul Crowley asks: "Given an HTTP URL for an MP3 file, what's the best way to read its ID3 tags on a GNU/Linux system? It shouldn't be necessary to fetch the whole file: HTTP byteranges should make it possible to fetch only the tiny fraction that's needed, for a big saving in network bandwidth. However, existing ID3v2 libraries are designed to read local files. Extending these libraries for this purpose, or implementing a new one, would be a big job. What's the clean solution - is FUSE the best way, or is there a simpler way that doesn't require root privs? Can I do it using the existing id3lib binary?"
Why couldn't you save the result of the remote HTTP access to a temporary local file and allow the libraries access to that file?
You'd better be prepared to extend the API with a URL handler...
There's no point adding http:// support without also adding ftp:// URL support. FTP supports range fetching as well.
So you have handlers for http:// URLs, ftp:// URLs, and file:// URLs.
Then you'd have to map all the old (compatibility) file-oriented APIs into the new function handlers for file://. (Or maybe the opposite, map file:// into the old API, leaving the old implementation intact)
THIS THING CAN TURN ON A DIME, MACROSSZERO STYLE ALSO FUCK BETA, ~NYORON
ID3v2 data *IS* at the start of the file, normally takes the first 4kB or so (depends on the padding settings).
Just read chunks of teh end of the file until you start hitting mp3 data. You'll waste some bandwidth, but if you choose appropriately sized chunks, you should be fine.
Photos.
You know from the mp3 spec where the tag will be. Just snag that information and feed it to the preexisting library. If you have to, make a temporary file that meets the bear minimum definition of an mp3 (using the snagged tag info, of course).
Without looking and without knowing, I'm willing to bet there's a Perl module for processing mp3 ID3v2 tags. The whole project can probably be done in Perl in a very small amount of lines.
It seems like it shouldn't be that hard. You just initiate the HTTP transfer and then cancel it as soon as you have as much data as you need.
I haven't actually done it, but speaking as a server operator, when I look through my server logs, you see some hits that end with status code 499, meaning that the transfer was aborted. So you just have the client software you're writing close the HTTP connection after it locates the end of the ID3 tag. It's probably not 100% efficient, but obviously a lot better than reading the whole MP3 file.
I'm assuming you're doing this in C/C++, but I'll try to do a prototype in perl.
The number of checks you have to do is phenominal. The biggest worry is buffer overflow where the length given is greater than the actual length of the tag and you read more than is in the file. There are just hundreds of such edge cases. Libraries for ID3v2 are likely to be buggy, crashy, and just no fun.
From your question, it sounds like you already have figured out how to use http to grab the relevant byte range. I don't know anything about ID3 tags but if they are at the head of the file, then you just need to get the head, if they are at the tail, then just get the tail. Save the relevant byte ranges as files locally on a directory structure that is based on the URL so that http:/mess-o-mp3.com/content/this.mp3 would map to /mess-o-mp3.com/content/this.mp3. If necessary append or prepend dummy mp3 file so that existing ID3v2 libraries that are designed to read local files can read the ID3 tags. Then just run the "existing ID3v2 library" against the file tree that you have just built, and translate the output from describing the contents of the file system to describing the contents of the Internet.
This doesn't see so complicated to me
ID3v2, to which he was refering, has been out for years and is at the beginning of the file.
Yet another signature that refers to itself. The irony and humor is dead.
id3v2 tags are at the beginning of the file. That's one of the reasons id3v2 was developed as an alternative to the id3v1 tags which were put on the end. Stick an http:// url to an mp3 file with id3v2 tags in your xmms playlist and you'll watch the tag info populate in the playlist window right away.
Al Qaeda has ninjas!
The easy way out is probably just to grab the first 10 bytes from HTTP and use your own stripped down library to read the header. These precious 10 bytes tell you a) that is is an id3 tag, and b) how much more has to be downloaded. If you take the size data you can easily see how much of the remaining file you need and dump it into a temp file for parsing.
Yet another signature that refers to itself. The irony and humor is dead.
I thought HTTP1.1 had continue features where you could specify a byterange... just ask for the first X bytes...if you didn't get enough, get another chunk and append.
meh
Ah. I am less aware of v2 than v1, then.
Look at the MP3::Info Perl module, you might recognize the author's handle. It reads (and writes) tag info. It's used by the "jukebox" module Apache::MP3 (sample site) to generate pages with track info.
Basically every web jukebox out there does something like this so I'm sure there's plenty of other code available to work from. The mod_perl way is to put SetHandler perl-script then PerlHandler [name of module] in your httpd.conf file so when a URL request falls within that Location or Directory, the perl module handles returning whatever you want it to return.
So actually, the code isn't worth posting. It's pretty much as I said it. Read until you have a valid ID3 tag, and then break the socket connection.
I just realized that I was answering with something the article poster probably already knows.
As far as how you might be able to use an existing library to extend other libraries, It seems like you should be able to save the first x bytes of http (mp3) data to a local temp file and then have the pre-existing id3 library run over that data. I would think that this doesn't necessarily require root.
Sounds like the RIAA has hired a college intern - They can't afford real programmers any more with all this money they're losing.
2. Check to make sure you have the whole tag. If it's bigger than what you downloaded, download the rest of the tag.
3. Write to a temporary file
4. Run existing libraries and/or tools against temporary file
Here is Netscape's JWZ hilariously sad-but-true rant about the ID3 header format:
CDDB: Feel the Pain
In case you didn't know, the file format that CDDB (and FreeDB) use is complete garbage. In addition to random idiotic crap like it being impossible to unambiguously represent a song title that has a slash in it, it's rocket science to figure out how long a song is supposed to be. I need this info not only to display it in Gronk (my MP3 jukebox software), but also for some error-checking that my CD-ripping scripts do, so that I don't end up with truncated files if there was a crash or a full disk or something.
So get this. CDDB files contain junk like this:
# Track frame offsets:
# 150
# 18265
# 32945
# 49812
# Disc length: 3603 seconds
#
DISCID=...
DTITLE=...
(You'd think that the fact that it's in a comment would mean something, but no: you have to parse both comments and non-comments, begging the question of what they thought "comment" means.)
Those numbers are the starting sectors of each track on the disc. There are 75 sectors per second. So you convert those to seconds by dividing, and then find the length of each track by subtracting each from the previous. Oh, but wait, they don't give you the sector address of the end of the last track: for that one, it's expressed in seconds instead of sectors, for no sensible reason. Still, the info is there, right?
Uh, almost.
It turns out that if the last track on a CD is a data track (an ISO9660 file system) then there is a gap between the last track (the data track) and the second-to-last track (the last audio track.) This gap is exactly 11400 sectors (152 seconds, 2:32.) On some discs, you can actually see this track, it's a differently-shiny ring. Why's it there? I don't know. Why's it that size? I don't know. What if the data track is not the last track on the CD? (Does that even work?) I don't know.
So what this means is, when computing the length that a track should be, you have to subtract 152 seconds from the length of the second-to-last track, only if the last track is a data track.
How do you tell whether the last track is a data track, without having the CD in question physically in your computer? By hoping that the CDDB file contains the words "data track" in the title of that track, I guess. Yeah, that's reliable.
And, just to keep things interesting, it turns out that older versions of grip and cdparanoia didn't skip over this gap when ripping: instead, they would append 152 seconds of silence onto the end of the second-to-last track. So now my script that sanity-checks the lengths of the files has to consider two different lengths to be "right", since I now have CDs that were ripped both ways.
Whee. I love love love supporting standards invented by 12-year-olds.
Of course the reason that I use CDDB files at all in Gronk is because of the mind-blowing worthlessness of ID3 tags (32 character limits on titles, etc.) Yay more standards invented by 12-year-olds. (Please don't even mention ID3v2 or Ogg. I laugh at you, you silly person. Those are universally-unsupported fantasies that simply trade one set of problems for a whole new set of problems.)
And as if CDDB wasn't bad enough, FreeDB has taken the CDDB braindeadness and layered even more braindeadness on top of it: it is truly a thing of wonder.
For example, go ahead and try to ever have the "genre" field be something approaching reality -- oops! The first person who ripped this CD said it was "folk" because that's genre number zero! So fix it and resubmit it to the database? Sorry! You can't ever change the genre of an entry in the database after creation, since the genre dictates what directory the file goes in on their server. And so on.
It's a wonder anything works at all.
cpeterso
You know from the mp3 spec where the tag will be.
If you had any knowledge of ID3x2, you'd be aware that you DON'T know where the tag will be. It can be placed pretty much anywhere within the file.
Vorbis-comments are ASCII only, right?
No. The field names are ACSII only (actually a printable subset minus '=') but the contents of the fields are specified as UTF-8.
The intention was you could put arbitrary binary data in there too, but there's no general mechanism for marking it as anything else. So any non-UTF-8 use would be application specific.
fopen() can open local files & URLs - look at the http:// example:
fopen()
fgets() will read in data from the steam - you can pick how many bytes you want to read in:
fgets()
Dont forget to use fclose() afterwards!
When you get those functions working, it's just a matter of interpreting the content returned. PHP has many useful string functions - many more than ASP does.
These functions are analogous to using a Microsoft.XMLHTTP object:
myXMLHTTPObject.Open "GET", someURL
The PHP and ASP way are both neat ways to read content from [X]HTML pages on other servers (the weather, share prices, etc) - although it might not be 100% ethical!
Hope this helps... I have nothing better to do while I wait for Fedora Core 2 to be delivered. Oh wait, I do... I'm at work reading Slashdot!
The simple choice seem to be "read a range of 0-50k" to see if the data is at the start of the file. If it is then you get lucky and win!
If it isn't then you assume it's at the end, and then ideally you just want to just say "give me the last 50k".
Unfortunately you can't do that as there isn't a notion of negative offsets from the end of a file in HTTP. So in the general case you cannot do better than read the whole thing.
I guess if you have a directory index you can parse the filesize from that and then use that in your range'd request, but that's sucky too.
CDDB is a completely different file format from ID3v2.
Xenu loves you!
Let's all use Ogg Vorbis instead! One of my main arguments for using Ogg is the superior format for metadata. //Blenda
This is an interesting general problem. I'm sorry that so few people seem to have taken the time to understand it before replying. The general approach seems to be "read the first sentence, assume the poster is an idiot, hit reply".
The problems are these:
1) Reading ID3v2 tags on an MP3 file is a complex business. I have no desire to re-implement the libraries that do that, or even to wade deep into the existing codebases, if I can avoid that. And it should be possible to avoid that.
2) Even knowing the size and location of ID3v2 tags is complex. Contrary to popular belief here, those tags can appear at either the beginning or the end of a file, and can be arbitrary size. I already implemented the "fetch some stuff at the beginning and some stuff at the end and feed that to the library" approach, and it sort-of works, but you have to guess the size of the tag. Guess too big, you fetch lots of data unnecessarily. Guess too small, you get breakage or wrong results. By contrast, the libraries that read ID3v2 tags know exactly where and how much to read to glean the appropriate data, and it should be possible to make use of that.
3) I want to read existing data - changing the format of that data is not an option.
So that's why I was suggesting solutions like "FUSE". With FUSE, when the library does a seek and a read, I can arrange for just the relevant portion of the file to be fetched. I don't have to include any knowledge about ID3 in my application - the library does all the work. But the library doesn't have to worry about HTTP byte ranges - FUSE handles that. And the code will always be correct.
The only trouble is that FUSE requires a kernel patch and root privs. The question is, is there a way to do the same trick without those limitations? Or is there a library for reading ID3v2 tags in an object-oriented language that will let me put an efficient back-end for fetching data on request using HTTP byteranges in place of the file?
The best information I've got out of this is that there's a pure-Python implementation of ID3v2 (most implementations appear to be built on top of the C library). This may be hackable to solve my problem.
Those of you who didn't think reading or thinking was necessary before posting - please don't do the next "Ask Slashdot" post the same discourtesy. Thanks.
Xenu loves you!
It can be anywhere from 20bytes up to hundreds of KB. iTunes writes particularly wasteful ID3 tags, which are nearer 4KB.
ID3v2 tags are very interesting, in my opinion :)
No it wouldn't. The tag is at the beginning of the file, so why not just fetch the first 512 bytes or so (more if you expect cover art in the tag) and save it into /tmp/.
If you really -must- download only the tag and not a byte more, then clearly you'd have to (A.) know the offset in each file where the tag ends. This is not possible without storing that in some sort of database. Which won't work if you aren't the person in control of the server. Or (B.) download the file and scan it as you download looking for the end of the tag and when you see it abort the download. Seems more trouble than it's worth to bother using those methods, though.
Was I that unclear?
Yes, it was unclear because you provided too much information.
What you appear to actually want is a generic way to wrap a library that reads a file or stream of some type and be able to feed it from an http stream doing efficent requests, by getting byteranges over http.
The fact that you want ID3 isn't totally relevant, as you want a way to wrap the existing ID3 class to read from http instead of, say, a file. This probably confused a lot of people.
Short answer is that no, I don't think there's any good way to do this for all libraries. Too much varience in what the library does.. If the library can read from stream input you give it directly (instead of from a file descriptor you pass in), then you could possibly fool it that way by writing something that pretends to be a stream but instead is reading from a file.. Could be tricky, but it's doable.
But if you're passing in a file descriptor, then you're looking as faking the file descriptor out, and that's probably kernel level code to do that. I don't think there's anyway to do it in userland.
- Give a man a fire and he's warm for a day, but set him on fire and he's warm for the rest of his life.
Replace "instead is reading from a file" with "instead is reading from http". Sorry.
- Give a man a fire and he's warm for a day, but set him on fire and he's warm for the rest of his life.
This is where you tell us how you expect to find out which sections of the files contain the tag. The "libraries that read ID3v2 tags know exactly where and how much to read" because of the fact that they have access to the whole file! If you want that kind of perfect efficiency, then you'll have to download the whole file!
If you want a solution that will allow you to escape downloading the whole file, just check for ID3 in the first 3 bytes and 3DI @ 10 bytes back from the end. Download a couple K in the appropriate direction (more if you expect more than a minority to have images). Neither means no tag, move on. Then parse the tags with your libraries, catch errors, and try to grab more on the few that will throw errors, in case someone put a huge image or War and Peace in a tag.
It's not that hard, and I'm not sure what sorcerer's magic you expected Ask Slashdot to come up with to help you with this.
I think we can all agree on one thing though. Whatever asshole who decided that a tag at the end of the file is a good idea needs to be smacked in the head.
It sounds like you already know what it is that you need to do, but are looking for implementation ideas.
Couple of thoughts:
LD_PRELOAD might help you to override open, seek, read, etc calls. You can probably do a HEAD on the URL to get the actual size of the MP3, without downloading the entire file and fake stat results from that.
Seek and read can be faked with Byte-Ranges, as you have already indicated.
Problems that I see are convincing the application to open "http://host:port/path" using the filesystem, and not spewing immediately.
I'm thinking that extending the python code that you have is probably going to be the easiest.
Okay. So why the hell do you need FUSE for that? Why can't one just implement this using HTTP partial content requests? These libraries are open source, right? So the submitter obviously just needs to modify the existing libraries to read and seek using HTTP partial content requests instead of filesystem ones. Problem solved.
Yes, this involves doing real work. No, Ask Slashdot rarely does real work for people.
My "sorcerer's magic" comment, by the way, was trying to communicate the idea that even these libraries (that already exist), must either inspect the sections of the file that I mentioned, or parse the whole file. They cannot, by any way other than magic, just ascertain exactly which bytes to read without probing the file like I described. The person to whom I replied seemed (to me) to suggest this was somehow possible.