使用 libFLAC++ 将 vorbis 注释元数据(标签)写入现有 FLAC 文件

发布于 2024-10-07 14:53:01 字数 551 浏览 2 评论 0原文

如何使用 libFLAC++ (http:// /flac.sourceforge.net)?

例如:

const char* fileName = "/path/to/file.flac";

// read tags from the file
FLAC::Metadata::VorbisComment vorbisComment;
FLAC::Metadata::get_tags(fileName, vorbisComment);

// make some changes
vorbisComment.append_comment("TITLE", "This is title");

// write changes back to the file ...
// :-( there is no API for that, i.e. something like this:
// FLAC::Metadata::write_tags(fileName, vorbisComment);

How to write vorbis comments metadata, i.e. tags (for example "TITLE"), to the existing FLAC file using libFLAC++ (http://flac.sourceforge.net)?

For example:

const char* fileName = "/path/to/file.flac";

// read tags from the file
FLAC::Metadata::VorbisComment vorbisComment;
FLAC::Metadata::get_tags(fileName, vorbisComment);

// make some changes
vorbisComment.append_comment("TITLE", "This is title");

// write changes back to the file ...
// :-( there is no API for that, i.e. something like this:
// FLAC::Metadata::write_tags(fileName, vorbisComment);

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

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

发布评论

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

评论(1

眼泪淡了忧伤 2024-10-14 14:53:01

我已经分析了metaflac源代码(按照建议),这是问题的解决方案:

#include <FLAC++/metadata.h>

const char* fileName = "/path/to/file.flac";

// read a file using FLAC::Metadata::Chain class
FLAC::Metadata::Chain chain;
if (!chain.read(fileName)) {
    // error handling
    throw ...;
}

// now, find vorbis comment block and make changes in it
{
    FLAC::Metadata::Iterator iterator;
    iterator.init(chain);

    // find vorbis comment block
    FLAC::Metadata::VorbisComment* vcBlock = 0;
    do {
        FLAC::Metadata::Prototype* block = iterator.get_block();
        if (block->get_type() == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
            vcBlock = (FLAC::Metadata::VorbisComment*) block;
            break;
        }
    } while (iterator.next());

    // if not found, create a new one
    if (vcBlock == 0) {
        // create a new block
        vcBlock = new FLAC::Metadata::VorbisComment();

        // move iterator to the end
        while (iterator.next()) {
        }

        // insert a new block at the end
        if (!iterator.insert_block_after(vcBlock)) {
            delete vcBlock;
            // error handling
            throw ...;
        }
    }

    // now, you can make any number of changes to the vorbis comment block,
    // for example you can append TITLE comment:
    vcBlock->append_comment(
        FLAC::Metadata::VorbisComment::Entry("TITLE", "This is title"));
}

// write changes back to the file
if (!chain.write()) {
    // error handling
    throw ...;
}

I've analysed metaflac source code (as suggested) and here is a solution to the problem:

#include <FLAC++/metadata.h>

const char* fileName = "/path/to/file.flac";

// read a file using FLAC::Metadata::Chain class
FLAC::Metadata::Chain chain;
if (!chain.read(fileName)) {
    // error handling
    throw ...;
}

// now, find vorbis comment block and make changes in it
{
    FLAC::Metadata::Iterator iterator;
    iterator.init(chain);

    // find vorbis comment block
    FLAC::Metadata::VorbisComment* vcBlock = 0;
    do {
        FLAC::Metadata::Prototype* block = iterator.get_block();
        if (block->get_type() == FLAC__METADATA_TYPE_VORBIS_COMMENT) {
            vcBlock = (FLAC::Metadata::VorbisComment*) block;
            break;
        }
    } while (iterator.next());

    // if not found, create a new one
    if (vcBlock == 0) {
        // create a new block
        vcBlock = new FLAC::Metadata::VorbisComment();

        // move iterator to the end
        while (iterator.next()) {
        }

        // insert a new block at the end
        if (!iterator.insert_block_after(vcBlock)) {
            delete vcBlock;
            // error handling
            throw ...;
        }
    }

    // now, you can make any number of changes to the vorbis comment block,
    // for example you can append TITLE comment:
    vcBlock->append_comment(
        FLAC::Metadata::VorbisComment::Entry("TITLE", "This is title"));
}

// write changes back to the file
if (!chain.write()) {
    // error handling
    throw ...;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文