2

我正在尝试从文件中提取 mp3 标头。这与 ID3 标签不同——mp3 标头是保存有关 MPEG 版本、比特率、频率等信息的地方。

您可以在此处查看 mp3 标头结构的概述:http: //upload.wikimedia.org/wikipedia/commons/0/01/Mp3filestructure.svg

我的问题是,尽管加载了文件并且现在接收到有效的(据我所知)二进制输出,但我没有看到预期的值。mp3 文件的前 12 位应全为 1,用于 mp3 同步字。但是,仅前 8 位我就收到了不同的东西。这对我来说是个问题。

作为旁注,我有一个通过 fopen 附加的有效 mp3 文件

// 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 problem
    }

    // 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]);
    }
4

6 回答 6

2

ID3v2 占据 MP3 文件的第一位(如果存在)。文件的前三个字节将是“ID3”:

http://www.id3.org/id3v2.4.0-结构

有两种处理方法。首先是检查是否存在 ID3 标记,然后解析 10 字节标头的标记大小,并向前跳过那么多字节。

编辑:如果解析标头,您需要检查标志字段中的第 4 位是否设置为 1,如果是,则需要跳过额外的 10 个字节才能越过页脚。

或者您可以只在 MP3 中搜索,直到您达到同步模式。ID3v2的设置方式,不应该出现连续11个一位,以保证兼容不支持的播放器。

于 2009-11-05T20:46:51.133 回答
1
fseek(mp3file,1,SEEK_SET);

您是否有理由跳过文件的第一个字节?

于 2009-11-05T20:24:21.007 回答
1

尝试

fseek(mp3file,0,SEEK_SET)

代替

fseek(mp3file,1,SEEK_SET).

文件从字节位置 0 开始。

于 2009-11-05T20:25:05.887 回答
1

ID3 信息可能会先出现。是前3个字符ID3吗?

于 2009-11-05T20:30:51.680 回答
1

我想你可能想要

fseek(mp3file,0,SEEK_SET);
于 2009-11-05T20:25:11.223 回答
0

fseek(mp3file,1,SEEK_SET);让你跳过前 8 位,所以你用 fread 得到的是第 9 到 16 位

于 2009-11-05T20:26:28.683 回答