使用来自 taglib 库函数的 C 的音频文件的属性

Properties of an audio file using C from the taglib library functions

我正在尝试使用 C 语言找出音频文件的属性(艺术家姓名、标题、持续时间),这可以通过安装 taglib(特别是 libtagc0-dev).在library中提供的其中一个例子中,给出了如下代码:

#include <stdio.h>
#include <tag_c.h>

#ifndef FALSE
#define FALSE 0
#endif

int main(int argc, char *argv[])
{
  int i;
  int seconds;
  int minutes;
  TagLib_File *file;
  TagLib_Tag *tag;
  const TagLib_AudioProperties *properties;

  taglib_set_strings_unicode(FALSE);

  for(i = 1; i < argc; i++) {
    printf("******************** \"%s\" ********************\n", argv[i]);

    file = taglib_file_new(argv[i]);

    if(file == NULL)
      break;

    tag = taglib_file_tag(file);
    properties = taglib_file_audioproperties(file);

    if(tag != NULL) {
      printf("-- TAG --\n");
      printf("title   - \"%s\"\n", taglib_tag_title(tag));
      printf("artist  - \"%s\"\n", taglib_tag_artist(tag));
      printf("album   - \"%s\"\n", taglib_tag_album(tag));
      printf("year    - \"%i\"\n", taglib_tag_year(tag));
      printf("comment - \"%s\"\n", taglib_tag_comment(tag));
      printf("track   - \"%i\"\n", taglib_tag_track(tag));
      printf("genre   - \"%s\"\n", taglib_tag_genre(tag));
    }

    if(properties != NULL) {
      seconds = taglib_audioproperties_length(properties) % 60;
      minutes = (taglib_audioproperties_length(properties) - seconds) / 60;

      printf("-- AUDIO --\n");
      printf("bitrate     - %i\n", taglib_audioproperties_bitrate(properties));
      printf("sample rate - %i\n", taglib_audioproperties_samplerate(properties));
      printf("channels    - %i\n", taglib_audioproperties_channels(properties));
      printf("length      - %i:%02i\n", minutes, seconds);
    }

    taglib_tag_free_strings();
    taglib_file_free(file);
  }

  return 0;
}

然而,当我编译代码时,出现以下错误:

/tmp/ccz8cuR1.o: In function `main':
tag.c:(.text+0x15): undefined reference to `taglib_set_strings_unicode'
tag.c:(.text+0x69): undefined reference to `taglib_file_new'
tag.c:(.text+0x85): undefined reference to `taglib_file_tag'
tag.c:(.text+0x95): undefined reference to `taglib_file_audioproperties'
tag.c:(.text+0xba): undefined reference to `taglib_tag_title'
tag.c:(.text+0xd8): undefined reference to `taglib_tag_artist'
tag.c:(.text+0xf6): undefined reference to `taglib_tag_album'
tag.c:(.text+0x114): undefined reference to `taglib_tag_year'
tag.c:(.text+0x131): undefined reference to `taglib_tag_comment'
tag.c:(.text+0x14f): undefined reference to `taglib_tag_track'
tag.c:(.text+0x16c): undefined reference to `taglib_tag_genre'
tag.c:(.text+0x195): undefined reference to `taglib_audioproperties_length'
tag.c:(.text+0x1d4): undefined reference to `taglib_audioproperties_length'
tag.c:(.text+0x20c): undefined reference to `taglib_audioproperties_bitrate'
tag.c:(.text+0x229): undefined reference to `taglib_audioproperties_samplerate'
tag.c:(.text+0x246): undefined reference to `taglib_audioproperties_channels'
tag.c:(.text+0x273): undefined reference to `taglib_tag_free_strings'
tag.c:(.text+0x27f): undefined reference to `taglib_file_free'
collect2: error: ld returned 1 exit status

似乎库无法找到上述错误中提到的函数。我无法找到消除这些错误的方法,而且,因为我只是从 reference 中复制并粘贴了代码,所以不应该以任何方式让上述错误持续存在。

任何建议都会有用。

可以先检查 header 文件 'tag_c.h' 的位置。在我的系统中,它的路径是“/usr/include/taglib”因此我不得不将 include header 修改为:

#include <taglib/tag_c.h>

然后使用以下命令将您的代码与标签库链接以编译您的代码:

gcc -Wall tag.c -L/usr/include/taglib -ltag_c