开发者

Accessing binary MP3 Header in C via fopen

I am trying to extract the mp3 header from a file. This is different then the ID3 tags -- the mp3 header is where information about the MPEG version, bit rate, frequency, etc is held.

You can see an overview of the mp3 header structure here: http://upload.wikimedia.org/wikipedia/commons/0/01/Mp3filestructure.svg

My problem is, despite loading the file and now receiving valid (as far as I know) binary output, I am not seeing the expected values. The first 12 bits of the mp3 file should be all ones, for the mp3 sync word. However, I am receiving something different for the first 8 bits alone. This would suggest a problem to me.

As a side note, I have a valid mp3 file being attached via fopen

// Main function
int main (void)
{
    // Declare variables
    FILE *mp3file;
    char requestedFile[255] = "";
    unsigned long fileLength;

    // Counters
    int i;

    // Tryout
    unsigned char byte; // Read from file
    unsigned char mask = 1; // Bit mask
    unsigned char bits[8];

    // Memory allocation with malloc
    // Ignore this at the moment! Will be used in the future
    //mp3syncword=(unsigned int *)malloc(20000);

    // Let's get the name of the file thats requested
    strcpy(requestedFile,"testmp3.mp3"); // lets hardcode this into here for now

    // Open the file
    mp3file = fopen(requestedFile, "rb"); // open the requested file with mode read, binary
    if (!mp3file){
        printf("Not found!"); // if we can't find the file, notify the user of the probl开发者_如何学运维em
    }

    // Let's get some header data from the file
    fseek(mp3file,0,SEEK_SET);
    fread(&byte,sizeof(byte),1,mp3file);

    // Extract the bits
    for (int i = 0; i < sizeof(bits); i++) {
        bits[i] = (byte >> i) & mask;
    }

    // For debug purposes, lets print the received data
    for (int i = 0; i < sizeof(bits); i++) {
        printf("Bit: %d\n",bits[i]);
    }


ID3v2 occupies the first bit of the MP3 file, if it is there. The first three bytes of the file will be "ID3":

http://www.id3.org/id3v2.4.0-structure

There's two methods of dealing with it. The first is checking for the presence of the ID3 tag, and then parse the 10 byte header for the tag size, and skip ahead that many bytes.

EDIT: If parsing the header, you need to check for the 4th bit in the Flags field to be set to one, if it is, you need to skip an additional 10 bytes to go past the footer.

Or you can just seek through the MP3 until you hit the sync pattern. The way ID3v2 is set up, 11 one bits in a row should not occur, to ensure compatibility with players that do not support it.


The ID3 info may come first. Are the first 3 characters ID3?


fseek(mp3file,1,SEEK_SET);

Is there a reason you're skipping the first byte of the file?


Try

fseek(mp3file,0,SEEK_SET)

instead of

fseek(mp3file,1,SEEK_SET).

Files start at byte position 0.


I think you probably want

fseek(mp3file,0,SEEK_SET);


fseek(mp3file,1,SEEK_SET); makes you skip the first 8 bits, so what you get with fread are bits 9 to 16


This approach worked for me. But I had to remove the int i; under //counter

That might be what you need to do. All the bits printed 1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