1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <vlc_playlist.h>
26 #include <vlc_demux.h>
33 #include <uniquefileidentifierframe.h>
34 #if 0 //for artist and album id
35 #include <textidentificationframe.h>
38 static int ReadMeta ( vlc_object_t * );
39 static int DownloadArt ( vlc_object_t * );
40 static int WriteMeta ( vlc_object_t * );
43 set_capability( "meta reader", 1000 );
44 set_callbacks( ReadMeta, NULL );
46 set_capability( "art downloader", 50 );
47 set_callbacks( DownloadArt, NULL );
49 set_capability( "meta writer", 50 );
50 set_callbacks( WriteMeta, NULL );
53 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
55 TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
59 /* Try detecting embedded art */
60 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
62 if( TagLib::MPEG::File *mpeg =
63 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
65 if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
66 vlc_meta_SetArtURL( p_meta, "APIC" );
68 else if( TagLib::FLAC::File *flac =
69 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
71 if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
72 vlc_meta_SetArtURL( p_meta, "APIC" );
75 /* This needs special additions to taglib */
76 * else if( TagLib::MP4::File *mp4 =
77 dynamic_cast<TagLib::MP4::File *>( f.file() ) )
79 TagLib::MP4::Tag *mp4tag =
80 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
81 if( mp4tag && mp4tag->cover().size() )
82 vlc_meta_SetArtURL( p_meta, "MP4C" );
87 static int ReadMeta( vlc_object_t *p_this )
89 demux_t *p_demux = (demux_t *)p_this;
91 if( !strncmp( p_demux->psz_access, "file", 4 ) )
93 if( !p_demux->p_private )
94 p_demux->p_private = (void*)vlc_meta_New();
95 TagLib::FileRef f( p_demux->psz_path );
96 if( !f.isNull() && f.tag() && !f.tag()->isEmpty() )
98 TagLib::Tag *tag = f.tag();
99 vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
101 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
103 SET( Artist, artist );
105 // SET( Comment, comment );
107 // SET( Year, year ); Gra, this is an int, need to convert
108 // SET( Tracknum , track ); Same
111 if( TagLib::MPEG::File *p_mpeg =
112 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
114 if( p_mpeg->ID3v2Tag() )
116 TagLib::ID3v2::Tag *tag = p_mpeg->ID3v2Tag();
117 TagLib::ID3v2::FrameList list = tag->frameListMap()["UFID"];
118 TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
119 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
120 iter != list.end(); iter++ )
122 p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
123 const char *owner = p_ufid->owner().toCString();
124 if (!strcmp( owner, "http://musicbrainz.org" ))
126 /* ID3v2 UFID contains up to 64 bytes binary data
127 * but in our case it will be a '\0'
128 * terminated string */
129 char *psz_ufid = (char*) malloc( 64 );
132 ( j < p_ufid->identifier().size() ) )
133 psz_ufid[j] = p_ufid->identifier()[j++];
135 vlc_meta_SetTrackID( p_meta, psz_ufid );
139 /* musicbrainz artist and album id: not useful yet */
141 list = tag->frameListMap()["TXXX"];
142 TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
143 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
144 iter != list.end(); iter++ )
146 p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
147 const char *psz_desc= p_txxx->description().toCString();
148 if( !strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
149 vlc_meta_SetArtistID( p_meta,
150 p_txxx->fieldList().toString().toCString());
151 if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
152 vlc_meta_SetAlbumID( p_meta,
153 p_txxx->fieldList().toString().toCString());
159 DetectImage( f, p_meta );
167 static int WriteMeta( vlc_object_t *p_this )
169 playlist_t *p_playlist = (playlist_t *)p_this;
170 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
171 input_item_t *p_item = p_export->p_item;
173 TagLib::FileRef f( p_export->psz_file );
174 if( !f.isNull() && f.tag() )
176 TagLib::Tag *tag = f.tag();
177 tag->setArtist( p_item->p_meta->psz_artist );
184 static int DownloadArt( vlc_object_t *p_this )
186 /* We need to be passed the file name
187 * Fetch the thing from the file, save it to the cache folder