How do I use TagLib to read/write coverart in different audio formats?

后端 未结 4 1968
温柔的废话
温柔的废话 2021-02-02 02:40

I would like to use TagLib to read/write coverart especially for mp3 AND ogg files, but I couldn\'t find any examples. Could someone point me to some examples? Where should I lo

4条回答
  •  日久生厌
    2021-02-02 03:10

    Ogg Vorbis tags are text-only (and as such don't support cover art). For MP3s, this is somewhat cleaner than the other solution suggested:

    using namespace TagLib;
    
    struct Image
    {
        Image(const String &m = String(), const ByteVector &d = ByteVector()) :
          mimeType(m), data(d) {}
        String mimeType;
        ByteVector data;
    };
    
    static Image getImage(const ID3v2::Tag *tag)
    {
        ID3v2::FrameList frames = tag->frameList("APIC");
    
        if(frames.isEmpty())
        {
            return Image();
        }
    
        ID3v2::AttachedPictureFrame *frame =
            static_cast(frames.front());
    
        return Image(frame->mimeType(), frame->picture());
    }
    
    static void setImage(ID3v2::Tag *tag, const Image &image)
    {
        ID3v2::FrameList frames = tag->frameList("APIC");
        ID3v2::AttachedPictureFrame *frame = 0;
    
        if(frames.isEmpty())
        {
            frame = new TagLib::ID3v2::AttachedPictureFrame;
            tag->addFrame(frame);
        }
        else
        {
            frame = static_cast(frames.front());
        }
    
        frame->setPicture(image.data);
        frame->setMimeType(image.mimeType);
    }
    

提交回复
热议问题