2018-02-27 18:06:05 +01:00
|
|
|
/* This file is part of Strawberry.
|
|
|
|
Copyright 2013, David Sansome <me@davidsansome.com>
|
2018-09-08 12:38:02 +02:00
|
|
|
Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Strawberry is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "tagreader.h"
|
|
|
|
|
|
|
|
#include <memory>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <sys/stat.h>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-07-07 23:44:04 +02:00
|
|
|
#include <taglib/taglib.h>
|
|
|
|
#include <taglib/fileref.h>
|
|
|
|
#include <taglib/tbytevector.h>
|
|
|
|
#include <taglib/tfile.h>
|
|
|
|
#include <taglib/tlist.h>
|
|
|
|
#include <taglib/tstring.h>
|
|
|
|
#include <taglib/tstringlist.h>
|
|
|
|
#include <taglib/audioproperties.h>
|
|
|
|
#include <taglib/attachedpictureframe.h>
|
|
|
|
#include <taglib/textidentificationframe.h>
|
|
|
|
#include <taglib/unsynchronizedlyricsframe.h>
|
|
|
|
#include <taglib/xiphcomment.h>
|
|
|
|
#include <taglib/commentsframe.h>
|
|
|
|
#include <taglib/tag.h>
|
|
|
|
#include <taglib/apetag.h>
|
|
|
|
#include <taglib/apeitem.h>
|
|
|
|
#include <taglib/apeproperties.h>
|
|
|
|
#include <taglib/id3v2tag.h>
|
|
|
|
#include <taglib/id3v2frame.h>
|
|
|
|
#include <taglib/flacfile.h>
|
|
|
|
#include <taglib/oggflacfile.h>
|
|
|
|
#include <taglib/flacproperties.h>
|
|
|
|
#include <taglib/flacpicture.h>
|
|
|
|
#include <taglib/vorbisfile.h>
|
|
|
|
#include <taglib/speexfile.h>
|
|
|
|
#include <taglib/wavfile.h>
|
|
|
|
#include <taglib/wavpackfile.h>
|
|
|
|
#include <taglib/wavpackproperties.h>
|
|
|
|
#include <taglib/aifffile.h>
|
|
|
|
#include <taglib/asffile.h>
|
|
|
|
#include <taglib/asftag.h>
|
|
|
|
#include <taglib/asfattribute.h>
|
|
|
|
#include <taglib/asfproperties.h>
|
|
|
|
#include <taglib/mp4file.h>
|
|
|
|
#include <taglib/mp4tag.h>
|
|
|
|
#include <taglib/mp4item.h>
|
|
|
|
#include <taglib/mp4coverart.h>
|
|
|
|
#include <taglib/mp4properties.h>
|
|
|
|
#include <taglib/mpcfile.h>
|
|
|
|
#include <taglib/mpegfile.h>
|
|
|
|
#include <taglib/opusfile.h>
|
|
|
|
#include <taglib/trueaudiofile.h>
|
|
|
|
#include <taglib/apefile.h>
|
|
|
|
#ifdef HAVE_TAGLIB_DSFFILE
|
|
|
|
# include <taglib/dsffile.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TAGLIB_DSDIFFFILE
|
|
|
|
# include <taglib/dsdifffile.h>
|
|
|
|
#endif
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QtGlobal>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QList>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QTextCodec>
|
|
|
|
#include <QVector>
|
|
|
|
#include <QtDebug>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/messagehandler.h"
|
|
|
|
#include "core/timeconstants.h"
|
|
|
|
|
|
|
|
class FileRefFactory {
|
|
|
|
public:
|
|
|
|
virtual ~FileRefFactory() {}
|
2018-05-01 00:41:33 +02:00
|
|
|
virtual TagLib::FileRef *GetFileRef(const QString &filename) = 0;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class TagLibFileRefFactory : public FileRefFactory {
|
|
|
|
public:
|
2020-06-15 21:55:05 +02:00
|
|
|
TagLib::FileRef *GetFileRef(const QString &filename) override {
|
2019-02-02 12:39:11 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
2018-02-27 18:06:05 +01:00
|
|
|
return new TagLib::FileRef(filename.toStdWString().c_str());
|
2019-02-02 12:39:11 +01:00
|
|
|
#else
|
2018-02-27 18:06:05 +01:00
|
|
|
return new TagLib::FileRef(QFile::encodeName(filename).constData());
|
2019-02-02 12:39:11 +01:00
|
|
|
#endif
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
TagLib::String StdStringToTaglibString(const std::string &s) {
|
2018-02-27 18:06:05 +01:00
|
|
|
return TagLib::String(s.c_str(), TagLib::String::UTF8);
|
|
|
|
}
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
TagLib::String QStringToTaglibString(const QString &s) {
|
2018-02-27 18:06:05 +01:00
|
|
|
return TagLib::String(s.toUtf8().constData(), TagLib::String::UTF8);
|
|
|
|
}
|
|
|
|
|
2020-06-26 23:30:30 +02:00
|
|
|
} // namespace
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
namespace {
|
2019-03-22 23:13:49 +01:00
|
|
|
// Tags containing the year the album was originally released (in contrast to other tags that contain the release year of the current edition)
|
2018-02-27 18:06:05 +01:00
|
|
|
const char *kMP4_OriginalYear_ID = "----:com.apple.iTunes:ORIGINAL YEAR";
|
|
|
|
const char *kASF_OriginalDate_ID = "WM/OriginalReleaseTime";
|
|
|
|
const char *kASF_OriginalYear_ID = "WM/OriginalReleaseYear";
|
2020-06-26 23:30:30 +02:00
|
|
|
} // namespace
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
|
2019-03-22 23:13:49 +01:00
|
|
|
TagReader::TagReader() :
|
|
|
|
factory_(new TagLibFileRefFactory),
|
2019-07-21 15:12:28 +02:00
|
|
|
kEmbeddedCover("(embedded)") {
|
|
|
|
}
|
|
|
|
|
|
|
|
TagReader::~TagReader() {
|
|
|
|
delete factory_;
|
|
|
|
}
|
2019-03-22 23:13:49 +01:00
|
|
|
|
|
|
|
pb::tagreader::SongMetadata_FileType TagReader::GuessFileType(TagLib::FileRef *fileref) const {
|
|
|
|
|
|
|
|
if (dynamic_cast<TagLib::RIFF::WAV::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_WAV;
|
|
|
|
if (dynamic_cast<TagLib::FLAC::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_FLAC;
|
|
|
|
if (dynamic_cast<TagLib::WavPack::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_WAVPACK;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::FLAC::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_OGGFLAC;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::Vorbis::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_OGGVORBIS;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::Opus::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_OGGOPUS;
|
|
|
|
if (dynamic_cast<TagLib::Ogg::Speex::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_OGGSPEEX;
|
|
|
|
if (dynamic_cast<TagLib::MPEG::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_MPEG;
|
|
|
|
if (dynamic_cast<TagLib::MP4::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_MP4;
|
|
|
|
if (dynamic_cast<TagLib::ASF::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_ASF;
|
|
|
|
if (dynamic_cast<TagLib::RIFF::AIFF::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_AIFF;
|
|
|
|
if (dynamic_cast<TagLib::MPC::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_MPC;
|
|
|
|
if (dynamic_cast<TagLib::TrueAudio::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_TRUEAUDIO;
|
|
|
|
if (dynamic_cast<TagLib::APE::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_APE;
|
2020-07-07 23:44:04 +02:00
|
|
|
#ifdef HAVE_TAGLIB_DSFFILE
|
2019-03-22 23:13:49 +01:00
|
|
|
if (dynamic_cast<TagLib::DSF::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_DSF;
|
2020-07-07 23:44:04 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_TAGLIB_DSDIFFFILE
|
2019-03-22 23:13:49 +01:00
|
|
|
if (dynamic_cast<TagLib::DSDIFF::File*>(fileref->file())) return pb::tagreader::SongMetadata_FileType_DSDIFF;
|
2020-07-07 23:44:04 +02:00
|
|
|
#endif
|
2019-03-22 23:13:49 +01:00
|
|
|
|
|
|
|
return pb::tagreader::SongMetadata_FileType_UNKNOWN;
|
|
|
|
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *song) const {
|
2018-09-02 01:40:14 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
const QByteArray url(QUrl::fromLocalFile(filename).toEncoded());
|
|
|
|
const QFileInfo info(filename);
|
|
|
|
|
|
|
|
qLog(Debug) << "Reading tags from" << filename;
|
|
|
|
|
|
|
|
song->set_basefilename(DataCommaSizeFromQString(info.fileName()));
|
|
|
|
song->set_url(url.constData(), url.size());
|
|
|
|
song->set_filesize(info.size());
|
2020-07-18 03:53:30 +02:00
|
|
|
song->set_mtime(info.lastModified().toSecsSinceEpoch());
|
2019-07-08 22:54:13 +02:00
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
|
2020-07-18 03:53:30 +02:00
|
|
|
song->set_ctime(info.birthTime().isValid() ? info.birthTime().toSecsSinceEpoch() : info.lastModified().toSecsSinceEpoch());
|
2019-07-08 22:54:13 +02:00
|
|
|
#else
|
2020-07-18 03:53:30 +02:00
|
|
|
song->set_ctime(info.created().toSecsSinceEpoch());
|
2019-07-08 22:54:13 +02:00
|
|
|
#endif
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
std::unique_ptr<TagLib::FileRef> fileref(factory_->GetFileRef(filename));
|
|
|
|
if (fileref->isNull()) {
|
2018-09-02 01:40:14 +02:00
|
|
|
qLog(Info) << "TagLib hasn't been able to read" << filename << "file";
|
2018-02-27 18:06:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-09-02 01:40:14 +02:00
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
song->set_filetype(GuessFileType(fileref.get()));
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (fileref->audioProperties()) {
|
|
|
|
song->set_bitrate(fileref->audioProperties()->bitrate());
|
|
|
|
song->set_samplerate(fileref->audioProperties()->sampleRate());
|
2020-06-14 16:08:18 +02:00
|
|
|
song->set_length_nanosec(fileref->audioProperties()->lengthInMilliseconds() * kNsecPerMsec);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TagLib::Tag *tag = fileref->tag();
|
|
|
|
if (tag) {
|
|
|
|
Decode(tag->title(), nullptr, song->mutable_title());
|
|
|
|
Decode(tag->artist(), nullptr, song->mutable_artist()); // TPE1
|
|
|
|
Decode(tag->album(), nullptr, song->mutable_album());
|
|
|
|
Decode(tag->genre(), nullptr, song->mutable_genre());
|
|
|
|
song->set_year(tag->year());
|
|
|
|
song->set_track(tag->track());
|
|
|
|
song->set_valid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString disc;
|
|
|
|
QString compilation;
|
2018-09-06 20:04:29 +02:00
|
|
|
QString lyrics;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Handle all the files which have VorbisComments (Ogg, OPUS, ...) in the same way;
|
2018-09-02 01:40:14 +02:00
|
|
|
// apart, so we keep specific behavior for some formats by adding another "else if" block below.
|
2020-04-23 21:08:28 +02:00
|
|
|
if (TagLib::Ogg::XiphComment *tag_ogg = dynamic_cast<TagLib::Ogg::XiphComment*>(fileref->file()->tag())) {
|
|
|
|
ParseOggTag(tag_ogg->fieldListMap(), nullptr, &disc, &compilation, song);
|
|
|
|
if (!tag_ogg->pictureList().isEmpty()) {
|
2019-02-02 12:39:11 +01:00
|
|
|
song->set_art_automatic(kEmbeddedCover);
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
if (TagLib::FLAC::File *file_flac = dynamic_cast<TagLib::FLAC::File *>(fileref->file())) {
|
2018-09-06 20:04:29 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
song->set_bitdepth(file_flac->audioProperties()->bitsPerSample());
|
2018-09-06 20:04:29 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
if (file_flac->xiphComment()) {
|
|
|
|
ParseOggTag(file_flac->xiphComment()->fieldListMap(), nullptr, &disc, &compilation, song);
|
|
|
|
if (!file_flac->pictureList().isEmpty()) {
|
2018-09-06 20:04:29 +02:00
|
|
|
song->set_art_automatic(kEmbeddedCover);
|
|
|
|
}
|
|
|
|
}
|
2018-09-06 20:49:05 +02:00
|
|
|
if (tag) Decode(tag->comment(), nullptr, song->mutable_comment());
|
2018-09-06 20:04:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::WavPack::File *file_wavpack = dynamic_cast<TagLib::WavPack::File *>(fileref->file())) {
|
|
|
|
song->set_bitdepth(file_wavpack->audioProperties()->bitsPerSample());
|
2020-10-21 20:27:43 +02:00
|
|
|
if (file_wavpack->APETag()) {
|
2020-04-23 21:08:28 +02:00
|
|
|
ParseAPETag(file_wavpack->APETag()->itemListMap(), nullptr, &disc, &compilation, song);
|
2019-03-22 23:13:49 +01:00
|
|
|
}
|
|
|
|
if (tag) Decode(tag->comment(), nullptr, song->mutable_comment());
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::APE::File *file_ape = dynamic_cast<TagLib::APE::File*>(fileref->file())) {
|
2020-10-21 20:27:43 +02:00
|
|
|
if (file_ape->APETag()) {
|
2020-04-23 21:08:28 +02:00
|
|
|
ParseAPETag(file_ape->APETag()->itemListMap(), nullptr, &disc, &compilation, song);
|
2019-03-22 23:13:49 +01:00
|
|
|
}
|
2020-04-23 21:08:28 +02:00
|
|
|
song->set_bitdepth(file_ape->audioProperties()->bitsPerSample());
|
2019-03-22 23:13:49 +01:00
|
|
|
if (tag) Decode(tag->comment(), nullptr, song->mutable_comment());
|
2018-09-06 20:04:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::MPEG::File *file_mpeg = dynamic_cast<TagLib::MPEG::File*>(fileref->file())) {
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
if (file_mpeg->ID3v2Tag()) {
|
|
|
|
const TagLib::ID3v2::FrameListMap &map = file_mpeg->ID3v2Tag()->frameListMap();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (!map["TPOS"].isEmpty()) disc = TStringToQString(map["TPOS"].front()->toString()).trimmed();
|
|
|
|
if (!map["TCOM"].isEmpty()) Decode(map["TCOM"].front()->toString(), nullptr, song->mutable_composer());
|
|
|
|
|
|
|
|
// content group
|
|
|
|
if (!map["TIT1"].isEmpty()) Decode(map["TIT1"].front()->toString(), nullptr, song->mutable_grouping());
|
|
|
|
|
|
|
|
// ID3v2: lead performer/soloist
|
|
|
|
if (!map["TPE1"].isEmpty()) Decode(map["TPE1"].front()->toString(), nullptr, song->mutable_performer());
|
|
|
|
|
|
|
|
// original artist/performer
|
|
|
|
if (!map["TOPE"].isEmpty()) Decode(map["TOPE"].front()->toString(), nullptr, song->mutable_performer());
|
|
|
|
|
|
|
|
// Skip TPE1 (which is the artist) here because we already fetched it
|
|
|
|
|
|
|
|
|
|
|
|
// non-standard: Apple, Microsoft
|
|
|
|
if (!map["TPE2"].isEmpty()) Decode(map["TPE2"].front()->toString(), nullptr, song->mutable_albumartist());
|
|
|
|
|
|
|
|
if (!map["TCMP"].isEmpty()) compilation = TStringToQString(map["TCMP"].front()->toString()).trimmed();
|
|
|
|
|
|
|
|
if (!map["TDOR"].isEmpty()) { song->set_originalyear(map["TDOR"].front()->toString().substr(0, 4).toInt()); }
|
|
|
|
else if (!map["TORY"].isEmpty()) {
|
|
|
|
song->set_originalyear(map["TORY"].front()->toString().substr(0, 4).toInt());
|
|
|
|
}
|
|
|
|
|
2019-07-08 22:01:34 +02:00
|
|
|
if (!map["USLT"].isEmpty()) {
|
|
|
|
Decode(map["USLT"].front()->toString(), nullptr, song->mutable_lyrics());
|
|
|
|
}
|
|
|
|
else if (!map["SYLT"].isEmpty()) {
|
|
|
|
Decode(map["SYLT"].front()->toString(), nullptr, song->mutable_lyrics());
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (!map["APIC"].isEmpty()) song->set_art_automatic(kEmbeddedCover);
|
|
|
|
|
|
|
|
// Find a suitable comment tag. For now we ignore iTunNORM comments.
|
2019-11-15 00:23:06 +01:00
|
|
|
for (uint i = 0; i < map["COMM"].size(); ++i) {
|
2018-02-27 18:06:05 +01:00
|
|
|
const TagLib::ID3v2::CommentsFrame *frame = dynamic_cast<const TagLib::ID3v2::CommentsFrame*>(map["COMM"][i]);
|
|
|
|
|
|
|
|
if (frame && TStringToQString(frame->description()) != "iTunNORM") {
|
|
|
|
Decode(frame->text(), nullptr, song->mutable_comment());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::MP4::File *file_mp4 = dynamic_cast<TagLib::MP4::File*>(fileref->file())) {
|
2018-09-06 20:04:29 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
song->set_bitdepth(file_mp4->audioProperties()->bitsPerSample());
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
if (file_mp4->tag()) {
|
|
|
|
TagLib::MP4::Tag *mp4_tag = file_mp4->tag();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Find album artists
|
2019-09-20 18:07:43 +02:00
|
|
|
if (mp4_tag->item("aART").isValid()) {
|
|
|
|
TagLib::StringList album_artists = mp4_tag->item("aART").toStringList();
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!album_artists.isEmpty()) {
|
|
|
|
Decode(album_artists.front(), nullptr, song->mutable_albumartist());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find album cover art
|
2019-09-20 18:07:43 +02:00
|
|
|
if (mp4_tag->item("covr").isValid()) {
|
2018-02-27 18:06:05 +01:00
|
|
|
song->set_art_automatic(kEmbeddedCover);
|
|
|
|
}
|
|
|
|
|
2019-09-20 18:07:43 +02:00
|
|
|
if (mp4_tag->item("disk").isValid()) {
|
|
|
|
disc = TStringToQString(TagLib::String::number(mp4_tag->item("disk").toIntPair().first));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-20 18:07:43 +02:00
|
|
|
if (mp4_tag->item("\251wrt").isValid()) {
|
|
|
|
Decode(mp4_tag->item("\251wrt").toStringList().toString(", "), nullptr, song->mutable_composer());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2019-09-20 18:07:43 +02:00
|
|
|
if (mp4_tag->item("\251grp").isValid()) {
|
|
|
|
Decode(mp4_tag->item("\251grp").toStringList().toString(" "), nullptr, song->mutable_grouping());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2020-08-03 20:44:25 +02:00
|
|
|
if (mp4_tag->item("\251lyr").isValid()) {
|
|
|
|
Decode(mp4_tag->item("\251lyr").toStringList().toString(" "), nullptr, song->mutable_lyrics());
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-20 18:07:43 +02:00
|
|
|
if (mp4_tag->item(kMP4_OriginalYear_ID).isValid()) {
|
|
|
|
song->set_originalyear(TStringToQString(mp4_tag->item(kMP4_OriginalYear_ID).toStringList().toString('\n')).left(4).toInt());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Decode(mp4_tag->comment(), nullptr, song->mutable_comment());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::ASF::File *file_asf = dynamic_cast<TagLib::ASF::File*>(fileref->file())) {
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
song->set_bitdepth(file_asf->audioProperties()->bitsPerSample());
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2020-08-03 21:03:14 +02:00
|
|
|
if (file_asf->tag()) {
|
|
|
|
Decode(file_asf->tag()->comment(), nullptr, song->mutable_comment());
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
const TagLib::ASF::AttributeListMap &attributes_map = file_asf->tag()->attributeListMap();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (attributes_map.contains(kASF_OriginalDate_ID)) {
|
2018-09-02 01:40:14 +02:00
|
|
|
const TagLib::ASF::AttributeList &attributes = attributes_map[kASF_OriginalDate_ID];
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!attributes.isEmpty()) {
|
2018-09-02 01:40:14 +02:00
|
|
|
song->set_originalyear(TStringToQString(attributes.front().toString()).left(4).toInt());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (attributes_map.contains(kASF_OriginalYear_ID)) {
|
2018-09-02 01:40:14 +02:00
|
|
|
const TagLib::ASF::AttributeList &attributes = attributes_map[kASF_OriginalYear_ID];
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!attributes.isEmpty()) {
|
2018-09-02 01:40:14 +02:00
|
|
|
song->set_originalyear(TStringToQString(attributes.front().toString()).left(4).toInt());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 01:40:14 +02:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::MPC::File* file_mpc = dynamic_cast<TagLib::MPC::File*>(fileref->file())) {
|
2020-10-21 20:29:06 +02:00
|
|
|
if (file_mpc->APETag()) {
|
2020-04-23 21:08:28 +02:00
|
|
|
ParseAPETag(file_mpc->APETag()->itemListMap(), nullptr, &disc, &compilation, song);
|
2019-03-22 23:13:49 +01:00
|
|
|
}
|
|
|
|
if (tag) Decode(tag->comment(), nullptr, song->mutable_comment());
|
2019-01-06 00:32:58 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
else if (tag) {
|
|
|
|
Decode(tag->comment(), nullptr, song->mutable_comment());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!disc.isEmpty()) {
|
|
|
|
const int i = disc.indexOf('/');
|
|
|
|
if (i != -1) {
|
|
|
|
// disc.right( i ).toInt() is total number of discs, we don't use this at the moment
|
|
|
|
song->set_disc(disc.left(i).toInt());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
song->set_disc(disc.toInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compilation.isEmpty()) {
|
|
|
|
// well, it wasn't set, but if the artist is VA assume it's a compilation
|
2020-09-03 19:20:11 +02:00
|
|
|
if (QStringFromStdString(song->artist()).toLower() == "various artists" || QStringFromStdString(song->albumartist()).toLower() == "various artists") {
|
2018-02-27 18:06:05 +01:00
|
|
|
song->set_compilation(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
song->set_compilation(compilation.toInt() == 1);
|
|
|
|
}
|
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
if (!lyrics.isEmpty()) song->set_lyrics(lyrics.toStdString());
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Set integer fields to -1 if they're not valid
|
|
|
|
#define SetDefault(field) if (song->field() <= 0) { song->set_##field(-1); }
|
|
|
|
SetDefault(track);
|
|
|
|
SetDefault(disc);
|
|
|
|
SetDefault(year);
|
|
|
|
SetDefault(bitrate);
|
|
|
|
SetDefault(samplerate);
|
|
|
|
SetDefault(bitdepth);
|
|
|
|
SetDefault(lastplayed);
|
|
|
|
#undef SetDefault
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagReader::Decode(const TagLib::String &tag, const QTextCodec *codec, std::string *output) {
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
QString tmp;
|
|
|
|
|
|
|
|
if (codec && tag.isLatin1()) { // Never override UTF-8.
|
|
|
|
const std::string fixed = QString::fromUtf8(tag.toCString(true)).toStdString();
|
|
|
|
tmp = codec->toUnicode(fixed.c_str()).trimmed();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tmp = TStringToQString(tag).trimmed();
|
|
|
|
}
|
|
|
|
|
|
|
|
output->assign(DataCommaSizeFromQString(tmp));
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagReader::Decode(const QString &tag, const QTextCodec *codec, std::string *output) {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (!codec) {
|
|
|
|
output->assign(DataCommaSizeFromQString(tag));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const QString decoded(codec->toUnicode(tag.toUtf8()));
|
|
|
|
output->assign(DataCommaSizeFromQString(decoded));
|
|
|
|
}
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 23:13:49 +01:00
|
|
|
void TagReader::ParseOggTag(const TagLib::Ogg::FieldListMap &map, const QTextCodec *codec, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const {
|
|
|
|
|
|
|
|
if (!map["COMPOSER"].isEmpty()) Decode(map["COMPOSER"].front(), codec, song->mutable_composer());
|
|
|
|
if (!map["PERFORMER"].isEmpty()) Decode(map["PERFORMER"].front(), codec, song->mutable_performer());
|
|
|
|
if (!map["CONTENT GROUP"].isEmpty()) Decode(map["CONTENT GROUP"].front(), codec, song->mutable_grouping());
|
|
|
|
|
|
|
|
if (!map["ALBUMARTIST"].isEmpty()) Decode(map["ALBUMARTIST"].front(), codec, song->mutable_albumartist());
|
|
|
|
else if (!map["ALBUM ARTIST"].isEmpty()) Decode(map["ALBUM ARTIST"].front(), codec, song->mutable_albumartist());
|
|
|
|
|
|
|
|
if (!map["ORIGINALDATE"].isEmpty()) song->set_originalyear(TStringToQString(map["ORIGINALDATE"].front()).left(4).toInt());
|
|
|
|
else if (!map["ORIGINALYEAR"].isEmpty()) song->set_originalyear(TStringToQString(map["ORIGINALYEAR"].front()).toInt());
|
|
|
|
|
|
|
|
if (!map["DISCNUMBER"].isEmpty()) *disc = TStringToQString( map["DISCNUMBER"].front() ).trimmed();
|
|
|
|
if (!map["COMPILATION"].isEmpty()) *compilation = TStringToQString( map["COMPILATION"].front() ).trimmed();
|
|
|
|
if (!map["COVERART"].isEmpty()) song->set_art_automatic(kEmbeddedCover);
|
|
|
|
if (!map["METADATA_BLOCK_PICTURE"].isEmpty()) song->set_art_automatic(kEmbeddedCover);
|
|
|
|
|
|
|
|
if (!map["FMPS_PLAYCOUNT"].isEmpty() && song->playcount() <= 0) song->set_playcount(TStringToQString( map["FMPS_PLAYCOUNT"].front() ).trimmed().toFloat());
|
|
|
|
|
|
|
|
if (!map["LYRICS"].isEmpty()) Decode(map["LYRICS"].front(), codec, song->mutable_lyrics());
|
|
|
|
else if (!map["UNSYNCEDLYRICS"].isEmpty()) Decode(map["UNSYNCEDLYRICS"].front(), codec, song->mutable_lyrics());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagReader::ParseAPETag(const TagLib::APE::ItemListMap &map, const QTextCodec *codec, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const {
|
|
|
|
|
2019-09-15 20:27:32 +02:00
|
|
|
Q_UNUSED(codec);
|
|
|
|
|
2019-03-22 23:13:49 +01:00
|
|
|
TagLib::APE::ItemListMap::ConstIterator it = map.find("ALBUM ARTIST");
|
|
|
|
if (it != map.end()) {
|
2020-06-26 23:30:30 +02:00
|
|
|
TagLib::StringList album_artists = it->second.values();
|
2019-03-22 23:13:49 +01:00
|
|
|
if (!album_artists.isEmpty()) {
|
|
|
|
Decode(album_artists.front(), nullptr, song->mutable_albumartist());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map.find("COVER ART (FRONT)") != map.end()) song->set_art_automatic(kEmbeddedCover);
|
|
|
|
if (map.contains("COMPILATION")) {
|
|
|
|
*compilation = TStringToQString(TagLib::String::number(map["COMPILATION"].toString().toInt()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("DISC")) {
|
|
|
|
*disc = TStringToQString(TagLib::String::number(map["DISC"].toString().toInt()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("PERFORMER")) {
|
2020-06-26 23:30:30 +02:00
|
|
|
Decode(map["PERFORMER"].values().toString(", "), nullptr, song->mutable_performer());
|
2019-03-22 23:13:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("COMPOSER")) {
|
2020-06-26 23:30:30 +02:00
|
|
|
Decode(map["COMPOSER"].values().toString(", "), nullptr, song->mutable_composer());
|
2019-03-22 23:13:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("GROUPING")) {
|
2020-06-26 23:30:30 +02:00
|
|
|
Decode(map["GROUPING"].values().toString(" "), nullptr, song->mutable_grouping());
|
2019-03-22 23:13:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("LYRICS")) {
|
|
|
|
Decode(map["LYRICS"].toString(), nullptr, song->mutable_lyrics());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (map.contains("FMPS_PLAYCOUNT")) {
|
|
|
|
int playcount = TStringToQString(map["FMPS_PLAYCOUNT"].toString()).toFloat();
|
|
|
|
if (song->playcount() <= 0 && playcount > 0) {
|
|
|
|
song->set_playcount(playcount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
void TagReader::SetVorbisComments(TagLib::Ogg::XiphComment *vorbis_comments, const pb::tagreader::SongMetadata &song) const {
|
|
|
|
|
|
|
|
vorbis_comments->addField("COMPOSER", StdStringToTaglibString(song.composer()), true);
|
|
|
|
vorbis_comments->addField("PERFORMER", StdStringToTaglibString(song.performer()), true);
|
|
|
|
vorbis_comments->addField("CONTENT GROUP", StdStringToTaglibString(song.grouping()), true);
|
|
|
|
vorbis_comments->addField("DISCNUMBER", QStringToTaglibString(song.disc() <= 0 -1 ? QString() : QString::number(song.disc())), true);
|
|
|
|
vorbis_comments->addField("COMPILATION", StdStringToTaglibString(song.compilation() ? "1" : "0"), true);
|
|
|
|
|
|
|
|
// Try to be coherent, the two forms are used but the first one is preferred
|
|
|
|
|
|
|
|
vorbis_comments->addField("ALBUMARTIST", StdStringToTaglibString(song.albumartist()), true);
|
2019-09-20 18:07:43 +02:00
|
|
|
vorbis_comments->removeFields("ALBUM ARTIST");
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
vorbis_comments->addField("LYRICS", StdStringToTaglibString(song.lyrics()), true);
|
2019-09-20 18:07:43 +02:00
|
|
|
vorbis_comments->removeFields("UNSYNCEDLYRICS");
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TagReader::SaveFile(const QString &filename, const pb::tagreader::SongMetadata &song) const {
|
|
|
|
|
2020-10-02 19:27:47 +02:00
|
|
|
if (filename.isEmpty()) return false;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
qLog(Debug) << "Saving tags to" << filename;
|
2018-09-06 20:04:29 +02:00
|
|
|
std::unique_ptr<TagLib::FileRef> fileref(factory_->GetFileRef(filename));;
|
|
|
|
if (!fileref || fileref->isNull()) return false;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
fileref->tag()->setTitle(StdStringToTaglibString(song.title()));
|
|
|
|
fileref->tag()->setArtist(StdStringToTaglibString(song.artist()));
|
|
|
|
fileref->tag()->setAlbum(StdStringToTaglibString(song.album()));
|
|
|
|
fileref->tag()->setGenre(StdStringToTaglibString(song.genre()));
|
|
|
|
fileref->tag()->setComment(StdStringToTaglibString(song.comment()));
|
|
|
|
fileref->tag()->setYear(song.year());
|
|
|
|
fileref->tag()->setTrack(song.track());
|
|
|
|
|
2018-09-06 20:49:05 +02:00
|
|
|
if (TagLib::FLAC::File *file = dynamic_cast<TagLib::FLAC::File*>(fileref->file())) {
|
|
|
|
TagLib::Ogg::XiphComment *tag = file->xiphComment();
|
|
|
|
SetVorbisComments(tag, song);
|
|
|
|
}
|
2019-03-22 23:13:49 +01:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::WavPack::File *file_wavpack = dynamic_cast<TagLib::WavPack::File*>(fileref->file())) {
|
|
|
|
TagLib::APE::Tag *tag = file_wavpack->APETag(true);
|
2018-09-06 20:49:05 +02:00
|
|
|
if (!tag) return false;
|
2019-03-22 23:13:49 +01:00
|
|
|
SaveAPETag(tag, song);
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::APE::File *file_ape = dynamic_cast<TagLib::APE::File*>(fileref->file())) {
|
|
|
|
TagLib::APE::Tag *tag = file_ape->APETag(true);
|
2019-03-22 23:13:49 +01:00
|
|
|
if (!tag) return false;
|
|
|
|
SaveAPETag(tag, song);
|
|
|
|
}
|
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::MPC::File *file_mpc = dynamic_cast<TagLib::MPC::File*>(fileref->file())) {
|
|
|
|
TagLib::APE::Tag *tag = file_mpc->APETag(true);
|
2019-03-22 23:13:49 +01:00
|
|
|
if (!tag) return false;
|
|
|
|
SaveAPETag(tag, song);
|
2018-09-06 20:49:05 +02:00
|
|
|
}
|
2019-03-22 23:13:49 +01:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::MPEG::File *file_mpeg = dynamic_cast<TagLib::MPEG::File*>(fileref->file())) {
|
|
|
|
TagLib::ID3v2::Tag *tag = file_mpeg->ID3v2Tag(true);
|
2018-09-06 20:04:29 +02:00
|
|
|
if (!tag) return false;
|
2018-02-27 18:06:05 +01:00
|
|
|
SetTextFrame("TPOS", song.disc() <= 0 -1 ? QString() : QString::number(song.disc()), tag);
|
|
|
|
SetTextFrame("TCOM", song.composer(), tag);
|
|
|
|
SetTextFrame("TIT1", song.grouping(), tag);
|
|
|
|
SetTextFrame("TOPE", song.performer(), tag);
|
|
|
|
// Skip TPE1 (which is the artist) here because we already set it
|
|
|
|
SetTextFrame("TPE2", song.albumartist(), tag);
|
|
|
|
SetTextFrame("TCMP", std::string(song.compilation() ? "1" : "0"), tag);
|
2018-09-06 20:04:29 +02:00
|
|
|
SetUnsyncLyricsFrame(song.lyrics(), tag);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
2019-03-22 23:13:49 +01:00
|
|
|
|
2020-04-23 21:08:28 +02:00
|
|
|
else if (TagLib::MP4::File *file_mp4 = dynamic_cast<TagLib::MP4::File*>(fileref->file())) {
|
|
|
|
TagLib::MP4::Tag *tag = file_mp4->tag();
|
2019-09-20 18:07:43 +02:00
|
|
|
tag->setItem("disk", TagLib::MP4::Item(song.disc() <= 0 -1 ? 0 : song.disc(), 0));
|
|
|
|
tag->setItem("\251wrt", TagLib::StringList(song.composer().c_str()));
|
|
|
|
tag->setItem("\251grp", TagLib::StringList(song.grouping().c_str()));
|
2020-08-03 20:44:25 +02:00
|
|
|
tag->setItem("\251lyr", TagLib::StringList(song.lyrics().c_str()));
|
2019-09-20 18:07:43 +02:00
|
|
|
tag->setItem("aART", TagLib::StringList(song.albumartist().c_str()));
|
|
|
|
tag->setItem("cpil", TagLib::StringList(song.compilation() ? "1" : "0"));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle all the files which have VorbisComments (Ogg, OPUS, ...) in the same way;
|
2018-05-01 00:41:33 +02:00
|
|
|
// apart, so we keep specific behavior for some formats by adding another "else if" block above.
|
2018-02-27 18:06:05 +01:00
|
|
|
if (TagLib::Ogg::XiphComment *tag = dynamic_cast<TagLib::Ogg::XiphComment*>(fileref->file()->tag())) {
|
|
|
|
SetVorbisComments(tag, song);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = fileref->save();
|
2019-02-02 12:39:11 +01:00
|
|
|
#ifdef Q_OS_LINUX
|
2018-02-27 18:06:05 +01:00
|
|
|
if (ret) {
|
2019-03-22 23:13:49 +01:00
|
|
|
// Linux: inotify doesn't seem to notice the change to the file unless we change the timestamps as well. (this is what touch does)
|
2018-02-27 18:06:05 +01:00
|
|
|
utimensat(0, QFile::encodeName(filename).constData(), nullptr, 0);
|
|
|
|
}
|
2019-02-02 12:39:11 +01:00
|
|
|
#endif // Q_OS_LINUX
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-22 23:13:49 +01:00
|
|
|
void TagReader::SaveAPETag(TagLib::APE::Tag *tag, const pb::tagreader::SongMetadata &song) const {
|
|
|
|
|
|
|
|
tag->setItem("album artist", TagLib::APE::Item("album artist", TagLib::StringList(song.albumartist().c_str())));
|
|
|
|
tag->setItem("disc", TagLib::APE::Item("disc", TagLib::String::number(song.disc() <= 0 - 1 ? 0 : song.disc())));
|
|
|
|
tag->setItem("composer", TagLib::APE::Item("composer", TagLib::StringList(song.composer().c_str())));
|
|
|
|
tag->setItem("grouping", TagLib::APE::Item("grouping", TagLib::StringList(song.grouping().c_str())));
|
|
|
|
tag->setItem("performer", TagLib::APE::Item("performer", TagLib::StringList(song.performer().c_str())));
|
|
|
|
tag->setItem("lyrics", TagLib::APE::Item("lyrics", TagLib::String(song.lyrics())));
|
|
|
|
tag->setItem("compilation", TagLib::APE::Item("compilation", TagLib::StringList(song.compilation() ? "1" : "0")));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
void TagReader::SetUserTextFrame(const QString &description, const QString &value, TagLib::ID3v2::Tag *tag) const {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
const QByteArray descr_utf8(description.toUtf8());
|
|
|
|
const QByteArray value_utf8(value.toUtf8());
|
|
|
|
qLog(Debug) << "Setting FMPSFrame:" << description << ", " << value;
|
|
|
|
SetUserTextFrame(std::string(descr_utf8.constData(), descr_utf8.length()), std::string(value_utf8.constData(), value_utf8.length()), tag);
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagReader::SetUserTextFrame(const std::string &description, const std::string &value, TagLib::ID3v2::Tag *tag) const {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
const TagLib::String t_description = StdStringToTaglibString(description);
|
|
|
|
// Remove the frame if it already exists
|
|
|
|
TagLib::ID3v2::UserTextIdentificationFrame *frame = TagLib::ID3v2::UserTextIdentificationFrame::find(tag, t_description);
|
|
|
|
if (frame) {
|
|
|
|
tag->removeFrame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and add a new frame
|
|
|
|
frame = new TagLib::ID3v2::UserTextIdentificationFrame(TagLib::String::UTF8);
|
|
|
|
|
|
|
|
frame->setDescription(t_description);
|
|
|
|
frame->setText(StdStringToTaglibString(value));
|
|
|
|
tag->addFrame(frame);
|
2018-09-06 20:49:05 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TagReader::SetTextFrame(const char *id, const QString &value, TagLib::ID3v2::Tag *tag) const {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
const QByteArray utf8(value.toUtf8());
|
|
|
|
SetTextFrame(id, std::string(utf8.constData(), utf8.length()), tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TagReader::SetTextFrame(const char *id, const std::string &value, TagLib::ID3v2::Tag *tag) const {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
TagLib::ByteVector id_vector(id);
|
|
|
|
QVector<TagLib::ByteVector> frames_buffer;
|
|
|
|
|
|
|
|
// Store and clear existing frames
|
|
|
|
while (tag->frameListMap().contains(id_vector) && tag->frameListMap()[id_vector].size() != 0) {
|
|
|
|
frames_buffer.push_back(tag->frameListMap()[id_vector].front()->render());
|
|
|
|
tag->removeFrame(tag->frameListMap()[id_vector].front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no frames stored create empty frame
|
|
|
|
if (frames_buffer.isEmpty()) {
|
|
|
|
TagLib::ID3v2::TextIdentificationFrame frame(id_vector, TagLib::String::UTF8);
|
|
|
|
frames_buffer.push_back(frame.render());
|
|
|
|
}
|
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
// Update and add the frames
|
|
|
|
for (int lyrics_index = 0; lyrics_index < frames_buffer.size(); lyrics_index++) {
|
|
|
|
TagLib::ID3v2::TextIdentificationFrame* frame = new TagLib::ID3v2::TextIdentificationFrame(frames_buffer.at(lyrics_index));
|
|
|
|
if (lyrics_index == 0) {
|
|
|
|
frame->setText(StdStringToTaglibString(value));
|
|
|
|
}
|
|
|
|
// add frame takes ownership and clears the memory
|
|
|
|
tag->addFrame(frame);
|
|
|
|
}
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TagReader::IsMediaFile(const QString &filename) const {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
qLog(Debug) << "Checking for valid file" << filename;
|
|
|
|
|
|
|
|
std::unique_ptr<TagLib::FileRef> fileref(factory_->GetFileRef(filename));
|
|
|
|
return !fileref->isNull() && fileref->tag();
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray TagReader::LoadEmbeddedArt(const QString &filename) const {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
if (filename.isEmpty()) return QByteArray();
|
|
|
|
|
|
|
|
qLog(Debug) << "Loading art from" << filename;
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
TagLib::FileRef ref(filename.toStdWString().c_str());
|
|
|
|
#else
|
|
|
|
TagLib::FileRef ref(QFile::encodeName(filename).constData());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ref.isNull() || !ref.file()) return QByteArray();
|
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
// FLAC
|
|
|
|
TagLib::FLAC::File *flac_file = dynamic_cast<TagLib::FLAC::File*>(ref.file());
|
|
|
|
if (flac_file && flac_file->xiphComment()) {
|
|
|
|
TagLib::List<TagLib::FLAC::Picture*> pics = flac_file->pictureList();
|
|
|
|
if (!pics.isEmpty()) {
|
|
|
|
// Use the first picture in the file - this could be made cleverer and
|
|
|
|
// pick the front cover if it's present.
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
std::list<TagLib::FLAC::Picture*>::iterator it = pics.begin();
|
|
|
|
TagLib::FLAC::Picture *picture = *it;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
return QByteArray(picture->data().data(), picture->data().size());
|
|
|
|
}
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 23:13:49 +01:00
|
|
|
// WavPack
|
|
|
|
|
|
|
|
TagLib::WavPack::File *wavpack_file = dynamic_cast<TagLib::WavPack::File*>(ref.file());
|
|
|
|
if (wavpack_file) {
|
|
|
|
return LoadEmbeddedAPEArt(wavpack_file->APETag()->itemListMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// APE
|
|
|
|
|
|
|
|
TagLib::APE::File *ape_file = dynamic_cast<TagLib::APE::File*>(ref.file());
|
|
|
|
if (ape_file) {
|
|
|
|
return LoadEmbeddedAPEArt(ape_file->APETag()->itemListMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
// MPC
|
|
|
|
|
|
|
|
TagLib::MPC::File *mpc_file = dynamic_cast<TagLib::MPC::File*>(ref.file());
|
|
|
|
if (mpc_file) {
|
|
|
|
return LoadEmbeddedAPEArt(mpc_file->APETag()->itemListMap());
|
|
|
|
}
|
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
// Ogg Vorbis / Speex
|
2018-02-27 18:06:05 +01:00
|
|
|
TagLib::Ogg::XiphComment *xiph_comment = dynamic_cast<TagLib::Ogg::XiphComment*>(ref.file()->tag());
|
|
|
|
if (xiph_comment) {
|
|
|
|
TagLib::Ogg::FieldListMap map = xiph_comment->fieldListMap();
|
|
|
|
|
|
|
|
TagLib::List<TagLib::FLAC::Picture*> pics = xiph_comment->pictureList();
|
|
|
|
if (!pics.isEmpty()) {
|
|
|
|
for (auto p : pics) {
|
|
|
|
if (p->type() == TagLib::FLAC::Picture::FrontCover)
|
|
|
|
return QByteArray(p->data().data(), p->data().size());
|
|
|
|
}
|
|
|
|
// If there was no specific front cover, just take the first picture
|
|
|
|
std::list<TagLib::FLAC::Picture*>::iterator it = pics.begin();
|
|
|
|
TagLib::FLAC::Picture *picture = *it;
|
|
|
|
|
|
|
|
return QByteArray(picture->data().data(), picture->data().size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ogg lacks a definitive standard for embedding cover art, but it seems
|
|
|
|
// b64 encoding a field called COVERART is the general convention
|
2019-09-20 18:07:43 +02:00
|
|
|
if (map.contains("COVERART"))
|
|
|
|
return QByteArray::fromBase64(map["COVERART"].toString().toCString());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-09-20 18:07:43 +02:00
|
|
|
return QByteArray();
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
// MP3
|
|
|
|
TagLib::MPEG::File *file = dynamic_cast<TagLib::MPEG::File*>(ref.file());
|
|
|
|
if (file && file->ID3v2Tag()) {
|
|
|
|
TagLib::ID3v2::FrameList apic_frames = file->ID3v2Tag()->frameListMap()["APIC"];
|
|
|
|
if (apic_frames.isEmpty())
|
|
|
|
return QByteArray();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame*>(apic_frames.front());
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-06-15 17:59:02 +02:00
|
|
|
return QByteArray(reinterpret_cast<const char*>(pic->picture().data()), pic->picture().size());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MP4/AAC
|
|
|
|
TagLib::MP4::File *aac_file = dynamic_cast<TagLib::MP4::File*>(ref.file());
|
|
|
|
if (aac_file) {
|
|
|
|
TagLib::MP4::Tag *tag = aac_file->tag();
|
2019-09-20 18:07:43 +02:00
|
|
|
if (tag->item("covr").isValid()) {
|
|
|
|
const TagLib::MP4::CoverArtList &art_list = tag->item("covr").toCoverArtList();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
if (!art_list.isEmpty()) {
|
|
|
|
// Just take the first one for now
|
|
|
|
const TagLib::MP4::CoverArt &art = art_list.front();
|
|
|
|
return QByteArray(art.data().data(), art.data().size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QByteArray();
|
|
|
|
|
|
|
|
}
|
2018-09-06 20:04:29 +02:00
|
|
|
|
2019-03-22 23:13:49 +01:00
|
|
|
QByteArray TagReader::LoadEmbeddedAPEArt(const TagLib::APE::ItemListMap &map) const {
|
|
|
|
|
|
|
|
QByteArray ret;
|
|
|
|
|
|
|
|
TagLib::APE::ItemListMap::ConstIterator it = map.find("COVER ART (FRONT)");
|
|
|
|
if (it != map.end()) {
|
|
|
|
TagLib::ByteVector data = it->second.binaryData();
|
|
|
|
|
|
|
|
int pos = data.find('\0') + 1;
|
2020-06-15 17:59:02 +02:00
|
|
|
if ((pos > 0) && (static_cast<uint>(pos) < data.size())) {
|
2019-03-22 23:13:49 +01:00
|
|
|
ret = QByteArray(data.data() + pos, data.size() - pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 20:04:29 +02:00
|
|
|
void TagReader::SetUnsyncLyricsFrame(const std::string& value, TagLib::ID3v2::Tag* tag) const {
|
|
|
|
|
|
|
|
TagLib::ByteVector id_vector("USLT");
|
|
|
|
QVector<TagLib::ByteVector> frames_buffer;
|
|
|
|
|
|
|
|
// Store and clear existing frames
|
|
|
|
while (tag->frameListMap().contains(id_vector) && tag->frameListMap()[id_vector].size() != 0) {
|
|
|
|
frames_buffer.push_back(tag->frameListMap()[id_vector].front()->render());
|
|
|
|
tag->removeFrame(tag->frameListMap()[id_vector].front());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no frames stored create empty frame
|
|
|
|
if (frames_buffer.isEmpty()) {
|
|
|
|
TagLib::ID3v2::UnsynchronizedLyricsFrame frame(TagLib::String::UTF8);
|
|
|
|
frame.setDescription("Clementine editor");
|
|
|
|
frames_buffer.push_back(frame.render());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update and add the frames
|
|
|
|
for (int lyrics_index = 0; lyrics_index < frames_buffer.size(); lyrics_index++) {
|
|
|
|
TagLib::ID3v2::UnsynchronizedLyricsFrame* frame = new TagLib::ID3v2::UnsynchronizedLyricsFrame(frames_buffer.at(lyrics_index));
|
|
|
|
if (lyrics_index == 0) {
|
|
|
|
frame->setText(StdStringToTaglibString(value));
|
|
|
|
}
|
|
|
|
// add frame takes ownership and clears the memory
|
|
|
|
tag->addFrame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|