1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/***************************************************************************
* This file is part of the KDE KIO-Extras project *
* Copyright (C) 2009 Vytautas Mickus <vmickus@gmail.com> *
* Copyright (C) 2016 Anthony Fieroni <bvbfan@abv.com> *
* *
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
* MA 02110-1301 USA *
***************************************************************************/
#include "audiocreator.h"
#include <QFile>
#include <QImage>
#include <QMimeType>
#include <QMimeDatabase>
#include <taglib/apetag.h>
#include <taglib/mp4tag.h>
#include <taglib/id3v2tag.h>
#include <taglib/fileref.h>
#include <taglib/mp4file.h>
#include <taglib/wavfile.h>
#include <taglib/apefile.h>
#include <taglib/mpcfile.h>
#include <taglib/mpegfile.h>
#include <taglib/aifffile.h>
#include <taglib/flacfile.h>
#include <taglib/wavpackfile.h>
#include <taglib/xiphcomment.h>
#include <taglib/flacpicture.h>
#include <taglib/attachedpictureframe.h>
extern "C"
{
Q_DECL_EXPORT ThumbCreator* new_creator()
{
return new AudioCreator;
}
}
AudioCreator::AudioCreator()
{
}
AudioCreator::~AudioCreator()
{
}
namespace TagLib
{
namespace RIFF
{
namespace AIFF
{
struct FileExt : public File
{
using File::File;
ID3v2::Tag* ID3v2Tag() const
{
return tag();
}
};
}
}
}
template<class T> static
bool parseID3v2Tag(T &file, QImage &img)
{
if (!file.hasID3v2Tag()) {
return false;
}
const auto &map = file.ID3v2Tag()->frameListMap();
if (map["APIC"].isEmpty()) {
return false;
}
auto apicFrame = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(map["APIC"].front());
if (!apicFrame) {
return false;
}
const auto coverData = apicFrame->picture();
img.loadFromData((uchar *)coverData.data(), coverData.size());
return true;
}
template<class T> static
bool parseFlacTag(T &file, QImage &img)
{
const auto pictureList = file.pictureList();
for (const auto &picture : pictureList) {
if (picture->type() != TagLib::FLAC::Picture::FrontCover) {
continue;
}
const auto coverData = picture->data();
img.loadFromData((uchar *)coverData.data(), coverData.size());
return true;
}
return false;
}
template<class T> static
bool parseMP4Tag(T &file, QImage &img)
{
if (!file.hasMP4Tag()) {
return false;
}
const auto &map = file.tag()->itemMap();
for (const auto &coverList : map) {
auto coverArtList = coverList.second.toCoverArtList();
if (coverArtList.isEmpty()) {
continue;
}
const auto coverData = coverArtList[0].data();
img.loadFromData((uchar *)coverData.data(), coverData.size());
return true;
}
return false;
}
template<class T> static
bool parseAPETag(T &file, QImage &img)
{
if (!file.hasAPETag()) {
return false;
}
const auto &map = file.APETag()->itemListMap();
for (const auto &item : map) {
if (item.second.type() != TagLib::APE::Item::Binary) {
continue;
}
const auto coverData = item.second.binaryData();
const auto data = coverData.data();
const auto size = coverData.size();
for (size_t i=0; i<size; ++i) {
if (data[i] == '\0' && (i+1) < size) {
const auto start = data+i+1;
img.loadFromData((uchar *)start, size-(start-data));
return true;
}
}
}
return false;
}
bool AudioCreator::create(const QString &path, int, int, QImage &img)
{
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
if (!type.isValid()) {
return false;
}
if (type.inherits("audio/mpeg")) {
TagLib::MPEG::File file(QFile::encodeName(path).data());
return parseID3v2Tag(file, img) || parseAPETag(file, img);
}
if (type.inherits("audio/x-flac") || type.inherits("audio/flac")) {
TagLib::FLAC::File file(QFile::encodeName(path).data());
return parseFlacTag(file, img) || parseID3v2Tag(file, img);
}
if (type.inherits("audio/mp4") || type.inherits("audio/x-m4a")) {
TagLib::MP4::File file(QFile::encodeName(path).data());
return parseMP4Tag(file, img);
}
if (type.inherits("audio/x-ape")) {
TagLib::APE::File file(QFile::encodeName(path).data());
return parseAPETag(file, img);
}
if (type.inherits("audio/x-wavpack") || type.inherits("audio/x-vw")) {
TagLib::WavPack::File file(QFile::encodeName(path).data());
return parseAPETag(file, img);
}
if (type.inherits("audio/x-musepack")) {
TagLib::MPC::File file(QFile::encodeName(path).data());
return parseAPETag(file, img);
}
if (type.inherits("audio/ogg") || type.inherits("audio/vorbis")) {
TagLib::FileRef fileRef(QFile::encodeName(path).data());
if (fileRef.isNull()) {
return false;
}
auto xiphComment = dynamic_cast<TagLib::Ogg::XiphComment*>(fileRef.tag());
if (!xiphComment || xiphComment->isEmpty()) {
return false;
}
return parseFlacTag(*xiphComment, img);
}
if (type.inherits("audio/x-aiff")) {
TagLib::RIFF::AIFF::FileExt file(QFile::encodeName(path).data());
return parseID3v2Tag(file, img);
}
if (type.inherits("audio/x-wav")) {
TagLib::RIFF::WAV::File file(QFile::encodeName(path).data());
return parseID3v2Tag(file, img);
}
return false;
}
ThumbCreator::Flags AudioCreator::flags() const
{
return (Flags)BlendIcon;
}
|