extras: tweak artist_to_albumartist script a bit
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
CXX=g++
|
||||
CXXFLAGS=-O2 -g
|
||||
CXXFLAGS=-O2 -march=native -pipe -std=c++0x -Wall -Wextra -Wshadow -Werror
|
||||
CPPFLAGS=`taglib-config --cflags`
|
||||
LDFLAGS=`taglib-config --libs`
|
||||
|
||||
|
||||
@@ -21,102 +21,106 @@
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#include "xiphcomment.h"
|
||||
#include "id3v2tag.h"
|
||||
#include "textidentificationframe.h"
|
||||
#include "fileref.h"
|
||||
#include "mpegfile.h"
|
||||
#include "vorbisfile.h"
|
||||
#include "flacfile.h"
|
||||
#include <fileref.h>
|
||||
#include <flacfile.h>
|
||||
#include <mpegfile.h>
|
||||
#include <vorbisfile.h>
|
||||
#include <textidentificationframe.h>
|
||||
#include <id3v2tag.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();
|
||||
while (it != fl.end())
|
||||
if (!((*it++)->toString() == TagLib::String::null))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
typedef TagLib::ID3v2::TextIdentificationFrame TextFrame;
|
||||
|
||||
bool write_id3v2_aa(TagLib::ID3v2::Tag *tag, const TagLib::String &artist)
|
||||
{
|
||||
using TagLib::ID3v2::TextIdentificationFrame;
|
||||
TagLib::ByteVector type = "TPE2";
|
||||
if (!framelist_empty(tag->frameList(type)))
|
||||
return false;
|
||||
TextIdentificationFrame *frame = new TextIdentificationFrame(type, TagLib::String::UTF8);
|
||||
frame->setText(artist);
|
||||
TagLib::ByteVector album_artist = "TPE2";
|
||||
if (!tag->frameList(album_artist).isEmpty())
|
||||
return CopyResult::AlbumArtistAlreadyInPlace;
|
||||
|
||||
auto artists = tag->frameList("TPE1");
|
||||
if (artists.isEmpty())
|
||||
return CopyResult::NoArtist;
|
||||
|
||||
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);
|
||||
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"))
|
||||
return false;
|
||||
tag->addField("ALBUM ARTIST", artist, true);
|
||||
return true;
|
||||
if (tag->contains("ALBUM ARTIST") || tag->contains("ALBUMARTIST"))
|
||||
return CopyResult::AlbumArtistAlreadyInPlace;
|
||||
|
||||
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)
|
||||
{
|
||||
if (n == 0)
|
||||
{
|
||||
cout << "No files to convert, exiting.\n";
|
||||
std::cout << "No files to convert, exiting.\n";
|
||||
return;
|
||||
}
|
||||
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)
|
||||
{
|
||||
cout << "Modifying " << files[i] << "... ";
|
||||
std::cout << "Modifying " << files[i] << "... ";
|
||||
|
||||
TagLib::FileRef f(files[i]);
|
||||
if (!f.isNull())
|
||||
{
|
||||
TagLib::String artist = f.tag()->artist();
|
||||
if (!(artist == TagLib::String::null))
|
||||
CopyResult result;
|
||||
if (auto mp3_f = dynamic_cast<TagLib::MPEG::File *>(f.file()))
|
||||
{
|
||||
TagLib::MPEG::File *mp3_f = 0;
|
||||
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);
|
||||
result = copy_album_artist(mp3_f->ID3v2Tag(true));
|
||||
}
|
||||
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
|
||||
{
|
||||
cout << "Not mp3/ogg/flac file, skipping.\n";
|
||||
std::cout << "Not mp3/ogg/flac file, skipping.\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (success)
|
||||
switch (result)
|
||||
{
|
||||
case CopyResult::Success:
|
||||
if (!dry_run)
|
||||
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
|
||||
cout << "AlbumArtist is already here, skipping.\n";
|
||||
}
|
||||
else
|
||||
cout << "Artist not found, skipping.\n";
|
||||
}
|
||||
else
|
||||
cout << "Could not open file, skipping.\n";
|
||||
std::cout << "Could not open file, skipping.\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,9 +128,9 @@ int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
cout << "This little program copies Artist tag (if present) to\n";
|
||||
cout << "AlbumArtist (if not present) for given mp3/ogg/flac files.\n\n";
|
||||
cout << "Usage: " << argv[0] << " [--dry-run] files\n";
|
||||
std::cout << "This little script copies Artist tag (if present) to\n";
|
||||
std::cout << "AlbumArtist (if not present) for given mp3/ogg/flac files.\n\n";
|
||||
std::cout << "Usage: " << argv[0] << " [--dry-run] files\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user