extras: tweak artist_to_albumartist script a bit

This commit is contained in:
Andrzej Rybczak
2012-10-02 19:40:03 +02:00
parent efe2a52382
commit 49d1d56722
2 changed files with 104 additions and 100 deletions

View File

@@ -1,5 +1,5 @@
CXX=g++ CXX=g++
CXXFLAGS=-O2 -g CXXFLAGS=-O2 -march=native -pipe -std=c++0x -Wall -Wextra -Wshadow -Werror
CPPFLAGS=`taglib-config --cflags` CPPFLAGS=`taglib-config --cflags`
LDFLAGS=`taglib-config --libs` LDFLAGS=`taglib-config --libs`

View File

@@ -21,102 +21,106 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include "xiphcomment.h" #include <fileref.h>
#include "id3v2tag.h" #include <flacfile.h>
#include "textidentificationframe.h" #include <mpegfile.h>
#include "fileref.h" #include <vorbisfile.h>
#include "mpegfile.h" #include <textidentificationframe.h>
#include "vorbisfile.h" #include <id3v2tag.h>
#include "flacfile.h" #include <xiphcomment.h>
using std::cout; enum class CopyResult { Success, NoArtist, AlbumArtistAlreadyInPlace };
bool framelist_empty(const TagLib::ID3v2::FrameList &fl) CopyResult copy_album_artist(TagLib::ID3v2::Tag *tag)
{ {
TagLib::ID3v2::FrameList::ConstIterator it = fl.begin(); typedef TagLib::ID3v2::TextIdentificationFrame TextFrame;
while (it != fl.end())
if (!((*it++)->toString() == TagLib::String::null))
return false;
return true;
}
bool write_id3v2_aa(TagLib::ID3v2::Tag *tag, const TagLib::String &artist) TagLib::ByteVector album_artist = "TPE2";
{ if (!tag->frameList(album_artist).isEmpty())
using TagLib::ID3v2::TextIdentificationFrame; return CopyResult::AlbumArtistAlreadyInPlace;
TagLib::ByteVector type = "TPE2";
if (!framelist_empty(tag->frameList(type))) auto artists = tag->frameList("TPE1");
return false; if (artists.isEmpty())
TextIdentificationFrame *frame = new TextIdentificationFrame(type, TagLib::String::UTF8); return CopyResult::NoArtist;
frame->setText(artist);
for (auto it = artists.begin(); it != artists.end(); ++it)
{
auto frame = new TextFrame(album_artist, TagLib::String::UTF8);
frame->setText((*it)->toString());
tag->addFrame(frame); tag->addFrame(frame);
return true; }
return CopyResult::Success;
} }
bool write_xiphcomment_aa(TagLib::Ogg::XiphComment *tag, const TagLib::String &artist) CopyResult copy_album_artist(TagLib::Ogg::XiphComment *tag)
{ {
if (tag->contains("ALBUM ARTIST")) if (tag->contains("ALBUM ARTIST") || tag->contains("ALBUMARTIST"))
return false; return CopyResult::AlbumArtistAlreadyInPlace;
tag->addField("ALBUM ARTIST", artist, true);
return true; auto artists = tag->fieldListMap()["ARTIST"];
if (artists.isEmpty())
return CopyResult::NoArtist;
for (auto it = artists.begin(); it != artists.end(); ++it)
tag->addField("ALBUMARTIST", *it, true);
return CopyResult::Success;
} }
void convert(int n, char **files, bool dry_run) void convert(int n, char **files, bool dry_run)
{ {
if (n == 0) if (n == 0)
{ {
cout << "No files to convert, exiting.\n"; std::cout << "No files to convert, exiting.\n";
return; return;
} }
if (dry_run) if (dry_run)
cout << "Dry run mode enabled, pretending to modify files.\n"; std::cout << "Dry run mode enabled, pretending to modify files.\n";
for (int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
{ {
cout << "Modifying " << files[i] << "... "; std::cout << "Modifying " << files[i] << "... ";
TagLib::FileRef f(files[i]); TagLib::FileRef f(files[i]);
if (!f.isNull()) if (!f.isNull())
{ {
TagLib::String artist = f.tag()->artist(); CopyResult result;
if (!(artist == TagLib::String::null)) if (auto mp3_f = dynamic_cast<TagLib::MPEG::File *>(f.file()))
{ {
TagLib::MPEG::File *mp3_f = 0; result = copy_album_artist(mp3_f->ID3v2Tag(true));
TagLib::Ogg::Vorbis::File *ogg_f = 0;
TagLib::FLAC::File *flac_f = 0;
bool success;
if ((mp3_f = dynamic_cast<TagLib::MPEG::File *>(f.file())))
{
success = write_id3v2_aa(mp3_f->ID3v2Tag(true), artist);
} }
else if ((ogg_f = dynamic_cast<TagLib::Ogg::Vorbis::File *>(f.file()))) else if (auto ogg_f = dynamic_cast<TagLib::Ogg::Vorbis::File *>(f.file()))
{ {
success = write_xiphcomment_aa(ogg_f->tag(), artist); result = copy_album_artist(ogg_f->tag());
} }
else if ((flac_f = dynamic_cast<TagLib::FLAC::File *>(f.file()))) else if (auto flac_f = dynamic_cast<TagLib::FLAC::File *>(f.file()))
{ {
success = write_xiphcomment_aa(flac_f->xiphComment(true), artist); result = copy_album_artist(flac_f->xiphComment(true));
} }
else else
{ {
cout << "Not mp3/ogg/flac file, skipping.\n"; std::cout << "Not mp3/ogg/flac file, skipping.\n";
continue; continue;
} }
if (success) switch (result)
{ {
case CopyResult::Success:
if (!dry_run) if (!dry_run)
f.save(); f.save();
cout << "Done.\n"; std::cout << "Done.\n";
break;
case CopyResult::NoArtist:
std::cout << "Artist not found, skipping.\n";
break;
case CopyResult::AlbumArtistAlreadyInPlace:
std::cout << "AlbumArtist is already there, skipping.\n";
break;
}
} }
else else
cout << "AlbumArtist is already here, skipping.\n"; std::cout << "Could not open file, skipping.\n";
}
else
cout << "Artist not found, skipping.\n";
}
else
cout << "Could not open file, skipping.\n";
} }
} }
@@ -124,9 +128,9 @@ int main(int argc, char **argv)
{ {
if (argc < 2) if (argc < 2)
{ {
cout << "This little program copies Artist tag (if present) to\n"; std::cout << "This little script copies Artist tag (if present) to\n";
cout << "AlbumArtist (if not present) for given mp3/ogg/flac files.\n\n"; std::cout << "AlbumArtist (if not present) for given mp3/ogg/flac files.\n\n";
cout << "Usage: " << argv[0] << " [--dry-run] files\n"; std::cout << "Usage: " << argv[0] << " [--dry-run] files\n";
} }
else else
{ {