gpt4 book ai didi

c - 阅读几个 MP3 文件的标签

转载 作者:太空宇宙 更新时间:2023-11-04 04:27:00 25 4
gpt4 key购买 nike

此代码用于读取一个 mp3 文件的 ID3V1 标签。但是我有一个包含几个 MP3 文件的目录。我想添加一些东西,让我的程序能够读取目录中的所有 MP3 文件。然后我必须将所有 ID3V1 标签导出到一个 CSV 文件中。

我不知道该怎么做,我们将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{

char tag[3];
char title[30];
char artist[30];
char album[30];
char year[4];
char comment[30];
unsigned char genre;

}mp3Info;

int main (int argc, char *argv[])
{
if ( argc != 1 )
{
printf( "Please choose one file: %s <song.mp3> \n", argv[0] );
} type FILE,
file = fopen("song.mp3", "rb");

if (file == NULL) {

printf("I couldn't open: %s for reading.\n");
exit(0);
}

else
{
mp3Info tag_in;

fseek(file, -sizeof(mp3Info), SEEK_END);

if (fread(&tag_in, sizeof(mp3Info), 1, file) != 1)
{
printf("Could not read the tag\n");
exit (0);
}


if (memcmp(tag_in.tag, "TAG", 3) == 0)
{
printf("Title: %.30s\n", tag_in.title);
printf("Artist: %.30s\n", tag_in.artist);
printf("Album: %.30s\n", tag_in.album);
printf("Year: %.4s\n", tag_in.year);

if (tag_in.comment[28] == '\0')
{
printf("Comment: %.28s\n", tag_in.comment);
printf("Track: %d\n", tag_in.comment[29]);
}

else
{
printf("Comment: %.30s\n", tag_in.comment);
}
printf("Genre: %d\n", tag_in.genre);
}

else
{
fprintf(stderr, "The program has failed to get the Tags\n");
return EXIT_FAILURE;
}

fclose(file);
return 0;

}
}
}

最佳答案

为了简短起见,我将只链接相关的答案,然后您可以结合这些答案来解决您的问题。

您应该做的第一件事是列出目录中的所有文件,如下所示How can I get the list of files in a directory using C or C++?

一旦你得到它,你应该遍历你的文件(这也在上一个链接中解释),检查哪些是 MP3 文件。一种简单的方法(虽然不是最好的方法)是检查其扩展名,如此处所示 Getting file extension in C对于您找到的每个 MP3 文件,只需使用您的代码来解析它,而不是使用 printf 将所有内容写入 CSV 文件。

对于CSV文件,格式很简单,用fprintffwrite就可以全部写完。如果您打算使用 Microsoft Excel 读取数据,请不要忘记将 sep=, 放在文件的顶部,否则它不会正确读取,我已经发生过一次(它将没有它在 LibreOffice 上也能正常工作)

关于c - 阅读几个 MP3 文件的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40440047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com