c++ - 如何使用 TagLib 读取/写入不同音频格式的封面?

标签 c++ taglib cover

我想使用 TagLib读/写封面,尤其是 mp3 和 ogg 文件,但我找不到任何示例。有人可以指出一些例子吗?我应该在哪里寻找有关此的更多信息?

最佳答案

这是 mp3 和 m4a 的版本。

#include <mpegfile.h>
#include <attachedpictureframe.h>
#include <id3v2tag.h>
#include <mp4file.h>
#include <mp4tag.h>
#include <mp4coverart.h>

#include <iostream>

class ImageFile : public TagLib::File
{
public:
ImageFile(const char *file) : TagLib::File(file)
{

}

TagLib::ByteVector data()
{
    return readBlock(length());
}


private:
virtual TagLib::Tag *tag() const { return 0; }
virtual TagLib::AudioProperties *audioProperties() const { return 0; }
virtual bool save() { return false; }
};

int main(int argc, char *argv[])
{
if (argc != 3) 
{
    std::cout << "Usage: setcover <mp3|m4a> cover.jpg" << std::endl;
    return 1;
}

TagLib::String fileName = argv[1];
TagLib::String fileType = fileName.substr(fileName.size() - 3).upper();

ImageFile imageFile(argv[2]);

if (fileType == "M4A")
{
  // read the image file
  TagLib::MP4::CoverArt coverArt((TagLib::MP4::CoverArt::Format) 0x0D,   imageFile.data());

  // read the mp4 file
  TagLib::MP4::File audioFile(argv[1]);

  // get the tag ptr
  TagLib::MP4::Tag *tag = audioFile.tag();

  // get the items map
  TagLib::MP4::ItemListMap itemsListMap = tag->itemListMap();

  // create cover art list
  TagLib::MP4::CoverArtList coverArtList;

  // append instance
  coverArtList.append(coverArt);

  // convert to item
  TagLib::MP4::Item coverItem(coverArtList);

  // add item to map
  itemsListMap.insert("covr", coverItem);

  tag->save();
  //audioFile.save();      
}
else if (fileType == "MP3")
{
  TagLib::MPEG::File audioFile(argv[1]);

  TagLib::ID3v2::Tag *tag = audioFile.ID3v2Tag(true);
  TagLib::ID3v2::AttachedPictureFrame *frame = new TagLib::ID3v2::AttachedPictureFrame;

  frame->setMimeType("image/jpeg");
  frame->setPicture(imageFile.data());

  tag->addFrame(frame);
  audioFile.save();      
}
else
{
   std::cout << fileType << " is unsupported." << std::endl;
}
}

关于c++ - 如何使用 TagLib 读取/写入不同音频格式的封面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4752020/

相关文章:

C++ 检查模板实例变量的类型

c++ - 我应该在头文件中的什么地方声明我的私有(private)类级静态常量?

java - 打包在 EAR 文件中的 JAR 文件中的 JSP taglib

Perl:将 MP3 ID3-Tags 的封面保存到外部 JPG 文件中

ios - UIImageView 覆盖单元格中的文本

c++用2星替换空格

c++ - 插入后对 vector 进行排序与​​插入到集合中

javascript - 如何在 tmLanguage 文件中添加另一个要解析为嵌入式 JS 的标签?

单元测试 grails 标签

html - 覆盖覆盖的背景图像。