用taglib提取mp3文件中的图片和一些其它信息的实例

//taglib可以从网上下载,解压包文件中,test and examples 里面有些很好的学习例子,taglib也有API ,可以参考
//reference size:http://sql.codeproject.com/KB/cs/Do_Anything_With_ID3.aspx?display=Print

//

#include <iostream>
#include <stdlib.h>

#include <taglib/tbytevector.h>
#include <taglib/tfile.h>

#include <taglib/mpegfile.h>

#include <taglib/id3v2tag.h>
#include <taglib/id3v2frame.h>
#include <taglib/id3v2header.h>

#include <taglib/id3v1tag.h>

#include <taglib/apetag.h>

#include <taglib/tag.h>
#include <taglib/attachedpictureframe.h>

#include <QDebug>
#include <QImage>

#include <taglib/fileref.h>

using namespace std;
using namespace TagLib;

int main(int argc, char *argv[])
{
  // process the command line args


  for(int i = 1; i < argc; i++) {

    cout << "******************** /"" << argv[i] << "/"********************" << endl;

    MPEG::File f(argv[i]);

    ID3v2::Tag *id3v2tag = f.ID3v2Tag();

    if(id3v2tag) {

      cout << "ID3v2."
           << id3v2tag->header()->majorVersion()
           << "."
           << id3v2tag->header()->revisionNumber()
           << ", "
           << id3v2tag->header()->tagSize()
           << " bytes in tag"
           << endl;

      ID3v2::FrameList::ConstIterator it = id3v2tag->frameList().begin();
      for(; it != id3v2tag->frameList().end(); it++)
        {
            cout << (*it)->frameID() << " - /"" << (*it)->toString() <<"/t it.size()="<<(*it)->size()<< "/"" << endl;
        if (    (*it)->frameID().operator==(ByteVector("APIC")) )
                {
                  cout <<"picture message"<<endl;
                  char * pic = NULL;
                  FILE *fp = NULL;
                  int len = 0;
                  
                 fp = fopen("./song.jpg","wt+");
                  cout<<"render().size() ="<<(*it)->render().size()<<endl;
                 //pic = (*it)->render().data();
                 // cout<<"..render().data()="<<pic <<"/tstrlen()="<<(len=strlen(pic))<<endl;
                  fwrite((*it)->render().data(),(*it)->size(),1,fp);
                }
                // cout <<"render()=" << (*it)->render()<<endl;

        }
    }
    else
      cout << "file does not have a valid id3v2 tag" << endl;

    }



//get the picture
    if(2==argc)

 //   MPEG::File f(argv[1]);
   {
      //TagLib::FileRef f(argv[1]);
      MPEG::File f(argv[1]);
      ID3v2::Tag *id3v2tag = f.ID3v2Tag();
   }
    MPEG::File f(argv[1]);
if (f.ID3v2Tag())
//if( id3v2tag)
 {
    TagLib::ID3v2::FrameList l= f.ID3v2Tag()->frameListMap()["APIC"];
    if (!l.isEmpty())
      {
        QImage cover;
            TagLib::ID3v2::AttachedPictureFrame *p =
static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
        qDebug() << "id3 have picture " << endl;
        size_t size = p->picture().size();
        qDebug() << "mimeType()="<<TStringToQString(p->mimeType()) << endl;
        qDebug() <<"p-type()="<< p->type() << endl;
        qDebug() << "p->picture().size()="<<size << endl;
           
                 FILE *file = NULL;
                 file = fopen("./pic.jpg","wt+");
                       //two methods that can get picture
                // fwrite( p->picture().data(),size,1,file);   //ok
                   fwrite( (const unsigned char*) p->picture().data(),size,1,file);//ok
 
        //cover.loadFromData((const unsigned char*)p->picture().data(), size);
      }
 }


   //*********************************
   //MPEG::File f(argv[i]);
    ID3v2::AttachedPictureFrame attachPic(ByteVector(
      "/x41/x50/x49/x43/x00/x02/x0c/x59/x00/x00/x01/x69/x6d/x61/x67/x65"
      "/x2f/x6a/x70/x65/x67/x00/x00/xfe/xff/x00/x63/x00/x6f/x00/x76/x00"
      "/x65/x00/x72/x00/x2e/x00/x6a/x00/x70/x00/x67/x00/x00/xff/xd8/xff",
      16 * 3));
    
    //ID3v2::AttachedPictureFrame f();
    cout << attachPic.mimeType()<<endl;
 
    cout << "describle="<< attachPic.description() << endl;
   
     
    
     // TagLib::File::File file("./out.jpg");     

     //ByteVector("/xff/xd8/xff", 3),

     //cout<<"bytesize="<<f.picture().size()<<endl;
     const char * pic = NULL;
     FILE *file = NULL;  

     pic = attachPic.picture().data();
     cout << "picture()="<<attachPic.picture()<<"/t picture().data()="<<pic<<endl;
 
                                   
    file = fopen("./picture.jpg","wt+");
   
   // fwrite(pic,strlen(f.picture().data()),1,fp);
     fwrite((const unsigned char*)attachPic.picture().data(),10839,1,file);
    
 

 

//*********************
return 0;

}


