2010-12-16 147 views
1

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

例如:

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

您有可用于metaflac程序的源代码,你应该能够弄清楚(即使它是C)。 – 2010-12-16 16:20:03

+0

如果没有人回答,我会走这条路。 – mvladic 2010-12-16 19:57:27

回答

1

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