]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
Uses TagLib class to represent UTF8 strings when writing metadata
[vlc] / modules / meta_engine / taglib.cpp
1 /*****************************************************************************
2  * taglib.cpp: Taglib tag parser/writer
3  *****************************************************************************
4  * Copyright (C) 2003-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 #include <stdlib.h>
24
25 #include <vlc/vlc.h>
26 #include <vlc_playlist.h>
27 #include <vlc_meta.h>
28 #include <vlc_demux.h>
29
30 #include <fileref.h>
31 #include <tag.h>
32 #include <tstring.h>
33 #include <id3v2tag.h>
34 #include <mpegfile.h>
35 #include <flacfile.h>
36 #include <uniquefileidentifierframe.h>
37 #if 0 //for artist and album id
38 #include <textidentificationframe.h>
39 #endif
40
41 static int  ReadMeta    ( vlc_object_t * );
42 static int  DownloadArt ( vlc_object_t * );
43 static int  WriteMeta   ( vlc_object_t * );
44
45 vlc_module_begin();
46     set_capability( "meta reader", 1000 );
47     set_callbacks( ReadMeta, NULL );
48     add_submodule();
49         set_capability( "art downloader", 50 );
50         set_callbacks( DownloadArt, NULL );
51     add_submodule();
52         set_capability( "meta writer", 50 );
53         set_callbacks( WriteMeta, NULL );
54 vlc_module_end();
55
56 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
57 {
58     TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
59     return !l.isEmpty();
60 }
61
62 /* Try detecting embedded art */
63 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
64 {
65     if( TagLib::MPEG::File *mpeg =
66                dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
67     {
68         if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
69             vlc_meta_SetArtURL( p_meta, "APIC" );
70     }
71     else if( TagLib::FLAC::File *flac =
72              dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
73     {
74         if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
75             vlc_meta_SetArtURL( p_meta, "APIC" );
76     }
77 #if 0
78 /* This needs special additions to taglib */
79  * else if( TagLib::MP4::File *mp4 =
80                dynamic_cast<TagLib::MP4::File *>( f.file() ) )
81     {
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" );
86     }
87 #endif
88 }
89
90 static int ReadMeta( vlc_object_t *p_this )
91 {
92     demux_t *p_demux = (demux_t *)p_this;
93
94     if( !strncmp( p_demux->psz_access, "file", 4 ) )
95     {
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() )
100         {
101             TagLib::Tag *tag = f.tag();
102             vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
103
104 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
105             SET( Title, title );
106             SET( Artist, artist );
107             SET( Album, album );
108 //            SET( Comment, comment );
109             SET( Genre, genre );
110 //            SET( Year, year ); Gra, this is an int, need to convert
111 //            SET( Tracknum , track ); Same
112 #undef SET
113
114             if( TagLib::MPEG::File *p_mpeg =
115                 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
116             {
117                 if( p_mpeg->ID3v2Tag() )
118                 {
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++ )
124                     {
125                         p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
126                         const char *owner = p_ufid->owner().toCString();
127                         if (!strcmp( owner, "http://musicbrainz.org" ))
128                         {
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 );
133                             int j = 0;
134                             while( ( j < 63 ) &&
135                                     ( j < p_ufid->identifier().size() ) )
136                                 psz_ufid[j] = p_ufid->identifier()[j++];
137                             psz_ufid[j] = '\0';
138                             vlc_meta_SetTrackID( p_meta, psz_ufid );
139                             free( psz_ufid );
140                         }
141                     }
142                     /* musicbrainz artist and album id: not useful yet */
143 #if 0
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++ )
148                     {
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());
157                     }
158 #endif
159                 }
160             }
161
162             DetectImage( f, p_meta );
163
164             return VLC_SUCCESS;
165         }
166     }
167     return VLC_EGENERIC;
168 }
169
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 ); \
174         delete psz_##a; \
175     }
176
177 static int WriteMeta( vlc_object_t *p_this )
178 {
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;
182     
183     if( p_item == NULL )
184     {
185         msg_Err( p_this, "Can't save meta data of an empty input" );
186         return VLC_EGENERIC;
187     }
188
189     TagLib::FileRef f( p_export->psz_file );
190     if( !f.isNull() && f.tag() )
191     {
192         msg_Dbg( p_this, "Updating metadata for %s", p_export->psz_file );
193
194         TagLib::Tag *tag = f.tag();
195
196         SET( Artist, p_item->p_meta->psz_artist );
197
198         char *psz_titlec = ( p_item->p_meta->psz_title ?
199             p_item->p_meta->psz_title : p_item->psz_name );
200         TagLib::String *psz_title = new TagLib::String( psz_titlec,
201             TagLib::String::UTF8 );
202         tag->setTitle( *psz_title );
203         delete psz_title;
204
205         SET( Album, p_item->p_meta->psz_album );
206         SET( Genre, p_item->p_meta->psz_genre );
207
208         if( p_item->p_meta->psz_date )
209             tag->setYear( atoi( p_item->p_meta->psz_date ) );
210         if( p_item->p_meta->psz_tracknum )
211             tag->setTrack( atoi( p_item->p_meta->psz_tracknum ) );
212
213         f.save();
214         return VLC_SUCCESS;
215     }
216     msg_Err( p_this, "File %s can't be opened for tag writing\n",
217         p_export->psz_file );
218     return VLC_EGENERIC;
219 }
220
221 static int DownloadArt( vlc_object_t *p_this )
222 {
223     /* We need to be passed the file name
224      * Fetch the thing from the file, save it to the cache folder
225      */
226     return VLC_EGENERIC;
227 }