//别外一个向mp3文件加picture 的例子

// g++ `taglib-config --cflags --libs` taglib-set-picture.cpp -o taglib-set-picture

#include <mpegfile.h>
#include <attachedpictureframe.h>
#include <id3v2tag.h>

#include <iostream>

/*!
 * Just a hacked up convenience class to read all of the data from an image file.
 * This is probably *NOT* the way that this should be done in real code.  But still
 * something will be needed to identify the mimetype and to read the data.
 */

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

    }

    TagLib::String mimeType() const
    {
        TagLib::String fileName = name();

        if(fileName.substr(fileName.size() - 4, 4).upper() == ".PNG")
            return "image/png";
        else if(fileName.substr(fileName.size() - 4, 4).upper() == ".JPG" ||
                fileName.substr(fileName.size() - 5, 5).upper() == ".JPEG")
            return "image/jpeg";
        else
            return TagLib::String::null;
    }

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

    bool isValid() const
    {
        return isOpen() && !mimeType().isNull();
    }

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: taglib-set-picture foo.mp3 bar.png" << std::endl;
        return 1;
    }

    TagLib::MPEG::File audioFile(argv[1]);
    ImageFile imageFile(argv[2]);

    if(!audioFile.isValid()) {
        std::cout << '"' << audioFile.name() << "/" is not valid." << std::endl;
        return 2;
    }
    if(!imageFile.isValid()) {
        std::cout << '"' << imageFile.name() << "/" is not valid." << std::endl;
        return 3;
    }

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

    frame->setMimeType(imageFile.mimeType());
    frame->setPicture(imageFile.data());

    tag->addFrame(frame);
    audioFile.save();

    return 0;
}


void getPicture()             //resume
{

    -----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,
ok now i know how to read picture in tags using AttachedPictureFrame. Here's
an example:


if(f.ID3v2Tag()){
    TagLib::ID3v2::FrameList l= f.ID3v2Tag()->frameListMap()["APIC"];
    if (!l.isEmpty()){
        QImage cover;
            TagLib::ID3v2::AttachedPictureFrame *p =
static_cast<TagLib::ID3v2::AttachedPictureFrame *>(l.front());
        kdDebug() << "id3 have picture " << endl;
        size_t size = p->picture().size();
        kdDebug() << TStringToQString(p->mimeType()) << endl;
        kdDebug() << p->type() << endl;
        kdDebug() << size << endl;
        cover.loadFromData((const uchar*)p->picture().data(), size);
    }
}

So, now i'm looking on howt to add a picture. Scott, do you have an exemple?
For the moment i try with something like this, but not successfully (c
contains raw binary from mPictureFile) :( :

TagLib::ID3v2::AttachedPictureFrame *p = new
TagLib::ID3v2::AttachedPictureFrame;
KMimeMagicResult *result = KMimeMagic::self()->findFileType(mPictureFile);
p->setMimeType(QStringToTString(result->mimeType()));
p->setType(TagLib::ID3v2::AttachedPictureFrame::Type(0x03));
p->picture().setData(c, sizeof(c));
f.ID3v2Tag()->addFrame(p);

Any help would be appreciate ;)

Bye,
    Cyril.

- --
Un artiste qui plait a tout le monde,
ca deplait a certains. Paradoxal, non ?
    -- P. Geluck
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFBCkXG9J4lbJfupC0RAm6fAJ9CRYONh4tvvKI0r1bulmwW7ATOGQCgrT8D
Q2WBzlxI6X1J+WYxIMq0dmc=
=5RE9
-----END PGP SIGNATURE-----

}

展开阅读全文

没有更多推荐了,返回首页