Записать метаданные (теги) комментария vorbis в существующий файл FLAC, используя libFLAC ++ - PullRequest
1 голос
/ 16 декабря 2010

Как записать метаданные комментариев vorbis, т. Е. Теги (например, «TITLE»), в существующий файл FLAC, используя 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);

1 Ответ

2 голосов
/ 17 декабря 2010

Я проанализировал исходный код 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 ...;
}
...