|
@@ -0,0 +1,108 @@
|
|
|
|
+// Requires:
|
|
|
|
+// apt-get install libtag1-dev
|
|
|
|
+//
|
|
|
|
+// Pythonばっかだったから頭ごっちゃになる
|
|
|
|
+
|
|
|
|
+#include <iostream>
|
|
|
|
+#include <iomanip>
|
|
|
|
+#include <string>
|
|
|
|
+#include <stdio.h>
|
|
|
|
+
|
|
|
|
+#include <taglib/fileref.h>
|
|
|
|
+#include <taglib/tag.h>
|
|
|
|
+#include <taglib/tpropertymap.h>
|
|
|
|
+
|
|
|
|
+#include <taglib/fileref.h>
|
|
|
|
+#include <taglib/apefile.h>
|
|
|
|
+#include <taglib/apetag.h>
|
|
|
|
+#include <taglib/asffile.h>
|
|
|
|
+#include <taglib/attachedpictureframe.h>
|
|
|
|
+#include <taglib/commentsframe.h>
|
|
|
|
+#include <taglib/flacfile.h>
|
|
|
|
+#include <taglib/id3v1genres.h>
|
|
|
|
+#include <taglib/id3v2tag.h>
|
|
|
|
+#include <taglib/mpcfile.h>
|
|
|
|
+#include <taglib/mpegfile.h>
|
|
|
|
+#include <taglib/mp4file.h>
|
|
|
|
+#include <taglib/tag.h>
|
|
|
|
+#include <taglib/taglib.h>
|
|
|
|
+#include <taglib/textidentificationframe.h>
|
|
|
|
+#include <taglib/tstring.h>
|
|
|
|
+#include <taglib/vorbisfile.h>
|
|
|
|
+
|
|
|
|
+using namespace std;
|
|
|
|
+
|
|
|
|
+void error(void)
|
|
|
|
+{
|
|
|
|
+ cerr << "extractCoverArt infile outfile" << endl;
|
|
|
|
+ exit(-1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int saveCoverArt(TagLib::ByteVector d, string outfile)
|
|
|
|
+{
|
|
|
|
+ FILE *f = fopen(outfile.c_str(), "wb");
|
|
|
|
+ if(f != NULL ){
|
|
|
|
+ fwrite(d.data(), 1,d.size(), f);
|
|
|
|
+ fclose(f);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+ return -1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int extractCoverArt(TagLib::FileRef ifile, string outfile)
|
|
|
|
+{
|
|
|
|
+ if(TagLib::FLAC::File *f = dynamic_cast<TagLib::FLAC::File *>(ifile.file())) { // flac
|
|
|
|
+
|
|
|
|
+ TagLib::List<TagLib::FLAC::Picture*> pl = f->pictureList();
|
|
|
|
+ if( !pl.isEmpty() ) {
|
|
|
|
+ TagLib::FLAC::Picture *pic = pl[0]; // 最初のカバー
|
|
|
|
+ return saveCoverArt(pic->data(), outfile);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ return -1; // 空
|
|
|
|
+ }
|
|
|
|
+ if(TagLib::MPEG::File *f = dynamic_cast<TagLib::MPEG::File *>(ifile.file())) {
|
|
|
|
+ if(f->ID3v2Tag()) { // ID3v2
|
|
|
|
+ TagLib::ID3v2::Tag* tag = f->ID3v2Tag();
|
|
|
|
+ TagLib::ID3v2::FrameList list = tag->frameList("APIC");
|
|
|
|
+ if(!list.isEmpty()) {
|
|
|
|
+ TagLib::ID3v2::AttachedPictureFrame* frame = (TagLib::ID3v2::AttachedPictureFrame *)list.front(); // フロントカバー
|
|
|
|
+ return saveCoverArt(frame->picture(), outfile);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ return -1; // 空
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ return -1; // ID3v2以外はサポートしない
|
|
|
|
+ }
|
|
|
|
+ if(TagLib::MP4::File *f = dynamic_cast<TagLib::MP4::File *>(ifile.file())) { // MPEG-4/AAC
|
|
|
|
+ TagLib::MP4::Tag *tag = f->tag();
|
|
|
|
+ TagLib::MP4::ItemListMap itemListMap = tag->itemListMap();
|
|
|
|
+ if(itemListMap.contains("covr")) { // カバー
|
|
|
|
+ TagLib::MP4::CoverArtList list = itemListMap["covr"].toCoverArtList();
|
|
|
|
+ if(!list.isEmpty()){
|
|
|
|
+ TagLib::MP4::CoverArt *pic = &(list.front()); // フロントカバー
|
|
|
|
+ return saveCoverArt(pic->data(), outfile);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ return -1; // 空
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ // 未実装
|
|
|
|
+ return -1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int main(int argc, char *argv[])
|
|
|
|
+{
|
|
|
|
+ if(argc < 3)
|
|
|
|
+ error();
|
|
|
|
+
|
|
|
|
+ TagLib::FileRef ifile(argv[1]);
|
|
|
|
+ if(ifile.isNull()) // メディアファイルでない
|
|
|
|
+ error();
|
|
|
|
+
|
|
|
|
+ return extractCoverArt(ifile, string(argv[2]));
|
|
|
|
+}
|