2011-01-20 10 views
8

मैं विशेष रूप से एमपी 3 और ogg फ़ाइलों के लिए coverart पढ़ने/लिखने के लिए TagLib का उपयोग करना चाहता हूं, लेकिन मुझे कोई उदाहरण नहीं मिला। क्या कोई मुझे कुछ उदाहरणों के लिए इंगित कर सकता है? मुझे इसके बारे में और जानकारी कहां मिलनी चाहिए?मैं विभिन्न ऑडियो प्रारूपों में कवरार्ट पढ़ने/लिखने के लिए टैगलिब का उपयोग कैसे करूं?

+0

विकिपीडिया कहते हैं एमपी 3 और ogg अलग टैगिंग स्वरूपों इसलिए इसकी संभावना है कि कोड के नीचे केवल एमपी 3 के लिए काम करेंगे है। इस लेख को देखें http://en.wikipedia.org/wiki/ID3 –

उत्तर

6

मुझे इसे एमपी 3 फाइलों के लिए काम करने के लिए मिला। देखें कि क्या आप इसे ogg

शुभकामनाएं के लिए अनुकूलित कर सकते हैं या नहीं।
पीएस :: यह टैगिंग सामान यह होना चाहिए जितना कठिन होना चाहिए। हमें एक और lib खोजने की जरूरत है।

/********************************************************************************************************************************* 
*Description: A simple program using taglib to extract pictures attached to mp3 id3v2 tags 
*Author: Dr Deo [at] stackoverflow *dot* com 
*AOB: I hope you will find this useful and are free to use it for anything, but there is no waranty and use at your own risk :) 
********************************************************************************************************************************* 
*/ 
#include<iostream> 
#include<stdio.h> 

/*taglib specific includes*/ 
#include<tbytevector.h>//ByteVector 
#include<mpegfile.h>//mp3 file 
#include<id3v2tag.h>//tag 
#include<id3v2frame.h>//frame 
#include <attachedPictureFrame.h>//attachedPictureFrame 

using namespace std ; 
using namespace TagLib::ID3v2 ; 

int main(int argc, char * argv[]) 
{ 
    if(argc !=2) 
    { 
     cout<<"usage: drag an mp3 file on to the program and it will extract the attached image"<<endl<<endl; 
     system("pause");//on linux you can replace this with cin.get() 
     exit(1); 
    } 

    TagLib::MPEG::File mp3File(argv[1]); 
    Tag * mp3Tag; 
    FrameList listOfMp3Frames; 
    AttachedPictureFrame * pictureFrame; 

    mp3Tag= mp3File.ID3v2Tag(); 
    if(mp3Tag) 
    { 
     listOfMp3Frames = mp3Tag->frameListMap()["APIC"];//look for picture frames only 
     if(!listOfMp3Frames.isEmpty()) 
     { 
      FrameList::ConstIterator it= listOfMp3Frames.begin(); 
      for(; it != listOfMp3Frames.end() ; it++) 
      { 
       pictureFrame = static_cast<AttachedPictureFrame *> (*it);//cast Frame * to AttachedPictureFrame* 

       //Warning. format of picture assumed to be jpg. This may be false, for example it may be png. 
       FILE * fout; 
       fopen_s(&fout, "outputFile.jpg", "wb"); 
       cout<<"processing the file "<< argv[1] <<endl<<endl; 
       fwrite(pictureFrame->picture().data(), pictureFrame->picture().size(), 1, fout); 
       fclose(fout); 
       cout<<" The picture has been written to \t outputFile.jpg \nRemember that the file type .jpg is just assumed for simplicity"<<endl<<endl; 
      } 
     } 
     else cerr<<"there seem to be no picture frames (APIC) frames in this file"<<endl<<endl; 
    } 
    else cerr<<"the file "<<argv[1]<<"does not appear to have any mp3 tags"<<endl<<endl; 

    system("pause");//on linux you can replace this with cin.get() 
    return 0; 
} 
3

Ogg Vorbis टैग केवल-पाठ (और इस तरह के रूप में कवर कला का समर्थन नहीं करते) हैं। एमपी 3 के लिए, यह सुझाए गए अन्य समाधान की तुलना में कुछ हद तक साफ है:

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<ID3v2::AttachedPictureFrame *>(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<ID3v2::AttachedPictureFrame *>(frames.front()); 
    } 

    frame->setPicture(image.data); 
    frame->setMimeType(image.mimeType); 
} 
+0

मुझे नहीं पता था कि ओग फाइलों में कवर आर्ट के लिए कोई आधिकारिक समर्थन नहीं है। यह वहां बहुत सारे मीडिया प्लेयर के साथ समस्याओं की व्याख्या करेगा :-( – Bernd

+0

ओग एल्बम-कला का समर्थन करता है, भले ही यह 'टेक्स्ट-केवल' है। –

+1

यह कुछ हद तक मनोरंजक है कि मुझे इस पर दो बार वोट दिया गया है: ** मैं टैगलिब ** का लेखक हूं। उस समय मैंने इस टिप्पणी को लिखा था, Xiph टैग वास्तव में छवियों का समर्थन नहीं करते थे (और विशेष रूप से एक अलग मेटा-डेटा स्ट्रीम का उपयोग करके अनुशंसा की गई थी)। उन्होंने बाद में बेस 64 टेक्स्ट विकल्प जोड़ा है – scotchi

9

एमपी 3 और एम 4 ए के लिए एक संस्करण है।

#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; 
} 
} 
2

ओग सहित एक संपूर्ण समाधान है। ऐसा करने का अनौपचारिक तरीका बेस 64 को फ़ाइल एन्कोड करना है और इसे मेटाडेटा में एम्बेड करना है। अब एक एफएलएसी पिक्चर ब्लॉक (जिसमें छवि डेटा, या एक फ़ाइल: // यूआरएल शामिल हो सकता है) बेस बेस एन्कोडिंग का एक प्रस्तावित (बेहतर) तरीका है।

https://gist.github.com/1468279