]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
Prevents segfault if no file are in the playlist (thanks j-b)
[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 <id3v2tag.h>
33 #include <mpegfile.h>
34 #include <flacfile.h>
35 #include <uniquefileidentifierframe.h>
36 #if 0 //for artist and album id
37 #include <textidentificationframe.h>
38 #endif
39
40 static int  ReadMeta    ( vlc_object_t * );
41 static int  DownloadArt ( vlc_object_t * );
42 static int  WriteMeta   ( vlc_object_t * );
43
44 vlc_module_begin();
45     set_capability( "meta reader", 1000 );
46     set_callbacks( ReadMeta, NULL );
47     add_submodule();
48         set_capability( "art downloader", 50 );
49         set_callbacks( DownloadArt, NULL );
50     add_submodule();
51         set_capability( "meta writer", 50 );
52         set_callbacks( WriteMeta, NULL );
53 vlc_module_end();
54
55 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
56 {
57     TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
58     return !l.isEmpty();
59 }
60
61 /* Try detecting embedded art */
62 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
63 {
64     if( TagLib::MPEG::File *mpeg =
65                dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
66     {
67         if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
68             vlc_meta_SetArtURL( p_meta, "APIC" );
69     }
70     else if( TagLib::FLAC::File *flac =
71              dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
72     {
73         if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
74             vlc_meta_SetArtURL( p_meta, "APIC" );
75     }
76 #if 0
77 /* This needs special additions to taglib */
78  * else if( TagLib::MP4::File *mp4 =
79                dynamic_cast<TagLib::MP4::File *>( f.file() ) )
80     {
81         TagLib::MP4::Tag *mp4tag =
82                 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
83         if( mp4tag && mp4tag->cover().size() )
84             vlc_meta_SetArtURL( p_meta, "MP4C" );
85     }
86 #endif
87 }
88
89 static int ReadMeta( vlc_object_t *p_this )
90 {
91     demux_t *p_demux = (demux_t *)p_this;
92
93     if( !strncmp( p_demux->psz_access, "file", 4 ) )
94     {
95         if( !p_demux->p_private )
96             p_demux->p_private = (void*)vlc_meta_New();
97         TagLib::FileRef f( p_demux->psz_path );
98         if( !f.isNull() && f.tag() && !f.tag()->isEmpty() )
99         {
100             TagLib::Tag *tag = f.tag();
101             vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
102
103 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
104             SET( Title, title );
105             SET( Artist, artist );
106             SET( Album, album );
107 //            SET( Comment, comment );
108             SET( Genre, genre );
109 //            SET( Year, year ); Gra, this is an int, need to convert
110 //            SET( Tracknum , track ); Same
111 #undef SET
112
113             if( TagLib::MPEG::File *p_mpeg =
114                 dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
115             {
116                 if( p_mpeg->ID3v2Tag() )
117                 {
118                     TagLib::ID3v2::Tag *tag = p_mpeg->ID3v2Tag();
119                     TagLib::ID3v2::FrameList list = tag->frameListMap()["UFID"];
120                     TagLib::ID3v2::UniqueFileIdentifierFrame* p_ufid;
121                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
122                             iter != list.end(); iter++ )
123                     {
124                         p_ufid = dynamic_cast<TagLib::ID3v2::UniqueFileIdentifierFrame*>(*iter);
125                         const char *owner = p_ufid->owner().toCString();
126                         if (!strcmp( owner, "http://musicbrainz.org" ))
127                         {
128                             /* ID3v2 UFID contains up to 64 bytes binary data
129                              * but in our case it will be a '\0' 
130                              * terminated string */
131                             char *psz_ufid = (char*) malloc( 64 );
132                             int j = 0;
133                             while( ( j < 63 ) &&
134                                     ( j < p_ufid->identifier().size() ) )
135                                 psz_ufid[j] = p_ufid->identifier()[j++];
136                             psz_ufid[j] = '\0';
137                             vlc_meta_SetTrackID( p_meta, psz_ufid );
138                             free( psz_ufid );
139                         }
140                     }
141                     /* musicbrainz artist and album id: not useful yet */
142 #if 0
143                     list = tag->frameListMap()["TXXX"];
144                     TagLib::ID3v2::UserTextIdentificationFrame* p_txxx;
145                     for( TagLib::ID3v2::FrameList::Iterator iter = list.begin();
146                             iter != list.end(); iter++ )
147                     {
148                         p_txxx = dynamic_cast<TagLib::ID3v2::UserTextIdentificationFrame*>(*iter);
149                         const char *psz_desc= p_txxx->description().toCString();
150                         if( !strncmp( psz_desc, "MusicBrainz Artist Id", 21 ) )
151                             vlc_meta_SetArtistID( p_meta,
152                                     p_txxx->fieldList().toString().toCString());
153                         if( !strncmp( psz_desc, "MusicBrainz Album Id", 20 ) )
154                             vlc_meta_SetAlbumID( p_meta,
155                                     p_txxx->fieldList().toString().toCString());
156                     }
157 #endif
158                 }
159             }
160
161             DetectImage( f, p_meta );
162
163             return VLC_SUCCESS;
164         }
165     }
166     return VLC_EGENERIC;
167 }
168
169 #define SET(a,b) if(b) tag->set##a( b );
170
171 static int WriteMeta( vlc_object_t *p_this )
172 {
173     playlist_t *p_playlist = (playlist_t *)p_this;
174     meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
175     input_item_t *p_item = p_export->p_item;
176     
177     if( p_item == NULL )
178     {
179         msg_Err( p_this, "Can't save meta data of an empty input" );
180         return VLC_EGENERIC;
181     }
182
183     TagLib::FileRef f( p_export->psz_file );
184     if( !f.isNull() && f.tag() )
185     {
186         msg_Dbg( p_this, "Updating metadata for %s", p_export->psz_file );
187         TagLib::Tag *tag = f.tag();
188         SET( Artist, p_item->p_meta->psz_artist );
189         if( p_item->p_meta->psz_title )
190             tag->setTitle( p_item->p_meta->psz_title );
191         else
192             tag->setTitle( p_item->psz_name );
193         SET( Album, p_item->p_meta->psz_album );
194         SET( Genre, p_item->p_meta->psz_genre );
195         if( p_item->p_meta->psz_date )
196             tag->setYear( atoi( p_item->p_meta->psz_date ) );
197         if( p_item->p_meta->psz_tracknum )
198             tag->setTrack( atoi( p_item->p_meta->psz_tracknum ) );
199         f.save();
200         return VLC_SUCCESS;
201     }
202     msg_Err( p_this, "File %s can't be opened for tag writing\n",
203         p_export->psz_file );
204     return VLC_EGENERIC;
205 }
206
207 static int DownloadArt( vlc_object_t *p_this )
208 {
209     /* We need to be passed the file name
210      * Fetch the thing from the file, save it to the cache folder
211      */
212     return VLC_EGENERIC;
213 }