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 *****************************************************************************/
26 #include <vlc_playlist.h>
28 #include <vlc_demux.h>
36 #include <uniquefileidentifierframe.h>
37 #if 0 //for artist and album id
38 #include <textidentificationframe.h>
41 static int ReadMeta ( vlc_object_t * );
42 static int DownloadArt ( vlc_object_t * );
43 static int WriteMeta ( vlc_object_t * );
46 set_capability( "meta reader", 1000 );
47 set_callbacks( ReadMeta, NULL );
49 set_capability( "art downloader", 50 );
50 set_callbacks( DownloadArt, NULL );
52 set_capability( "meta writer", 50 );
53 set_callbacks( WriteMeta, NULL );
56 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
58 TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
62 /* Try detecting embedded art */
63 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
65 if( TagLib::MPEG::File *mpeg =
66 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
68 if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
69 vlc_meta_SetArtURL( p_meta, "APIC" );
71 else if( TagLib::FLAC::File *flac =
72 dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
74 if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
75 vlc_meta_SetArtURL( p_meta, "APIC" );
78 /* This needs special additions to taglib */
79 * else if( TagLib::MP4::File *mp4 =
80 dynamic_cast<TagLib::MP4::File *>( f.file() ) )
82 TagLib::MP4::Tag *mp4tag =
83 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
84 if( mp4tag && mp4tag->cover().size() )
85 vlc_meta_SetArtURL( p_meta, "MP4C" );
90 static int ReadMeta( vlc_object_t *p_this )
92 demux_t *p_demux = (demux_t *)p_this;
94 if( !strncmp( p_demux->psz_access, "file", 4 ) )
96 if( !p_demux->p_private )
97 p_demux->p_private = (void*)vlc_meta_New();
98 TagLib::FileRef f( p_demux->psz_path );
99 if( !f.isNull() && f.tag() && !f.tag()->isEmpty() )
101 TagLib::Tag *tag = f.tag();
102 vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
104 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
106 SET( Artist, artist );
108 // SET( Comment, comment );
110 // SET( Year, year ); Gra, this is an int, need to convert
111 // SET( Tracknum , track ); Same
114 if( TagLib::MPEG::File *p_mpeg =
115 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
117 if( p_mpeg->ID3v2Tag() )
119 TagLib::ID3v2::Tag *tag = p_mpeg->ID3v2Tag();
120 TagLib::ID3v2::FrameList list = tag->frameListMap()["UFID"];
121 TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
122 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
123 iter != list.end(); iter++ )
125 p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
126 const char *owner = p_ufid->owner().toCString();
127 if (!strcmp( owner, "http://musicbrainz.org" ))
129 /* ID3v2 UFID contains up to 64 bytes binary data
130 * but in our case it will be a '\0'
131 * terminated string */
132 char *psz_ufid = (char*) malloc( 64 );
135 ( j < p_ufid->identifier().size() ) )
136 psz_ufid[j] = p_ufid->identifier()[j++];
138 vlc_meta_SetTrackID( p_meta, psz_ufid );
142 /* musicbrainz artist and album id: not useful yet */
144 list = tag->frameListMap()["TXXX"];
145 TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
146 for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
147 iter != list.end(); iter++ )
149 p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
150 const char *psz_desc= p_txxx->description().toCString();
151 if( !strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
152 vlc_meta_SetArtistID( p_meta,
153 p_txxx->fieldList().toString().toCString());
154 if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
155 vlc_meta_SetAlbumID( p_meta,
156 p_txxx->fieldList().toString().toCString());
162 DetectImage( f, p_meta );
170 #define SET(a,b) if(b) { \
171 TagLib::String *psz_##a = new TagLib::String( b, \
172 TagLib::String::UTF8 ); \
173 tag->set##a( *psz_##a ); \
177 static int WriteMeta( vlc_object_t *p_this )
179 playlist_t *p_playlist = (playlist_t *)p_this;
180 meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
181 input_item_t *p_item = p_export->p_item;
185 msg_Err( p_this, "Can't save meta data of an empty input" );
189 TagLib::FileRef f( p_export->psz_file );
190 if( !f.isNull() && f.tag() )
192 msg_Dbg( p_this, "Updating metadata for %s", p_export->psz_file );
194 TagLib::Tag *tag = f.tag();
196 SET( Artist, input_item_GetArtist( p_item ) );
198 const char *psz_titlec = ( input_item_GetTitle( p_item ) ?
199 input_item_GetTitle( p_item ) : p_item->psz_name );
200 TagLib::String *psz_title = new TagLib::String( psz_titlec,
201 TagLib::String::UTF8 );
202 tag->setTitle( *psz_title );
205 SET( Album, input_item_GetAlbum( p_item ) );
206 SET( Genre, input_item_GetGenre( p_item ) );
208 if( input_item_GetDate( p_item ) )
209 tag->setYear( atoi( input_item_GetDate( p_item ) ) );
210 if( input_item_GetTrackNum( p_item ) )
211 tag->setTrack( atoi( input_item_GetTrackNum( p_item ) ) );
216 msg_Err( p_this, "File %s can't be opened for tag writing\n",
217 p_export->psz_file );
221 static int DownloadArt( vlc_object_t *p_this )
223 /* We need to be passed the file name
224 * Fetch the thing from the file, save it to the cache folder