bool ASFTagHelper::setEmbeddedCover( const QImage &cover ) { QByteArray bytes; QBuffer buffer( &bytes ); buffer.open( QIODevice::WriteOnly ); if( !cover.save( &buffer, "JPEG" ) ) { buffer.close(); return false; } buffer.close(); TagLib::String name = fieldName( Meta::valHasCover ); // remove all covers m_tag->removeItem( name ); // add new cover TagLib::ASF::Picture picture; picture.setPicture( TagLib::ByteVector( bytes.data(), bytes.count() ) ); picture.setType( TagLib::ASF::Picture::FrontCover ); picture.setMimeType( "image/jpeg" ); m_tag->addAttribute( name, TagLib::ASF::Attribute( picture.render() ) ); return true; }
bool ASFTagHelper::hasEmbeddedCover() const { TagLib::ASF::AttributeListMap map = m_tag->attributeListMap(); TagLib::String name = fieldName( Meta::valHasCover ); for( TagLib::ASF::AttributeListMap::ConstIterator it = map.begin(); it != map.end(); ++it ) { if( it->first == name ) { TagLib::ASF::AttributeList coverList = it->second; for( TagLib::ASF::AttributeList::ConstIterator cover = coverList.begin(); cover != coverList.end(); ++cover ) { if( cover->type() != TagLib::ASF::Attribute::BytesType ) continue; TagLib::ASF::Picture pict = cover->toPicture(); if( ( pict.type() == TagLib::ASF::Picture::FrontCover || pict.type() == TagLib::ASF::Picture::Other ) && pict.dataSize() > MIN_COVER_SIZE ) { return true; } } } } return false; }
Meta::FieldHash ASFTagHelper::tags() const { Meta::FieldHash data = TagHelper::tags(); TagLib::ASF::AttributeListMap map = m_tag->attributeListMap(); for( TagLib::ASF::AttributeListMap::ConstIterator it = map.begin(); it != map.end(); ++it ) { if( !it->second.size() ) continue; qint64 field; TagLib::ASF::Attribute value = it->second[0]; QString strValue = TStringToQString( value.toString() ); if( ( field = fieldName( it->first ) ) ) { if( field == Meta::valBpm || field == Meta::valPlaycount ) data.insert( field, value.toUInt() ); else if( field == Meta::valRating ) data.insert( field, qRound( strValue.toFloat() * 10.0 ) ); else if( field == Meta::valScore ) data.insert( field, strValue.toFloat() * 100.0 ); else if( field == Meta::valDiscNr ) data.insert( field, value.toUInt() ); else if( field == Meta::valCompilation ) data.insert( field, value.toBool() ); else if( field == Meta::valHasCover ) { for( TagLib::ASF::AttributeList::ConstIterator cover = it->second.begin(); cover != it->second.end(); ++cover ) { if( cover->type() != TagLib::ASF::Attribute::BytesType ) continue; TagLib::ASF::Picture pict = cover->toPicture(); if( ( pict.type() == TagLib::ASF::Picture::FrontCover || pict.type() == TagLib::ASF::Picture::Other ) && pict.dataSize() > MIN_COVER_SIZE ) { data.insert( field, true ); break; } } } else data.insert( field, strValue ); } else if( it->first == uidFieldName( UIDAFT ) && isValidUID( strValue, UIDAFT ) ) data.insert( Meta::valUniqueId, strValue ); else if( it->first == uidFieldName( UIDMusicBrainz ) && isValidUID( strValue, UIDMusicBrainz ) ) { if( !data.contains( Meta::valUniqueId ) ) // we prefere AFT uids data.insert( Meta::valUniqueId, strValue.prepend( "mb-" ) ); } } return data; }
QImage ASFTagHelper::embeddedCover() const { TagLib::ASF::AttributeListMap map = m_tag->attributeListMap(); TagLib::String name = fieldName( Meta::valHasCover ); TagLib::ASF::Picture cover, otherCover; bool hasCover = false, hasOtherCover = false; for( TagLib::ASF::AttributeListMap::ConstIterator it = map.begin(); it != map.end(); ++it ) { if( it->first == name ) { TagLib::ASF::AttributeList coverList = it->second; for( TagLib::ASF::AttributeList::ConstIterator it = coverList.begin(); it != coverList.end(); ++it ) { if( it->type() != TagLib::ASF::Attribute::BytesType ) continue; TagLib::ASF::Picture pict = it->toPicture(); if( pict.dataSize() < MIN_COVER_SIZE ) continue; if( pict.type() == TagLib::ASF::Picture::FrontCover ) { cover = pict; hasCover = true; } else if( pict.type() == TagLib::ASF::Picture::Other ) { otherCover = pict; hasOtherCover = true; } } } } if( !hasCover && hasOtherCover ) { cover = otherCover; hasCover = true; } if( !hasCover ) return QImage(); return QImage::fromData( ( uchar * ) cover.picture().data(), cover.picture().size() ); }
/* ** Extracts cover art embedded in ASF/WMA files. ** */ bool CCover::ExtractASF(TagLib::ASF::File* file, const std::wstring& target) { const TagLib::ASF::AttributeListMap& attrListMap = file->tag()->attributeListMap(); if (attrListMap.contains("WM/Picture")) { const TagLib::ASF::AttributeList& attrList = attrListMap["WM/Picture"]; if (!attrList.isEmpty()) { // Let's grab the first cover. TODO: Check/loop for correct type TagLib::ASF::Picture wmpic = attrList[0].toPicture(); if (wmpic.isValid()) { return WriteCover(wmpic.picture(), target); } } } return false; }
/* ** Extracts cover art embedded in ASF/WMA files. ** */ bool QCoverArt::ExtractASF(TagLib::ASF::File* file) { const TagLib::ASF::AttributeListMap& attrListMap = file->tag()->attributeListMap(); if (attrListMap.contains("WM/Picture")) { const TagLib::ASF::AttributeList& attrList = attrListMap["WM/Picture"]; if (!attrList.isEmpty()) { // Let's grab the first cover. TODO: Check/loop for correct type TagLib::ASF::Picture wmpic = attrList[0].toPicture(); if (wmpic.isValid()) { img.loadFromData((const unsigned char*)wmpic.picture().data(), (int)wmpic.picture().size()); return true; } } } return false; }