taglib.cpp 3.37 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * taglib.cpp: Taglib tag parser/writer
 *****************************************************************************
 * Copyright (C) 2003-2006 the VideoLAN team
dionoea's avatar
dionoea committed
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * Authors: Clément Stenac <zorglub@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/input.h>
#include <vlc_meta.h>

#include <fileref.h>
#include <tag.h>
zorglub's avatar
zorglub committed
29 30
#include <id3v2tag.h>
#include <mpegfile.h>
31
#include <flacfile.h>
32 33 34 35 36 37 38

static int  ReadMeta ( vlc_object_t * );

vlc_module_begin();
    set_capability( "meta reader", 1000 );
    set_callbacks( ReadMeta, NULL );
vlc_module_end();
zorglub's avatar
zorglub committed
39

40
static bool checkID3Image( const TagLib::ID3v2::Tag *tag )
zorglub's avatar
zorglub committed
41 42 43 44 45
{
    TagLib::ID3v2::FrameList l = tag->frameListMap()[ "APIC" ];
    return !l.isEmpty();
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/* Try detecting embedded art */
static void DetectImage( TagLib::FileRef f, vlc_meta_t *p_meta )
{
    if( TagLib::MPEG::File *mpeg =
               dynamic_cast<TagLib::MPEG::File *>(f.file() ) )
    {
        if( mpeg->ID3v2Tag() && checkID3Image( mpeg->ID3v2Tag() ) )
            vlc_meta_SetArtURL( p_meta, "APIC" );
    }
    else if( TagLib::FLAC::File *flac =
             dynamic_cast<TagLib::FLAC::File *>(f.file() ) )
    {
        if( flac->ID3v2Tag() && checkID3Image( flac->ID3v2Tag() ) )
            vlc_meta_SetArtURL( p_meta, "APIC" );
    }
#if 0
/* This needs special additions to taglib */
 * else if( TagLib::MP4::File *mp4 =
               dynamic_cast<TagLib::MP4::File *>( f.file() ) )
    {
        TagLib::MP4::Tag *mp4tag =
                dynamic_cast<TagLib::MP4::Tag *>( mp4->tag() );
        if( mp4tag && mp4tag->cover().size() )
            vlc_meta_SetArtURL( p_meta, "MP4C" );
    }
#endif
}
zorglub's avatar
zorglub committed
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87
static int ReadMeta( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t *)p_this;

    if( !strncmp( p_demux->psz_access, "file", 4 ) )
    {
        if( !p_demux->p_private )
            p_demux->p_private = (void*)vlc_meta_New();
        TagLib::FileRef f( p_demux->psz_path );
        if( !f.isNull() && f.tag() )
        {
            TagLib::Tag *tag = f.tag();
            vlc_meta_t *p_meta = (vlc_meta_t *)(p_demux->p_private );

88 89 90 91 92 93 94 95 96 97
#define SET( foo, bar ) vlc_meta_Set##foo( p_meta, tag->bar ().toCString(true))
            SET( Title, title );
            SET( Artist, artist );
            SET( Album, album );
//            SET( Comment, comment );
            SET( Genre, genre );
//            SET( Year, year ); Gra, this is an int, need to convert
//            SET( Tracknum , track ); Same
#undef SET
            DetectImage( f, p_meta );
zorglub's avatar
zorglub committed
98

99 100 101 102 103
            return VLC_SUCCESS;
        }
    }
    return VLC_EGENERIC;
}