1

我想从 mp3 文件中读取 mp3 标签:D 并将其保存到 txt 文件中。但是我的代码不起作用:(我的意思是我在我的 mp3 文件中设置正确位置时遇到了一些问题,请看一下:(为什么它不想工作?)。我必须自己做,没有额外的库。

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>

int getFileSize(const char *filename)
{
    struct stat st;
    if (stat(filename, &st) == 0)
        return st.st_size;
    return -1;
}


int main(int argc, char **argv)
{
    char *infile = "in.mp3", *outfile = "out.txt";
    int infd, bytes_read = 0, buffsize = 255;
    char buffer[255];

                infd = open(infile, O_RDONLY);
                if (infd == -1)
                    return -1;

                int outfd = open(outfile, O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
                if (outfd == -1)
                    return -1;

                    if(lseek(infd, -128, SEEK_END) < 0)
                        return -1;

                for(;;)
                {
                    bytes_read = read(infd, buffer, buffsize);
                    if (bytes_read > 0)
                    {
                        write(outfd, buffer, bytes_read);

                    }
                    else
                    {
                        if (bytes_read == 0)
                        {
                            if (close(infd) < 0)
                                return -1;
                            break;
                        }
                        else if (bytes_read == -1)
                        {
                            break;
                            return -1;
                        }
                    }
                }

    return 0;
}
4

1 回答 1

1

解决此问题的一种方法:

您需要根据您使用的 ID3 版本扫描文件(问题没有指定 Steven 指出的特定版本),找到整个标签或标签头并从那里解码。

对于ID3v2,标头序列为 10 字节,如下所示(来自 ID3v2 规范):

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

我的建议是,在这里查看 ID3v2 的规范。检查第 3.1 章,因为部分工作正在进行背景研究。

对于ID3v1 ,请在此处查看概述规范。解码该信息非常容易,并且完全按照您对问题的评论中的说明进行操作。查看您的代码,这可能是您想要做的(在文件末尾跳转到 128 个字节并从那里开始读取)。

在将解码器扔给它之前,请确保您有一个正确标记的文件并确定您使用的标记版本。

于 2012-10-18T13:09:18.433 回答