【问题标题】:Read Tags for few MP3 files阅读几个 MP3 文件的标签
【发布时间】:2016-11-05 15:38:26
【问题描述】:

此代码用于读取一个 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;

}
}
}

【问题讨论】:

    标签: c


    【解决方案1】:

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

    您应该做的第一件事是列出目录中的所有文件,如下所示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 上也可以正常工作)

    【讨论】:

    • 这可以工作,但是当我这样做时出现问题: fprintf("this is an example") 我的变量“this is an example”因为空格被分为 4 种情况,我该如何解决是吗?
    • 嗯,不,这不应该发生,正如第 4 页第 2 点 Spaces are considered part of a field and should not be ignored. 中的 RFC4180's document 中所述。它还指出,行尾不能有逗号。这意味着如果您运行以下代码:fprintf(file, "field1,field2 with spaces, field3\n"); 它应该可以正常工作。不要忘记最后的换行符。
    • 正如我在回答中所说,如果您使用 Excel,请不要忘记在第一行添加 sep=,,这意味着我的完整示例将是这样的 fprintf(file, "sep=,\nfield1,field2 with spaces, field3\n");
    猜你喜欢
    • 1970-01-01
    • 2011-09-12
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2011-05-02
    相关资源
    最近更新 更多