]> git.sesse.net Git - vlc/blob - modules/meta_engine/taglib.cpp
taglib.cpp: Don't put empty info in playlist for files with no tag
[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 <vlc/vlc.h>
24 #include <vlc/input.h>
25 #include <vlc_meta.h>
26
27 #include <fileref.h>
28 #include <tag.h>
29 #include <id3v2tag.h>
30 #include <mpegfile.h>
31 #include <flacfile.h>
32
33 static int  ReadMeta    ( vlc_object_t * );
34 static int  DownloadArt ( vlc_object_t * );
35 static int  WriteMeta   ( vlc_object_t * );
36
37 vlc_module_begin();
38     set_capability( "meta reader", 1000 );
39     set_callbacks( ReadMeta, NULL );
40     add_submodule();
41         set_capability( "art downloader", 50 );
42         set_callbacks( DownloadArt, NULL );
43     add_submodule();
44         set_capability( "meta writer", 50 );
45         set_callbacks( WriteMeta, NULL );
46 vlc_module_end();
47
48 static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
49 {
50     TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
51     return !l.isEmpty();
52 }
53
54 /* Try detecting embedded art */
55 static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
56 {
57     if( TagLib::MPEG::File *mpeg =
58                dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
59     {
60         if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
61             vlc_meta_SetArtURL( p_meta, "APIC" );
62     }
63     else if( TagLib::FLAC::File *flac =
64              dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
65     {
66         if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
67             vlc_meta_SetArtURL( p_meta, "APIC" );
68     }
69 #if 0
70 /* This needs special additions to taglib */
71  * else if( TagLib::MP4::File *mp4 =
72                dynamic_cast<TagLib::MP4::File *>( f.file() ) )
73     {
74         TagLib::MP4::Tag *mp4tag =
75                 dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
76         if( mp4tag && mp4tag->cover().size() )
77             vlc_meta_SetArtURL( p_meta, "MP4C" );
78     }
79 #endif
80 }
81
82 static int ReadMeta( vlc_object_t *p_this )
83 {
84     demux_t *p_demux = (demux_t *)p_this;
85
86     if( !strncmp( p_demux->psz_access, "file", 4 ) )
87     {
88         if( !p_demux->p_private )
89             p_demux->p_private = (void*)vlc_meta_New();
90         TagLib::FileRef f( p_demux->psz_path );
91         if( !f.isNull() && f.tag() && !f.tag()->isEmpty() )
92         {
93             TagLib::Tag *tag = f.tag();
94             vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );
95
96 #define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
97             SET( Title, title );
98             SET( Artist, artist );
99             SET( Album, album );
100 //            SET( Comment, comment );
101             SET( Genre, genre );
102 //            SET( Year, year ); Gra, this is an int, need to convert
103 //            SET( Tracknum , track ); Same
104 #undef SET
105             DetectImage( f, p_meta );
106
107             return VLC_SUCCESS;
108         }
109     }
110     return VLC_EGENERIC;
111 }
112
113 static int WriteMeta( vlc_object_t *p_this )
114 {
115     playlist_t *p_playlist = (playlist_t *)p_this;
116     meta_export_t *p_export = (meta_export_t *)p_playlist->p_private;
117     input_item_t *p_item = p_export->p_item;
118
119     TagLib::FileRef f( p_export->psz_file );
120     if( !f.isNull() && f.tag() )
121     {
122         TagLib::Tag *tag = f.tag();
123         tag->setArtist( p_item->p_meta->psz_artist );
124         f.save();
125         return VLC_SUCCESS;
126     }
127     return VLC_EGENERIC;
128 }
129
130 static int DownloadArt( vlc_object_t *p_this )
131 {
132     /* We need to be passed the file name
133      * Fetch the thing from the file, save it to the cache folder
134      */
135     return VLC_EGENERIC;
136 }