bool updateTags(char *path, Metadata *meta) { MPEG::File file(path); if (!file.isValid() || file.readOnly()) { return false; } ID3v2::Tag *tag = file.ID3v2Tag(true); if (!tag) { return false; } if (meta->hasArtist) { setTextFrame(tag, "TPE1", meta->artist); } if (meta->hasAlbum) { setTextFrame(tag, "TALB", meta->album); } if (meta->hasAlbumArtist) { setTextFrame(tag, "TPE2", meta->albumartist); } if (meta->hasTitle) { setTextFrame(tag, "TIT2", meta->title); } if (meta->hasTrackNumber) { String text = String::number(meta->trackNumber); if (meta->hasTrackCount) { text += "/" + String::number(meta->trackCount); } setTextFrame(tag, "TRCK", text); } if (meta->hasGenre) { setTextFrame(tag, "TCON", meta->genre); } if (meta->hasPublisher) { setTextFrame(tag, "TPUB", meta->publisher); } if (meta->hasYear) { setTextFrame(tag, "TDRC", String::number(meta->year)); } if (meta->hasImage) { ID3v2::AttachedPictureFrame *f = new ID3v2::AttachedPictureFrame(); f->setType(ID3v2::AttachedPictureFrame::Other); f->setPicture(meta->image); f->setMimeType(detectImageMimeType(meta->image)); tag->removeFrames("APIC"); tag->addFrame(f); } return file.save(MPEG::File::ID3v1 | MPEG::File::ID3v2, false, 3); }
void testUpdateGenre24() { ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance(); ByteVector data = ByteVector("TCON" // Frame ID "\x00\x00\x00\x0D" // Frame size "\x00\x00" // Frame flags "\0" // Encoding "14\0Eurodisco", 23); // Text ID3v2::TextIdentificationFrame *frame = static_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, TagLib::uint(4))); CPPUNIT_ASSERT_EQUAL(TagLib::uint(2), frame->fieldList().size()); CPPUNIT_ASSERT_EQUAL(String("14"), frame->fieldList()[0]); CPPUNIT_ASSERT_EQUAL(String("Eurodisco"), frame->fieldList()[1]); ID3v2::Tag tag; tag.addFrame(frame); CPPUNIT_ASSERT_EQUAL(String("R&B Eurodisco"), tag.genre()); }
void testUpdateGenre23_1() { // "Refinement" is the same as the ID3v1 genre - duplicate ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance(); ByteVector data = ByteVector("TCON" // Frame ID "\x00\x00\x00\x10" // Frame size "\x00\x00" // Frame flags "\x00" // Encoding "(22)Death Metal", 26); // Text ID3v2::TextIdentificationFrame *frame = static_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, TagLib::uint(3))); CPPUNIT_ASSERT_EQUAL(TagLib::uint(1), frame->fieldList().size()); CPPUNIT_ASSERT_EQUAL(String("Death Metal"), frame->fieldList()[0]); ID3v2::Tag tag; tag.addFrame(frame); CPPUNIT_ASSERT_EQUAL(String("Death Metal"), tag.genre()); }
void testUpdateGenre23_2() { // "Refinement" is different from the ID3v1 genre ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance(); ByteVector data = ByteVector("TCON" // Frame ID "\x00\x00\x00\x13" // Frame size "\x00\x00" // Frame flags "\x00" // Encoding "(4)Eurodisco", 23); // Text ID3v2::TextIdentificationFrame *frame = static_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, TagLib::uint(3))); CPPUNIT_ASSERT_EQUAL(TagLib::uint(2), frame->fieldList().size()); CPPUNIT_ASSERT_EQUAL(String("4"), frame->fieldList()[0]); CPPUNIT_ASSERT_EQUAL(String("Eurodisco"), frame->fieldList()[1]); ID3v2::Tag tag; tag.addFrame(frame); CPPUNIT_ASSERT_EQUAL(String("Disco Eurodisco"), tag.genre()); }
void testDontRender22() { ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance(); ByteVector data = ByteVector("FOO" "\x00\x00\x08" "\x00" "JPG" "\x01" "d\x00" "\x00", 18); ID3v2::AttachedPictureFrame *frame = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(factory->createFrame(data, TagLib::uint(2))); CPPUNIT_ASSERT(frame); ID3v2::Tag tag; tag.addFrame(frame); CPPUNIT_ASSERT_EQUAL(TagLib::uint(1034), tag.render().size()); }
int main(int argc, char *argv[]) { if (argc > 1) { filesystem::path current_dir; regex pattern(".*(mp3|m4a)$"); for (int i = 1; i < argc; ++i) { current_dir = argv[i]; cerr << current_dir << endl; for (filesystem::recursive_directory_iterator iter(current_dir), end; iter != end; ++iter) { const string name = iter->path().filename().native(); smatch what; if (regex_match(name, what, pattern) && filesystem::is_regular_file(iter->path())) { string ext = what[1]; string filename(what[0].first, what[0].second); transform(ext.begin(), ext.end(), ext.begin(), ::tolower); String album; String albumartist; String track; String trackartist; TagLib::uint tracknumber; TagLib::uint year; if (ext == "mp3") { MPEG::File file(iter->path().string().c_str()); ID3v2::Tag *tag = file.ID3v2Tag(); if (tag) { track = tag->title(); trackartist = tag->artist(); album = tag->album(); if (tag->frameListMap().contains("TPE2")) { albumartist = tag->frameListMap()["TPE2"].front()->toString(); } tracknumber = tag->track(); year = tag->year(); } else { ID3v1::Tag *old_tag = file.ID3v1Tag(); if (old_tag) { track = old_tag->title(); trackartist = old_tag->artist(); album = old_tag->album(); /*if (old_tag->frameListMap().contains("TPE2")) { albumartist = old_tag->frameListMap()["TPE2"].front()->toString(); }*/ tracknumber = old_tag->track(); year = old_tag->year(); } } } else if (ext == "m4a") { MP4::File file(iter->path().string().c_str()); MP4::Tag *tag = file.tag(); if (tag) { track = tag->title(); trackartist = tag->artist(); album = tag->album(); albumartist = tag->itemListMap()["aArt"].toStringList().toString(", "); tracknumber = tag->track(); year = tag->year(); } } string tn; if (tracknumber != 0) { tn = lexical_cast<string>(tracknumber); } else { tn = ""; } string yr; if (year != 0) { yr = lexical_cast<string>(year); } else { yr = ""; } cout << "TRACK " << iter->path().string() << "\t" << tn << "\t" << albumartist << "\t" << album << "\t" << yr << "\t" << trackartist << "\t" << track << "\n"; } } } return 0; } else { cerr << "No dirs specified" << endl; return 1; } }