bool CoverUtils::coverFromMP4(QString filename, Album *album) { TagLib::MP4::File f((TagLib::FileName)filename.toUtf8()); if (!f.isValid()) return false; TagLib::MP4::Tag *tag = static_cast<TagLib::MP4::Tag *>(f.tag()); if (!tag) return false; TagLib::MP4::ItemListMap itemsListMap = tag->itemListMap(); TagLib::MP4::Item coverItem = itemsListMap["covr"]; TagLib::MP4::CoverArtList coverArtList = coverItem.toCoverArtList(); TagLib::MP4::CoverArt coverArt = coverArtList.front(); QImage image; image.loadFromData((const uchar *) coverArt.data().data(), coverArt.data().size()); if (!isAcceptableImage(image)) return false; qDebug() << "Cover from MP4!"; return saveImage(image, album); }
QString FileHelper::extractMp4Feature(const QString &featureToExtract) const { QString feature; if (TagLib::MP4::File *mp4File = static_cast<TagLib::MP4::File*>(_file)) { if (mp4File->tag()) { TagLib::MP4::ItemListMap &items = mp4File->tag()->itemListMap(); if (items.contains(featureToExtract.toStdString().data())) { TagLib::MP4::Item item = items[featureToExtract.toStdString().data()]; TagLib::StringList list = item.toStringList(); if (list.size() > 0) { feature = list[0].toCString(true); } /*for (uint i = 0; i < list.size(); i++) { TagLib::String s = list[i]; qDebug() << Q_FUNC_INFO << s.toCString(true); }*/ } /*for (auto it = items.begin(); it != items.end(); ++it) { qDebug() << Q_FUNC_INFO << QString(it->first.toCString(false)); }*/ } } return feature; }
void LocalSong::setInfo(const QString &filePath) { QFileInfo fileInfo(filePath); fileExtension = fileInfo.completeSuffix(); QString singer=""; QString sname=""; QString album=""; QImage covpic; if(fileExtension == "m4a") { TagLib::MP4::File *mp4File = new TagLib::MP4::File( QFile::encodeName(filePath).constData()); if(false==mp4File->isOpen()) return; sname=QString(mp4File->tag()->title().toCString(true)); singer=QString(mp4File->tag()->artist().toCString(true)); album=QString(mp4File->tag()->album().toCString(true)); TagLib::MP4::ItemListMap itemListMap = mp4File->tag()->itemListMap(); TagLib::MP4::Item albumArtItem = itemListMap["covr"]; TagLib::MP4::CoverArtList albumArtList = albumArtItem.toCoverArtList(); TagLib::MP4::CoverArt albumArt = albumArtList.front(); QImage cover; cover.loadFromData((const uchar *) albumArt.data().data(), albumArt.data().size()); covpic = cover.scaled( 50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if(!covpic.isNull()) { qDebug()<<"读取M4A封面信息成功"; }else { covpic.load(":/image/image/playIcon.png"); qDebug()<<"读取音乐封面信息失败"; } } else if(fileExtension == "mp3") { TagLib::MPEG::File *mpegFile = new TagLib::MPEG::File( QFile::encodeName(filePath).constData()); if(false==mpegFile->isOpen()) return; else { sname=QString(mpegFile->tag()->title().toCString(true)); singer=QString(mpegFile->tag()->artist().toCString(true)); album=QString(mpegFile->tag()->album().toCString(true)); auto tag = mpegFile->ID3v2Tag(false); auto list = tag->frameListMap()["APIC"]; if(!list.isEmpty()) { auto frame = list.front(); auto pic = reinterpret_cast<TagLib::ID3v2::AttachedPictureFrame *>(frame); if(pic && !pic->picture().isNull()) { QImage cover; if(cover.loadFromData(QByteArray::fromRawData(pic->picture().data(), pic->picture().size()))) { covpic = cover.scaled( 50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); qDebug()<<"读取MP3封面信息成功"; } } } else { covpic.load(":/image/image/playIcon.png"); qDebug()<<"读取音乐封面信息失败"; } } } singerlist.append(singer); snamelist.append(sname); albumlist.append(album); covPiclist.append(covpic); int covCount=covPiclist.length(); QString covpicpath=QDir::tempPath()+QString("/%1.png").arg(covCount); covpic.save(covpicpath); QUrl covpicUrl=QUrl::fromLocalFile(covpicpath); qDebug()<<"歌名为"<<sname; qDebug()<<"歌手为"<<singer; qDebug()<<"专辑为"<<album; qDebug()<<"URL为"<<covpicUrl; emit setSongInfo(singer,sname,album,covpicUrl); return; }
QString TagLibMetadata::getGrouping() const{ if(f == NULL || !f->isValid()) return ""; TagLib::MPEG::File* fileTestMpeg = dynamic_cast<TagLib::MPEG::File*>(f); if(fileTestMpeg != NULL){ TagLib::ID3v2::Tag* tagTestId3v2 = fileTestMpeg->ID3v2Tag(); if(tagTestId3v2 != NULL){ TagLib::ID3v2::FrameList l = tagTestId3v2->frameListMap()["TIT1"]; if(!l.isEmpty()){ TagLib::String out = l.front()->toString(); return QString::fromUtf8((out.toCString())); } return ""; } TagLib::ID3v1::Tag* tagTestId3v1 = fileTestMpeg->ID3v1Tag(); if(tagTestId3v1 != NULL){ #ifdef Q_OS_WIN qDebug("ID3v1 does not support the Grouping tag"); #else qDebug("ID3v1 does not support the Grouping tag (%s)",f->name()); #endif return "N/A"; } } TagLib::RIFF::AIFF::File* fileTestAiff = dynamic_cast<TagLib::RIFF::AIFF::File*>(f); if(fileTestAiff != NULL){ TagLib::ID3v2::Tag* tagTestId3v2 = fileTestAiff->tag(); if(tagTestId3v2 != NULL){ TagLib::ID3v2::FrameList l = tagTestId3v2->frameListMap()["TIT1"]; if(!l.isEmpty()){ TagLib::String out = l.front()->toString(); return QString::fromUtf8((out.toCString())); } return ""; } } TagLib::RIFF::WAV::File* fileTestWav = dynamic_cast<TagLib::RIFF::WAV::File*>(f); if(fileTestWav != NULL){ TagLib::ID3v2::Tag* tagTestId3v2 = fileTestWav->tag(); if(tagTestId3v2 != NULL){ TagLib::ID3v2::FrameList l = tagTestId3v2->frameListMap()["TIT1"]; if(!l.isEmpty()){ TagLib::String out = l.front()->toString(); return QString::fromUtf8((out.toCString())); } return ""; } } TagLib::MP4::Tag* tagTestMp4 = dynamic_cast<TagLib::MP4::Tag*>(f->tag()); if(tagTestMp4 != NULL){ TagLib::MP4::Item m = tagTestMp4->itemListMap()["\251grp"]; if(m.isValid()){ TagLib::String out = m.toStringList().front(); return QString::fromUtf8((out.toCString())); } return ""; } TagLib::ASF::Tag* tagTestAsf = dynamic_cast<TagLib::ASF::Tag*>(f->tag()); if(tagTestAsf != NULL){ TagLib::ASF::AttributeList l = tagTestAsf->attributeListMap()["WM/ContentGroupDescription"]; if(!l.isEmpty()){ TagLib::String out = l.front().toString(); return QString::fromUtf8((out.toCString())); } return ""; } TagLib::APE::Tag* tagTestApe = dynamic_cast<TagLib::APE::Tag*>(f->tag()); if(tagTestApe != NULL){ TagLib::APE::Item m = tagTestApe->itemListMap()["Grouping"]; if(!m.isEmpty()){ TagLib::String out = m.toStringList().front(); return QString::fromUtf8((out.toCString())); } return ""; } #ifdef Q_OS_WIN qDebug("Grouping tag read failed all tests"); #else qDebug("Grouping tag read failed all tests on %s",f->name()); #endif return "N/A"; }