为什么 libFLAC 的 FLAC__metadata_get_tags 函数不是确定性的?

发布于 2025-01-13 20:22:20 字数 957 浏览 1 评论 0原文

在涉足 libFLAC 时,我尝试使用此 C 程序使用 FLAC__metadata_get_tags 函数:

#include <stdio.h>
#include <FLAC/metadata.h>

int main() {
    FLAC__StreamMetadata* metas;
    if (FLAC__metadata_get_tags("/tmp/test.flac", &metas) && metas != NULL) {
        for (int i = 0;; i++) {
            printf("type: %d, length: %d\n", (metas+i)->type, (metas+i)->length);
            if ((metas+i)->is_last) {
                break;
            }
        }
        FLAC__metadata_object_delete(metas);
    }
}

多次运行此程序时,我会得到不同的输出。类型 0 (FLAC__METADATA_TYPE_STREAMINFO) 的元数据条目数量似乎有所不同。有时我什至得到未定义的类型和负长度。

这是为什么?我希望程序总是给出相同的输出。我也不确定长度为 0 甚至负长度的元数据条目应该意味着什么。

下面是一个执行示例:

$ clang -I/usr/local/include -L/usr/local/lib -lFLAC -logg test.c && ./a.out
type: 4, length: 213
type: 0, length: 0
type: 0, length: 0
type: 11, length: 1083592000
type: -538976289, length: 0

While dabbling with libFLAC I tried out the FLAC__metadata_get_tags function with this C program:

#include <stdio.h>
#include <FLAC/metadata.h>

int main() {
    FLAC__StreamMetadata* metas;
    if (FLAC__metadata_get_tags("/tmp/test.flac", &metas) && metas != NULL) {
        for (int i = 0;; i++) {
            printf("type: %d, length: %d\n", (metas+i)->type, (metas+i)->length);
            if ((metas+i)->is_last) {
                break;
            }
        }
        FLAC__metadata_object_delete(metas);
    }
}

When running this program multiple times, I get different outputs. There seems to be a varying number of metadata entries with type 0 (FLAC__METADATA_TYPE_STREAMINFO). Sometimes I even get undefined types and negative lenghts.

Why is that? I would expect the program to always give the same output. I'm also not sure what a metadata entry with length 0 or even a negative length is supposed to mean.

Here is one example execution:

$ clang -I/usr/local/include -L/usr/local/lib -lFLAC -logg test.c && ./a.out
type: 4, length: 213
type: 0, length: 0
type: 0, length: 0
type: 11, length: 1083592000
type: -538976289, length: 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-01-20 20:22:21

FLAC__StreamMetadata 不是元数据列表,它是一个元数据,并且只填充一个 TYPE_VORBIS_COMMENT

https://github.com/xiph/flac/blob/27c615706cedd252a206dd77e3910dfa395dcc49/src/libFLAC/metadata_iterators.c#L217

我找到了如何迭代它的代码,来自 https://isrc.iscas.ac.cn/gitlab/mirrors/github.com/musicplayerdaemon_mpd/-/blob/6419cc54ac200b217d99ee35d1e2d57dbfda3f55/src/decoder/flac_plugin.c

#include <stdio.h>
#include <FLAC/metadata.h>

int main() {
    FLAC__StreamMetadata* metas;
    if (FLAC__metadata_get_tags("./test.flac", &metas) && metas != NULL) {
        for (unsigned i = 0; i < metas->data.vorbis_comment.num_comments; i++) {
            char *ptr;
            if ((ptr = (char*)metas->data.vorbis_comment.comments[i].entry) != NULL) {
                printf("%s\n", ptr);
            }
        }
        FLAC__metadata_object_delete(metas);
    }
}

在从 < 下载的 .flac 上一个href="https://helpguide.sony.net/high-res/sample1/v1/en/index.html" rel="nofollow noreferrer">https://helpguide.sony.net/high-res/sample1/ v1/en/index.html 输出:

ALBUM=Bee Moved
TITLE=Bee Moved
ALBUMARTIST=Blue Monday FM
MRAT=0
ARTIST=Blue Monday FM

总的来说,当有疑问时 - 当项目是开源的时,检查源代码。

FLAC__StreamMetadata is not a list of metadata, it's one metadata, and only one TYPE_VORBIS_COMMENT is filled.

https://github.com/xiph/flac/blob/27c615706cedd252a206dd77e3910dfa395dcc49/src/libFLAC/metadata_iterators.c#L217

I found code how to iterate over it, from https://isrc.iscas.ac.cn/gitlab/mirrors/github.com/musicplayerdaemon_mpd/-/blob/6419cc54ac200b217d99ee35d1e2d57dbfda3f55/src/decoder/flac_plugin.c :

#include <stdio.h>
#include <FLAC/metadata.h>

int main() {
    FLAC__StreamMetadata* metas;
    if (FLAC__metadata_get_tags("./test.flac", &metas) && metas != NULL) {
        for (unsigned i = 0; i < metas->data.vorbis_comment.num_comments; i++) {
            char *ptr;
            if ((ptr = (char*)metas->data.vorbis_comment.comments[i].entry) != NULL) {
                printf("%s\n", ptr);
            }
        }
        FLAC__metadata_object_delete(metas);
    }
}

On the .flac downloaded from https://helpguide.sony.net/high-res/sample1/v1/en/index.html outputs:

ALBUM=Bee Moved
TITLE=Bee Moved
ALBUMARTIST=Blue Monday FM
MRAT=0
ARTIST=Blue Monday FM

Overall, when in doubt - when the project is open-source, check sources.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文