gpt4 book ai didi

c++ - 使用 libFLAC++ 将 vorbis 评论元数据(标签)写入现有的 FLAC 文件

转载 作者:行者123 更新时间:2023-11-30 04:36:40 26 4
gpt4 key购买 nike

如何使用 libFLAC++ ( http://flac.sourceforge.net ) 将 vorbis 评论元数据,即标签(例如“TITLE”)写入现有的 FLAC 文件?

例如:

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);

最佳答案

我已经分析了 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 ...;
}

关于c++ - 使用 libFLAC++ 将 vorbis 评论元数据(标签)写入现有的 FLAC 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4462566/

26 4 0