1 /*****************************************************************************
2 * taglib.cpp: Taglib tag parser/writer
3 *****************************************************************************
4 * Copyright (C) 2003-2011 VLC authors and VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Rafaël Carré <funman@videolanorg>
9 * Rémi Duraffort <ivoire@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_demux.h> /* demux_meta_t */
33 #include <vlc_strings.h> /* vlc_b64_decode_binary */
34 #include <vlc_input.h> /* for attachment_new */
35 #include <vlc_url.h> /* make_path */
36 #include <vlc_mime.h> /* mime type */
42 # include <vlc_charset.h>
51 # define TAGLIB_STATIC
54 #define VERSION_INT(a, b, c) ((a)<<16 | (b)<<8 | (c))
55 #define TAGLIB_VERSION VERSION_INT(TAGLIB_MAJOR_VERSION, \
56 TAGLIB_MINOR_VERSION, \
61 #include <tbytevector.h>
63 #if TAGLIB_VERSION >= VERSION_INT(1,7,0)
64 # define TAGLIB_HAVE_APEFILE_H
66 # ifdef TAGLIB_WITH_ASF // ASF pictures comes with v1.7.0
67 # define TAGLIB_HAVE_ASFPICTURE_H
77 #include <oggflacfile.h>
78 #include "../demux/vorbis.h"
83 #if defined(TAGLIB_WITH_MP4)
87 #include <speexfile.h>
88 #include <trueaudiofile.h>
89 #include <vorbisfile.h>
90 #include <wavpackfile.h>
92 #include <attachedpictureframe.h>
93 #include <textidentificationframe.h>
94 #include <uniquefileidentifierframe.h>
96 // taglib is not thread safe
97 static vlc_mutex_t taglib_lock = VLC_STATIC_MUTEX;
100 static int ReadMeta ( vlc_object_t * );
101 static int WriteMeta ( vlc_object_t * );
104 set_capability( "meta reader", 1000 )
105 set_callbacks( ReadMeta, NULL )
107 set_capability( "meta writer", 50 )
108 set_callbacks( WriteMeta, NULL )
111 using namespace TagLib;
113 static void ExtractTrackNumberValues( vlc_meta_t* p_meta, const char *psz_value )
115 unsigned int i_trknum, i_trktot;
116 if( sscanf( psz_value, "%u/%u", &i_trknum, &i_trktot ) == 2 )
119 snprintf( psz_trck, sizeof( psz_trck ), "%u", i_trknum );
120 vlc_meta_SetTrackNum( p_meta, psz_trck );
121 snprintf( psz_trck, sizeof( psz_trck ), "%u", i_trktot );
122 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, psz_trck );
127 * Read meta information from APE tags
128 * @param tag: the APE tag
129 * @param p_demux_meta: the demuxer meta
130 * @param p_meta: the meta
132 static void ReadMetaFromAPE( APE::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
136 item = tag->itemListMap()["COVER ART (FRONT)"];
137 if( !item.isEmpty() )
139 input_attachment_t *p_attachment;
141 const ByteVector picture = item.value();
142 const char *p_data = picture.data();
143 unsigned i_data = picture.size();
145 size_t desc_len = strnlen(p_data, i_data);
146 if (desc_len < i_data) {
147 const char *psz_name = p_data;
148 p_data += desc_len + 1; /* '\0' */
149 i_data -= desc_len + 1;
150 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
151 psz_name, "image/jpeg", i_data );
153 p_attachment = vlc_input_attachment_New( "cover", "image/jpeg",
154 psz_name, p_data, i_data );
156 TAB_APPEND_CAST( (input_attachment_t**),
157 p_demux_meta->i_attachments, p_demux_meta->attachments,
160 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
164 #define SET( keyName, metaName ) \
165 item = tag->itemListMap()[keyName]; \
166 if( !item.isEmpty() ) vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) ); \
168 SET( "ALBUM", Album );
169 SET( "ARTIST", Artist );
170 SET( "COMMENT", Description );
171 SET( "GENRE", Genre );
172 SET( "TITLE", Title );
173 SET( "COPYRIGHT", Copyright );
174 SET( "LANGUAGE", Language );
175 SET( "PUBLISHER", Publisher );
180 item = tag->itemListMap()["TRACK"];
181 if( !item.isEmpty() )
183 ExtractTrackNumberValues( p_meta, item.toString().toCString( true ) );
188 #ifdef TAGLIB_HAVE_ASFPICTURE_H
190 * Read meta information from APE tags
191 * @param tag: the APE tag
192 * @param p_demux_meta: the demuxer meta
193 * @param p_meta: the meta
195 static void ReadMetaFromASF( ASF::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
198 ASF::AttributeList list = tag->attributeListMap()["WM/Picture"];
199 ASF::AttributeList::Iterator iter;
200 for( iter = list.begin(); iter != list.end(); iter++ )
202 const ASF::Picture asfPicture = (*iter).toPicture();
203 const ByteVector picture = asfPicture.picture();
204 const char *psz_mime = asfPicture.mimeType().toCString();
205 const char *p_data = picture.data();
206 const unsigned i_data = picture.size();
208 input_attachment_t *p_attachment;
210 if( asfPicture.description().size() > 0 )
211 psz_name = strdup( asfPicture.description().toCString( true ) );
214 if( asprintf( &psz_name, "%i", asfPicture.type() ) == -1 )
218 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
219 psz_name, psz_mime, i_data );
221 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
222 psz_name, p_data, i_data );
224 TAB_APPEND_CAST( (input_attachment_t**),
225 p_demux_meta->i_attachments, p_demux_meta->attachments,
230 if( asprintf( &psz_url, "attachment://%s",
231 p_attachment->psz_name ) == -1 )
233 vlc_meta_SetArtURL( p_meta, psz_url );
241 * Read meta information from id3v2 tags
242 * @param tag: the id3v2 tag
243 * @param p_demux_meta: the demuxer meta
244 * @param p_meta: the meta
246 static void ReadMetaFromId3v2( ID3v2::Tag* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
248 // Get the unique file identifier
249 ID3v2::FrameList list = tag->frameListMap()["UFID"];
250 ID3v2::FrameList::Iterator iter;
251 for( iter = list.begin(); iter != list.end(); iter++ )
253 ID3v2::UniqueFileIdentifierFrame* p_ufid =
254 dynamic_cast<ID3v2::UniqueFileIdentifierFrame*>(*iter);
257 const char *owner = p_ufid->owner().toCString();
258 if (!strcmp( owner, "http://musicbrainz.org" ))
260 /* ID3v2 UFID contains up to 64 bytes binary data
261 * but in our case it will be a '\0'
262 * terminated string */
264 int max_size = __MIN( p_ufid->identifier().size(), 63);
265 strncpy( psz_ufid, p_ufid->identifier().data(), max_size );
266 psz_ufid[max_size] = '\0';
267 vlc_meta_SetTrackID( p_meta, psz_ufid );
272 list = tag->frameListMap()["TXXX"];
273 for( iter = list.begin(); iter != list.end(); iter++ )
275 ID3v2::UserTextIdentificationFrame* p_txxx =
276 dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
279 if( !strcmp( p_txxx->description().toCString( true ), "TRACKTOTAL" ) )
281 vlc_meta_Set( p_meta, vlc_meta_TrackTotal, p_txxx->fieldList().back().toCString( true ) );
284 vlc_meta_AddExtra( p_meta, p_txxx->description().toCString( true ),
285 p_txxx->fieldList().back().toCString( true ) );
288 // Get some more information
289 #define SET( tagName, metaName ) \
290 list = tag->frameListMap()[tagName]; \
291 if( !list.isEmpty() ) \
292 vlc_meta_Set##metaName( p_meta, \
293 (*list.begin())->toString().toCString( true ) );
295 SET( "TCOP", Copyright );
296 SET( "TENC", EncodedBy );
297 SET( "TLAN", Language );
298 SET( "TPUB", Publisher );
303 list = tag->frameListMap()["TRCK"];
304 if( !list.isEmpty() )
306 ExtractTrackNumberValues( p_meta, (*list.begin())->toString().toCString( true ) );
309 /* Preferred type of image
310 * The 21 types are defined in id3v2 standard:
311 * http://www.id3.org/id3v2.4.0-frames */
312 static const int pi_cover_score[] = {
314 5, /* 32x32 PNG image that should be used as the file icon */
315 4, /* File icon of a different size or format. */
316 20, /* Front cover image of the album. */
317 19, /* Back cover image of the album. */
318 13, /* Inside leaflet page of the album. */
319 18, /* Image from the album itself. */
320 17, /* Picture of the lead artist or soloist. */
321 16, /* Picture of the artist or performer. */
322 14, /* Picture of the conductor. */
323 15, /* Picture of the band or orchestra. */
324 9, /* Picture of the composer. */
325 8, /* Picture of the lyricist or text writer. */
326 7, /* Picture of the recording location or studio. */
327 10, /* Picture of the artists during recording. */
328 11, /* Picture of the artists during performance. */
329 6, /* Picture from a movie or video related to the track. */
330 1, /* Picture of a large, coloured fish. */
331 12, /* Illustration related to the track. */
332 3, /* Logo of the band or performer. */
333 2 /* Logo of the publisher (record company). */
335 #define PI_COVER_SCORE_SIZE (sizeof (pi_cover_score) / sizeof (pi_cover_score[0]))
338 // Try now to get embedded art
339 list = tag->frameListMap()[ "APIC" ];
343 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
344 for( iter = list.begin(); iter != list.end(); iter++ )
346 ID3v2::AttachedPictureFrame* p_apic =
347 dynamic_cast<ID3v2::AttachedPictureFrame*>(*iter);
350 input_attachment_t *p_attachment;
352 const char *psz_mime;
353 char *psz_name, *psz_description;
355 // Get the mime and description of the image.
356 // If the description is empty, take the type as a description
357 psz_mime = p_apic->mimeType().toCString( true );
358 if( p_apic->description().size() > 0 )
359 psz_description = strdup( p_apic->description().toCString( true ) );
362 if( asprintf( &psz_description, "%i", p_apic->type() ) == -1 )
363 psz_description = NULL;
366 if( !psz_description )
368 psz_name = psz_description;
370 /* some old iTunes version not only sets incorrectly the mime type
371 * or the description of the image,
372 * but also embeds incorrectly the image.
373 * Recent versions seem to behave correctly */
374 if( !strncmp( psz_mime, "PNG", 3 ) ||
375 !strncmp( psz_name, "\xC2\x89PNG", 5 ) )
377 msg_Warn( p_demux_meta, "Invalid picture embedded by broken iTunes version" );
378 free( psz_description );
382 const ByteVector picture = p_apic->picture();
383 const char *p_data = picture.data();
384 const unsigned i_data = picture.size();
386 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %u bytes",
387 psz_name, psz_mime, i_data );
389 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
390 psz_description, p_data, i_data );
393 free( psz_description );
396 TAB_APPEND_CAST( (input_attachment_t**),
397 p_demux_meta->i_attachments, p_demux_meta->attachments,
399 free( psz_description );
401 unsigned i_pic_type = p_apic->type();
402 if( i_pic_type >= PI_COVER_SCORE_SIZE )
403 i_pic_type = 0; // Defaults to "Other"
405 if( pi_cover_score[i_pic_type] > i_score )
407 i_score = pi_cover_score[i_pic_type];
409 if( asprintf( &psz_url, "attachment://%s",
410 p_attachment->psz_name ) == -1 )
412 vlc_meta_SetArtURL( p_meta, psz_url );
420 * Read the meta information from XiphComments
421 * @param tag: the Xiph Comment
422 * @param p_demux_meta: the demuxer meta
423 * @param p_meta: the meta
425 static void ReadMetaFromXiph( Ogg::XiphComment* tag, demux_meta_t* p_demux_meta, vlc_meta_t* p_meta )
428 #define SET( keyName, metaName ) \
429 list = tag->fieldListMap()[keyName]; \
430 if( !list.isEmpty() ) \
431 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
433 SET( "COPYRIGHT", Copyright );
436 // Try now to get embedded art
437 StringList mime_list = tag->fieldListMap()[ "COVERARTMIME" ];
438 StringList art_list = tag->fieldListMap()[ "COVERART" ];
440 input_attachment_t *p_attachment;
442 if( mime_list.size() != 0 && art_list.size() != 0 )
444 // We get only the first covert art
445 if( mime_list.size() > 1 || art_list.size() > 1 )
446 msg_Warn( p_demux_meta, "Found %i embedded arts, so using only the first one",
449 const char* psz_name = "cover";
450 const char* psz_mime = mime_list[0].toCString(true);
451 const char* psz_description = "cover";
454 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
456 msg_Dbg( p_demux_meta, "Found embedded art: %s (%s) is %i bytes",
457 psz_name, psz_mime, i_data );
459 p_attachment = vlc_input_attachment_New( psz_name, psz_mime,
460 psz_description, p_data, i_data );
465 art_list = tag->fieldListMap()[ "METADATA_BLOCK_PICTURE" ];
466 if( art_list.size() == 0 )
471 int i_data = vlc_b64_decode_binary( &p_data, art_list[0].toCString(true) );
472 p_attachment = ParseFlacPicture( p_data, i_data, 0, &type );
475 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
477 TAB_APPEND_CAST( (input_attachment_t**),
478 p_demux_meta->i_attachments, p_demux_meta->attachments,
482 if( asprintf( &psz_url, "attachment://%s", p_attachment->psz_name ) != -1 ) {
483 vlc_meta_SetArtURL( p_meta, psz_url );
490 #if defined(TAGLIB_WITH_MP4)
492 * Read the meta information from mp4 specific tags
493 * @param tag: the mp4 tag
494 * @param p_demux_meta: the demuxer meta
495 * @param p_meta: the meta
497 static void ReadMetaFromMP4( MP4::Tag* tag, demux_meta_t *p_demux_meta, vlc_meta_t* p_meta )
499 if( tag->itemListMap().contains("covr") )
501 MP4::CoverArtList list = tag->itemListMap()["covr"].toCoverArtList();
502 const char *psz_format = list[0].format() == MP4::CoverArt::PNG ? "image/png" : "image/jpeg";
504 msg_Dbg( p_demux_meta, "Found embedded art (%s) is %i bytes",
505 psz_format, list[0].data().size() );
507 TAB_INIT( p_demux_meta->i_attachments, p_demux_meta->attachments );
508 input_attachment_t *p_attachment =
509 vlc_input_attachment_New( "cover", psz_format, "cover",
510 list[0].data().data(), list[0].data().size() );
511 TAB_APPEND_CAST( (input_attachment_t**),
512 p_demux_meta->i_attachments, p_demux_meta->attachments,
514 vlc_meta_SetArtURL( p_meta, "attachment://cover" );
521 * Get the tags from the file using TagLib
522 * @param p_this: the demux object
523 * @return VLC_SUCCESS if the operation success
525 static int ReadMeta( vlc_object_t* p_this)
527 vlc_mutex_locker locker (&taglib_lock);
528 demux_meta_t* p_demux_meta = (demux_meta_t *)p_this;
529 demux_t* p_demux = p_demux_meta->p_demux;
533 p_demux_meta->p_meta = NULL;
534 if( strcmp( p_demux->psz_access, "file" ) )
537 char *psz_path = strdup( p_demux->psz_file );
542 wchar_t *wpath = ToWide( psz_path );
548 f = FileRef( wpath );
551 f = FileRef( psz_path );
557 if( !f.tag() || f.tag()->isEmpty() )
560 p_demux_meta->p_meta = p_meta = vlc_meta_New();
565 // Read the tags from the file
566 Tag* p_tag = f.tag();
568 #define SET( tag, meta ) \
569 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
570 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
571 #define SETINT( tag, meta ) \
575 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
576 vlc_meta_Set##meta( p_meta, psz_tmp ); \
580 SET( artist, Artist );
582 SET( comment, Description );
584 SETINT( year, Date );
585 SETINT( track, TrackNum );
591 // Try now to read special tags
592 #ifdef TAGLIB_HAVE_APEFILE_H
593 if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
596 ReadMetaFromAPE( ape->APETag(), p_demux_meta, p_meta );
600 #ifdef TAGLIB_HAVE_ASFPICTURE_H
601 if( ASF::File* asf = dynamic_cast<ASF::File*>(f.file()) )
604 ReadMetaFromASF( asf->tag(), p_demux_meta, p_meta );
608 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
610 if( flac->ID3v2Tag() )
611 ReadMetaFromId3v2( flac->ID3v2Tag(), p_demux_meta, p_meta );
612 else if( flac->xiphComment() )
613 ReadMetaFromXiph( flac->xiphComment(), p_demux_meta, p_meta );
615 #if defined(TAGLIB_WITH_MP4)
616 else if( MP4::File *mp4 = dynamic_cast<MP4::File*>(f.file()) )
619 ReadMetaFromMP4( mp4->tag(), p_demux_meta, p_meta );
622 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
625 ReadMetaFromAPE( mpc->APETag(), p_demux_meta, p_meta );
627 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
629 if( mpeg->ID3v2Tag() )
630 ReadMetaFromId3v2( mpeg->ID3v2Tag(), p_demux_meta, p_meta );
631 else if( mpeg->APETag() )
632 ReadMetaFromAPE( mpeg->APETag(), p_demux_meta, p_meta );
634 else if( dynamic_cast<Ogg::File*>(f.file()) )
636 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
637 ReadMetaFromXiph( ogg_flac->tag(), p_demux_meta, p_meta );
638 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
639 ReadMetaFromXiph( ogg_speex->tag(), p_demux_meta, p_meta );
640 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
641 ReadMetaFromXiph( ogg_vorbis->tag(), p_demux_meta, p_meta );
643 else if( dynamic_cast<RIFF::File*>(f.file()) )
645 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
646 ReadMetaFromId3v2( riff_aiff->tag(), p_demux_meta, p_meta );
647 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
648 ReadMetaFromId3v2( riff_wav->tag(), p_demux_meta, p_meta );
650 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
652 if( trueaudio->ID3v2Tag() )
653 ReadMetaFromId3v2( trueaudio->ID3v2Tag(), p_demux_meta, p_meta );
655 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
657 if( wavpack->APETag() )
658 ReadMetaFromAPE( wavpack->APETag(), p_demux_meta, p_meta );
666 * Write meta information to APE tags
667 * @param tag: the APE tag
668 * @param p_item: the input item
670 static void WriteMetaToAPE( APE::Tag* tag, input_item_t* p_item )
673 #define WRITE( metaName, keyName ) \
674 psz_meta = input_item_Get##metaName( p_item ); \
677 String key( keyName, String::UTF8 ); \
678 String value( psz_meta, String::UTF8 ); \
679 tag->addValue( key, value, true ); \
683 WRITE( Copyright, "COPYRIGHT" );
684 WRITE( Language, "LANGUAGE" );
685 WRITE( Publisher, "PUBLISHER" );
692 * Write meta information to id3v2 tags
693 * @param tag: the id3v2 tag
694 * @param p_input: the input item
696 static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
699 #define WRITE( metaName, tagName ) \
700 psz_meta = input_item_Get##metaName( p_item ); \
703 ByteVector p_byte( tagName, 4 ); \
704 tag->removeFrames( p_byte ); \
705 ID3v2::TextIdentificationFrame* p_frame = \
706 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
707 p_frame->setText( psz_meta ); \
708 tag->addFrame( p_frame ); \
712 WRITE( Copyright, "TCOP" );
713 WRITE( EncodedBy, "TENC" );
714 WRITE( Language, "TLAN" );
715 WRITE( Publisher, "TPUB" );
718 /* Track Total as Custom Field */
719 psz_meta = input_item_GetTrackTotal( p_item );
722 ID3v2::FrameList list = tag->frameListMap()["TXXX"];
723 ID3v2::UserTextIdentificationFrame *p_txxx;
724 for( ID3v2::FrameList::Iterator iter = list.begin(); iter != list.end(); iter++ )
726 p_txxx = dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);
729 if( !strcmp( p_txxx->description().toCString( true ), "TRACKTOTAL" ) )
731 p_txxx->setText( psz_meta );
732 FREENULL( psz_meta );
736 if( psz_meta ) /* not found in existing custom fields */
738 ByteVector p_byte( "TXXX", 4 );
739 p_txxx = new ID3v2::UserTextIdentificationFrame( p_byte );
740 p_txxx->setDescription( "TRACKTOTAL" );
741 p_txxx->setText( psz_meta );
743 tag->addFrame( p_txxx );
747 /* Write album art */
748 char *psz_url = input_item_GetArtworkURL( p_item );
749 if( psz_url == NULL )
752 char *psz_path = make_path( psz_url );
754 if( psz_path == NULL )
757 const char *psz_mime = vlc_mime_Ext2Mime( psz_path );
759 FILE *p_file = vlc_fopen( psz_path, "rb" );
767 if( vlc_stat( psz_path, &st ) == -1 )
773 off_t file_size = st.st_size;
777 /* Limit picture size to 10MiB */
778 if( file_size > 10485760 )
784 char *p_buffer = new (std::nothrow) char[file_size];
785 if( p_buffer == NULL )
791 if( fread( p_buffer, 1, file_size, p_file ) != (unsigned)file_size )
799 ByteVector data( p_buffer, file_size );
802 ID3v2::FrameList frames = tag->frameList( "APIC" );
803 ID3v2::AttachedPictureFrame *frame = NULL;
804 if( frames.isEmpty() )
806 frame = new TagLib::ID3v2::AttachedPictureFrame;
807 tag->addFrame( frame );
811 frame = static_cast<ID3v2::AttachedPictureFrame *>( frames.back() );
814 frame->setPicture( data );
815 frame->setMimeType( psz_mime );
820 * Write the meta information to XiphComments
821 * @param tag: the Xiph Comment
822 * @param p_input: the input item
824 static void WriteMetaToXiph( Ogg::XiphComment* tag, input_item_t* p_item )
827 #define WRITE( metaName, keyName ) \
828 psz_meta = input_item_Get##metaName( p_item ); \
831 String key( keyName, String::UTF8 ); \
832 String value( psz_meta, String::UTF8 ); \
833 tag->addField( key, value, true ); \
837 WRITE( Copyright, "COPYRIGHT" );
844 * Set the tags to the file using TagLib
845 * @param p_this: the demux object
846 * @return VLC_SUCCESS if the operation success
849 static int WriteMeta( vlc_object_t *p_this )
851 vlc_mutex_locker locker (&taglib_lock);
852 meta_export_t *p_export = (meta_export_t *)p_this;
853 input_item_t *p_item = p_export->p_item;
858 msg_Err( p_this, "Can't save meta data of an empty input" );
863 wchar_t *wpath = ToWide( p_export->psz_file );
866 f = FileRef( wpath );
869 f = FileRef( p_export->psz_file );
872 if( f.isNull() || !f.tag() || f.file()->readOnly() )
874 msg_Err( p_this, "File %s can't be opened for tag writing",
875 p_export->psz_file );
879 msg_Dbg( p_this, "Writing metadata for %s", p_export->psz_file );
881 Tag *p_tag = f.tag();
885 #define SET( a, b ) \
886 psz_meta = input_item_Get ## a( p_item ); \
889 String tmp( psz_meta, String::UTF8 ); \
890 p_tag->set##b( tmp ); \
894 // Saving all common fields
895 // If the title is empty, use the name
896 SET( TitleFbName, Title );
897 SET( Artist, Artist );
899 SET( Description, Comment );
904 psz_meta = input_item_GetDate( p_item );
905 if( !EMPTY_STR(psz_meta) ) p_tag->setYear( atoi( psz_meta ) );
906 else p_tag->setYear( 0 );
909 psz_meta = input_item_GetTrackNum( p_item );
910 if( !EMPTY_STR(psz_meta) ) p_tag->setTrack( atoi( psz_meta ) );
911 else p_tag->setTrack( 0 );
915 // Try now to write special tags
916 #ifdef TAGLIB_HAVE_APEFILE_H
917 if( APE::File* ape = dynamic_cast<APE::File*>(f.file()) )
920 WriteMetaToAPE( ape->APETag(), p_item );
924 if( FLAC::File* flac = dynamic_cast<FLAC::File*>(f.file()) )
926 if( flac->ID3v2Tag() )
927 WriteMetaToId3v2( flac->ID3v2Tag(), p_item );
928 else if( flac->xiphComment() )
929 WriteMetaToXiph( flac->xiphComment(), p_item );
931 else if( MPC::File* mpc = dynamic_cast<MPC::File*>(f.file()) )
934 WriteMetaToAPE( mpc->APETag(), p_item );
936 else if( MPEG::File* mpeg = dynamic_cast<MPEG::File*>(f.file()) )
938 if( mpeg->ID3v2Tag() )
939 WriteMetaToId3v2( mpeg->ID3v2Tag(), p_item );
940 else if( mpeg->APETag() )
941 WriteMetaToAPE( mpeg->APETag(), p_item );
943 else if( dynamic_cast<Ogg::File*>(f.file()) )
945 if( Ogg::FLAC::File* ogg_flac = dynamic_cast<Ogg::FLAC::File*>(f.file()))
946 WriteMetaToXiph( ogg_flac->tag(), p_item );
947 else if( Ogg::Speex::File* ogg_speex = dynamic_cast<Ogg::Speex::File*>(f.file()) )
948 WriteMetaToXiph( ogg_speex->tag(), p_item );
949 else if( Ogg::Vorbis::File* ogg_vorbis = dynamic_cast<Ogg::Vorbis::File*>(f.file()) )
950 WriteMetaToXiph( ogg_vorbis->tag(), p_item );
952 else if( dynamic_cast<RIFF::File*>(f.file()) )
954 if( RIFF::AIFF::File* riff_aiff = dynamic_cast<RIFF::AIFF::File*>(f.file()) )
955 WriteMetaToId3v2( riff_aiff->tag(), p_item );
956 else if( RIFF::WAV::File* riff_wav = dynamic_cast<RIFF::WAV::File*>(f.file()) )
957 WriteMetaToId3v2( riff_wav->tag(), p_item );
959 else if( TrueAudio::File* trueaudio = dynamic_cast<TrueAudio::File*>(f.file()) )
961 if( trueaudio->ID3v2Tag() )
962 WriteMetaToId3v2( trueaudio->ID3v2Tag(), p_item );
964 else if( WavPack::File* wavpack = dynamic_cast<WavPack::File*>(f.file()) )
966 if( wavpack->APETag() )
967 WriteMetaToAPE( wavpack->APETag(), p_item );
970 // Save the meta data