English 中文(简体)
Reading MP3 audio data, or calculating the checksum thereof
原标题:

Is there a Ruby library that will allow me to either calculate the checksum of an MP3 file s audio data (minus the metadata) or allow me to read in an MP3 s audio data to calculate the checksum myself?


I m looking for something like this:

mp3 = Mp3Lib::MP3.new( /path/to/song.mp3 )
mp3.audio.sha1sum # => the sha1 checksum of _only_ the audio, minus the metadata

I found Mp3Info, but it seems a bit tedious. When initializing an Mp3Info object, you can get the frames where the actual audio data begins and ends.

最佳回答

Isn t the ID3 tag stored either at the end of the file (ID3 v1) in a 128-byte block, or in a block at the start of the file (ID3v2.3 and v2.4) ? (id3.org)

You could use the audio_content method from Mp3Info, and read that much data from the file, though it s probably not much more complicated to have a look in the file yourself and work out where the headers aren t.

问题回答

Extracting the mp3file without it s metadata is fairly easy done by yourself.

ID3v1

The metadata are the last 128 bytes of the file. The metadata always begins with the 3 bytes "TAG" if it exists. Just ignore this last 128bytes.

ID3v2

The metadata can be stored at the beginning or the end of the file. Most implemantations only support the beginning. ID3v2 has a header where the size is stored. The header is always loacted at the beginning of the metadata. There is an optional footer, which is a copy of the header at the end of the metadata. If the metadata is at the end of the file, the footer is required.

The header has the folloing form

ID3v2/file identifier      "ID3"
ID3v2 version              $04 00
ID3v2 flags                %abcd0000
ID3v2 size             4 * %0xxxxxxx

The footer has the following form

ID3v2/file identifier      "3DI"
ID3v2 version              $04 00
ID3v2 flags                %abcd0000
ID3v2 size             4 * %0xxxxxxx

The d bit says, wheter the footer is present. The size is measured without header and footer. Every byte of the size has always the highest bit set. So only 28 of the acutal 32 bits represent the size.

Just compute, which part of the file is not the metadata, and use it for your hashing.

Be aware, if both ID3v1 and ID3v2 are located at the end of the file, ID3v1 is located behind IDv2

The spec can be found at http://www.id3.org/id3v2.4.0-structure.





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签