kde-extraapps/thumbnailers/audio/AudioThumbs.cpp

148 lines
5.4 KiB
C++
Raw Normal View History

2015-02-20 18:55:34 +00:00
/***************************************************************************
* This file is part of the AudioThumbs. *
* Copyright (C) 2009 Vytautas Mickus <vmickus@gmail.com> *
* Copyright (C) 2011 Raizner Evgeniy <razrfalcon@gmail.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 "AudioThumbs.h"
#include <QImage>
#include <QFile>
#include <taglib/fileref.h>
#include <taglib/id3v2tag.h>
#include <taglib/mpegfile.h>
#include <taglib/attachedpictureframe.h>
#include <taglib/flacfile.h>
#include <taglib/flacpicture.h>
#include <taglib/mp4file.h>
#include <taglib/mp4properties.h>
#include <taglib/mp4tag.h>
#include <taglib/mp4coverart.h>
#include <taglib/xiphcomment.h>
#include <taglib/xingheader.h>
#include <taglib/oggflacfile.h>
#include <taglib/oggfile.h>
#include <taglib/vorbisfile.h>
#include <kmimetype.h>
#include <kdemacros.h>
2015-02-20 18:55:34 +00:00
extern "C"
{
KDE_EXPORT ThumbCreator *new_creator()
{
return new ATCreator();
2015-02-20 18:55:34 +00:00
}
}
ATCreator::ATCreator()
{
}
bool ATCreator::create(const QString &path, int /*w*/, int /*h*/, QImage &img)
2015-02-20 18:55:34 +00:00
{
bool bRet = false;
KMimeType::Ptr type = KMimeType::findByUrl(KUrl(path));
2015-02-20 18:55:34 +00:00
if (type->is("audio/mpeg")) {
2015-02-20 18:55:34 +00:00
TagLib::MPEG::File mp3File(path.toUtf8());
TagLib::ID3v2::Tag *mp3Tag = mp3File.ID3v2Tag();
if (mp3Tag) {
TagLib::ID3v2::FrameList fList = mp3Tag->frameList("APIC");
TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(fList.front());
if (pic && !pic->picture().isEmpty()) {
img.loadFromData(pic->picture().data(), pic->picture().size());
bRet = true;
}
2015-02-20 18:55:34 +00:00
}
} else if (type->is("audio/flac") || type->is("audio/x-flac")) {
2015-02-20 18:55:34 +00:00
TagLib::FLAC::File ff(path.toUtf8());
TagLib::List<TagLib::FLAC::Picture *> coverList = ff.pictureList();
2015-02-20 18:55:34 +00:00
if (!coverList.isEmpty()) {
TagLib::FLAC::Picture *pic = coverList.front();
if (pic) {
TagLib::ByteVector coverData = pic->data();
img.loadFromData(coverData.data(), coverData.size());
bRet = true;
}
2015-02-20 18:55:34 +00:00
}
} else if (type->is("audio/mp4")) {
2015-02-20 18:55:34 +00:00
TagLib::MP4::File mp4file(path.toUtf8());
TagLib::MP4::Tag *mp4tag = mp4file.tag();
if (mp4tag) {
#if TAGLIB_MAJOR_VERSION >= 2
TagLib::MP4::ItemMap map = mp4tag->itemMap();
for (TagLib::MP4::ItemMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
#else
TagLib::MP4::ItemListMap map = mp4tag->itemListMap();
for (TagLib::MP4::ItemListMap::ConstIterator it = map.begin(); it != map.end(); ++it) {
#endif
TagLib::MP4::CoverArtList coverList = (*it).second.toCoverArtList();
if (!coverList.isEmpty()) {
TagLib::MP4::CoverArt cover = coverList.front();
TagLib::ByteVector coverData = cover.data();
img.loadFromData(coverData.data(), coverData.size());
bRet = true;
break;
}
}
}
} else if (type->is("audio/x-vorbis+ogg") || type->is("audio/ogg")) {
2015-02-20 18:55:34 +00:00
TagLib::Ogg::Vorbis::File file(path.toUtf8());
TagLib::Ogg::XiphComment *oggtag = file.tag();
2015-02-20 18:55:34 +00:00
if (oggtag) {
TagLib::List<TagLib::FLAC::Picture*> picturelist = oggtag->pictureList();
for (int i = 0; i < picturelist.size(); i++) {
// qDebug() << Q_FUNC_INFO << picturelist[i]->code();
switch (picturelist[i]->code()) {
case TagLib::FLAC::Picture::FrontCover:
case TagLib::FLAC::Picture::BackCover:
case TagLib::FLAC::Picture::Media: {
break;
}
default: {
continue;
break;
}
}
img.loadFromData(picturelist[i]->data().data(), picturelist[i]->data().size());
if (!img.isNull()) {
bRet = true;
break;
}
}
2015-02-20 18:55:34 +00:00
}
}
return bRet;
}
ThumbCreator::Flags ATCreator::flags() const
{
return (Flags)(None);
}