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
72 #if TAGLIB_VERSION >= VERSION_INT(1,9,0)
73 # include <opusfile.h>
81 #include <oggflacfile.h>
82 #include "../demux/xiph_metadata.h"
87 #if defined(TAGLIB_WITH_MP4)
91 #include <speexfile.h>
92 #include <trueaudiofile.h>
93 #include <vorbisfile.h>
94 #include <wavpackfile.h>
96 #include <attachedpictureframe.h>
97 #include <textidentificationframe.h>
98 #include <uniquefileidentifierframe.h>
100 // taglib is not thread safe
101 static vlc_mutex_t taglib_lock
= VLC_STATIC_MUTEX
;
104 static int ReadMeta ( vlc_object_t
* );
105 static int WriteMeta ( vlc_object_t
* );
108 set_capability( "meta reader", 1000 )
109 set_callbacks( ReadMeta
, NULL
)
111 set_capability( "meta writer", 50 )
112 set_callbacks( WriteMeta
, NULL
)
115 using namespace TagLib
;
117 static void ExtractTrackNumberValues( vlc_meta_t
* p_meta
, const char *psz_value
)
119 unsigned int i_trknum
, i_trktot
;
120 if( sscanf( psz_value
, "%u/%u", &i_trknum
, &i_trktot
) == 2 )
123 snprintf( psz_trck
, sizeof( psz_trck
), "%u", i_trknum
);
124 vlc_meta_SetTrackNum( p_meta
, psz_trck
);
125 snprintf( psz_trck
, sizeof( psz_trck
), "%u", i_trktot
);
126 vlc_meta_Set( p_meta
, vlc_meta_TrackTotal
, psz_trck
);
131 * Read meta information from APE tags
132 * @param tag: the APE tag
133 * @param p_demux_meta: the demuxer meta
134 * @param p_meta: the meta
136 static void ReadMetaFromAPE( APE::Tag
* tag
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
140 item
= tag
->itemListMap()["COVER ART (FRONT)"];
141 if( !item
.isEmpty() )
143 input_attachment_t
*p_attachment
;
145 const ByteVector picture
= item
.value();
146 const char *p_data
= picture
.data();
147 unsigned i_data
= picture
.size();
149 size_t desc_len
= strnlen(p_data
, i_data
);
150 if (desc_len
< i_data
) {
151 const char *psz_name
= p_data
;
152 p_data
+= desc_len
+ 1; /* '\0' */
153 i_data
-= desc_len
+ 1;
154 msg_Dbg( p_demux_meta
, "Found embedded art: %s (%s) is %u bytes",
155 psz_name
, "image/jpeg", i_data
);
157 p_attachment
= vlc_input_attachment_New( "cover", "image/jpeg",
158 psz_name
, p_data
, i_data
);
160 TAB_APPEND_CAST( (input_attachment_t
**),
161 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
164 vlc_meta_SetArtURL( p_meta
, "attachment://cover" );
168 #define SET( keyName, metaName ) \
169 item = tag->itemListMap()[keyName]; \
170 if( !item.isEmpty() ) vlc_meta_Set##metaName( p_meta, item.toString().toCString( true ) ); \
172 SET( "ALBUM", Album );
173 SET( "ARTIST", Artist
);
174 SET( "COMMENT", Description
);
175 SET( "GENRE", Genre
);
176 SET( "TITLE", Title
);
177 SET( "COPYRIGHT", Copyright
);
178 SET( "LANGUAGE", Language
);
179 SET( "PUBLISHER", Publisher
);
184 item
= tag
->itemListMap()["TRACK"];
185 if( !item
.isEmpty() )
187 ExtractTrackNumberValues( p_meta
, item
.toString().toCString( true ) );
192 #ifdef TAGLIB_HAVE_ASFPICTURE_H
194 * Read meta information from APE tags
195 * @param tag: the APE tag
196 * @param p_demux_meta: the demuxer meta
197 * @param p_meta: the meta
199 static void ReadMetaFromASF( ASF::Tag
* tag
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
202 ASF::AttributeList list
= tag
->attributeListMap()["WM/Picture"];
203 ASF::AttributeList::Iterator iter
;
204 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
206 const ASF::Picture asfPicture
= (*iter
).toPicture();
207 const ByteVector picture
= asfPicture
.picture();
208 const char *psz_mime
= asfPicture
.mimeType().toCString();
209 const char *p_data
= picture
.data();
210 const unsigned i_data
= picture
.size();
212 input_attachment_t
*p_attachment
;
214 if( asfPicture
.description().size() > 0 )
215 psz_name
= strdup( asfPicture
.description().toCString( true ) );
218 if( asprintf( &psz_name
, "%i", asfPicture
.type() ) == -1 )
222 msg_Dbg( p_demux_meta
, "Found embedded art: %s (%s) is %u bytes",
223 psz_name
, psz_mime
, i_data
);
225 p_attachment
= vlc_input_attachment_New( psz_name
, psz_mime
,
226 psz_name
, p_data
, i_data
);
228 TAB_APPEND_CAST( (input_attachment_t
**),
229 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
234 if( asprintf( &psz_url
, "attachment://%s",
235 p_attachment
->psz_name
) == -1 )
237 vlc_meta_SetArtURL( p_meta
, psz_url
);
245 * Read meta information from id3v2 tags
246 * @param tag: the id3v2 tag
247 * @param p_demux_meta: the demuxer meta
248 * @param p_meta: the meta
250 static void ReadMetaFromId3v2( ID3v2::Tag
* tag
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
252 // Get the unique file identifier
253 ID3v2::FrameList list
= tag
->frameListMap()["UFID"];
254 ID3v2::FrameList::Iterator iter
;
255 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
257 ID3v2::UniqueFileIdentifierFrame
* p_ufid
=
258 dynamic_cast<ID3v2::UniqueFileIdentifierFrame
*>(*iter
);
261 const char *owner
= p_ufid
->owner().toCString();
262 if (!strcmp( owner
, "http://musicbrainz.org" ))
264 /* ID3v2 UFID contains up to 64 bytes binary data
265 * but in our case it will be a '\0'
266 * terminated string */
268 int max_size
= __MIN( p_ufid
->identifier().size(), 63);
269 strncpy( psz_ufid
, p_ufid
->identifier().data(), max_size
);
270 psz_ufid
[max_size
] = '\0';
271 vlc_meta_SetTrackID( p_meta
, psz_ufid
);
276 list
= tag
->frameListMap()["TXXX"];
277 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
279 ID3v2::UserTextIdentificationFrame
* p_txxx
=
280 dynamic_cast<ID3v2::UserTextIdentificationFrame
*>(*iter
);
283 if( !strcmp( p_txxx
->description().toCString( true ), "TRACKTOTAL" ) )
285 vlc_meta_Set( p_meta
, vlc_meta_TrackTotal
, p_txxx
->fieldList().back().toCString( true ) );
288 vlc_meta_AddExtra( p_meta
, p_txxx
->description().toCString( true ),
289 p_txxx
->fieldList().back().toCString( true ) );
292 // Get some more information
293 #define SET( tagName, metaName ) \
294 list = tag->frameListMap()[tagName]; \
295 if( !list.isEmpty() ) \
296 vlc_meta_Set##metaName( p_meta, \
297 (*list.begin())->toString().toCString( true ) );
299 SET( "TCOP", Copyright
);
300 SET( "TENC", EncodedBy
);
301 SET( "TLAN", Language
);
302 SET( "TPUB", Publisher
);
307 list
= tag
->frameListMap()["TRCK"];
308 if( !list
.isEmpty() )
310 ExtractTrackNumberValues( p_meta
, (*list
.begin())->toString().toCString( true ) );
313 /* Preferred type of image
314 * The 21 types are defined in id3v2 standard:
315 * http://www.id3.org/id3v2.4.0-frames */
316 static const int pi_cover_score
[] = {
318 5, /* 32x32 PNG image that should be used as the file icon */
319 4, /* File icon of a different size or format. */
320 20, /* Front cover image of the album. */
321 19, /* Back cover image of the album. */
322 13, /* Inside leaflet page of the album. */
323 18, /* Image from the album itself. */
324 17, /* Picture of the lead artist or soloist. */
325 16, /* Picture of the artist or performer. */
326 14, /* Picture of the conductor. */
327 15, /* Picture of the band or orchestra. */
328 9, /* Picture of the composer. */
329 8, /* Picture of the lyricist or text writer. */
330 7, /* Picture of the recording location or studio. */
331 10, /* Picture of the artists during recording. */
332 11, /* Picture of the artists during performance. */
333 6, /* Picture from a movie or video related to the track. */
334 1, /* Picture of a large, coloured fish. */
335 12, /* Illustration related to the track. */
336 3, /* Logo of the band or performer. */
337 2 /* Logo of the publisher (record company). */
339 #define PI_COVER_SCORE_SIZE (sizeof (pi_cover_score) / sizeof (pi_cover_score[0]))
342 // Try now to get embedded art
343 list
= tag
->frameListMap()[ "APIC" ];
347 TAB_INIT( p_demux_meta
->i_attachments
, p_demux_meta
->attachments
);
348 for( iter
= list
.begin(); iter
!= list
.end(); iter
++ )
350 ID3v2::AttachedPictureFrame
* p_apic
=
351 dynamic_cast<ID3v2::AttachedPictureFrame
*>(*iter
);
354 input_attachment_t
*p_attachment
;
356 const char *psz_mime
;
357 char *psz_name
, *psz_description
;
359 // Get the mime and description of the image.
360 // If the description is empty, take the type as a description
361 psz_mime
= p_apic
->mimeType().toCString( true );
362 if( p_apic
->description().size() > 0 )
363 psz_description
= strdup( p_apic
->description().toCString( true ) );
366 if( asprintf( &psz_description
, "%i", p_apic
->type() ) == -1 )
367 psz_description
= NULL
;
370 if( !psz_description
)
372 psz_name
= psz_description
;
374 /* some old iTunes version not only sets incorrectly the mime type
375 * or the description of the image,
376 * but also embeds incorrectly the image.
377 * Recent versions seem to behave correctly */
378 if( !strncmp( psz_mime
, "PNG", 3 ) ||
379 !strncmp( psz_name
, "\xC2\x89PNG", 5 ) )
381 msg_Warn( p_demux_meta
, "Invalid picture embedded by broken iTunes version" );
382 free( psz_description
);
386 const ByteVector picture
= p_apic
->picture();
387 const char *p_data
= picture
.data();
388 const unsigned i_data
= picture
.size();
390 msg_Dbg( p_demux_meta
, "Found embedded art: %s (%s) is %u bytes",
391 psz_name
, psz_mime
, i_data
);
393 p_attachment
= vlc_input_attachment_New( psz_name
, psz_mime
,
394 psz_description
, p_data
, i_data
);
397 free( psz_description
);
400 TAB_APPEND_CAST( (input_attachment_t
**),
401 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
403 free( psz_description
);
405 unsigned i_pic_type
= p_apic
->type();
406 if( i_pic_type
>= PI_COVER_SCORE_SIZE
)
407 i_pic_type
= 0; // Defaults to "Other"
409 if( pi_cover_score
[i_pic_type
] > i_score
)
411 i_score
= pi_cover_score
[i_pic_type
];
413 if( asprintf( &psz_url
, "attachment://%s",
414 p_attachment
->psz_name
) == -1 )
416 vlc_meta_SetArtURL( p_meta
, psz_url
);
424 * Read the meta information from XiphComments
425 * @param tag: the Xiph Comment
426 * @param p_demux_meta: the demuxer meta
427 * @param p_meta: the meta
429 static void ReadMetaFromXiph( Ogg::XiphComment
* tag
, demux_meta_t
* p_demux_meta
, vlc_meta_t
* p_meta
)
432 bool hasTrackTotal
= false;
433 #define SET( keyName, metaName ) \
434 list = tag->fieldListMap()[keyName]; \
435 if( !list.isEmpty() ) \
436 vlc_meta_Set##metaName( p_meta, (*list.begin()).toCString( true ) );
438 SET( "COPYRIGHT", Copyright
);
439 SET( "ORGANIZATION", Publisher
);
441 SET( "ENCODER", EncodedBy
);
442 SET( "RATING", Rating
);
443 SET( "LANGUAGE", Language
);
446 list
= tag
->fieldListMap()["TRACKNUMBER"];
447 if( !list
.isEmpty() )
449 const char *psz_value
;
450 unsigned short u_track
;
451 unsigned short u_total
;
452 psz_value
= (*list
.begin()).toCString( true );
453 if( sscanf( psz_value
, "%hu/%hu", &u_track
, &u_total
) == 2)
456 snprintf(str
, 6, "%u", u_track
);
457 vlc_meta_SetTrackNum( p_meta
, str
);
458 snprintf(str
, 6, "%u", u_total
);
459 vlc_meta_SetTrackTotal( p_meta
, str
);
460 hasTrackTotal
= true;
463 vlc_meta_SetTrackNum( p_meta
, psz_value
);
467 list
= tag
->fieldListMap()["TRACKTOTAL"];
469 list
= tag
->fieldListMap()["TOTALTRACKS"];
470 if( !list
.isEmpty() )
471 vlc_meta_SetTrackTotal( p_meta
, (*list
.begin()).toCString( true ) );
474 // Try now to get embedded art
475 StringList mime_list
= tag
->fieldListMap()[ "COVERARTMIME" ];
476 StringList art_list
= tag
->fieldListMap()[ "COVERART" ];
478 input_attachment_t
*p_attachment
;
480 if( mime_list
.size() != 0 && art_list
.size() != 0 )
482 // We get only the first covert art
483 if( mime_list
.size() > 1 || art_list
.size() > 1 )
484 msg_Warn( p_demux_meta
, "Found %i embedded arts, so using only the first one",
487 const char* psz_name
= "cover";
488 const char* psz_mime
= mime_list
[0].toCString(true);
489 const char* psz_description
= "cover";
492 int i_data
= vlc_b64_decode_binary( &p_data
, art_list
[0].toCString(true) );
494 msg_Dbg( p_demux_meta
, "Found embedded art: %s (%s) is %i bytes",
495 psz_name
, psz_mime
, i_data
);
497 p_attachment
= vlc_input_attachment_New( psz_name
, psz_mime
,
498 psz_description
, p_data
, i_data
);
503 art_list
= tag
->fieldListMap()[ "METADATA_BLOCK_PICTURE" ];
504 if( art_list
.size() == 0 )
510 int i_data
= vlc_b64_decode_binary( &p_data
, art_list
[0].toCString(true) );
511 i_cover_score
= i_cover_idx
= 0;
512 /* TODO: Use i_cover_score / i_cover_idx to select the picture. */
513 p_attachment
= ParseFlacPicture( p_data
, i_data
, 0,
514 &i_cover_score
, &i_cover_idx
);
517 TAB_INIT( p_demux_meta
->i_attachments
, p_demux_meta
->attachments
);
519 TAB_APPEND_CAST( (input_attachment_t
**),
520 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
524 if( asprintf( &psz_url
, "attachment://%s", p_attachment
->psz_name
) != -1 ) {
525 vlc_meta_SetArtURL( p_meta
, psz_url
);
532 #if defined(TAGLIB_WITH_MP4)
534 * Read the meta information from mp4 specific tags
535 * @param tag: the mp4 tag
536 * @param p_demux_meta: the demuxer meta
537 * @param p_meta: the meta
539 static void ReadMetaFromMP4( MP4::Tag
* tag
, demux_meta_t
*p_demux_meta
, vlc_meta_t
* p_meta
)
541 if( tag
->itemListMap().contains("covr") )
543 MP4::CoverArtList list
= tag
->itemListMap()["covr"].toCoverArtList();
544 const char *psz_format
= list
[0].format() == MP4::CoverArt::PNG
? "image/png" : "image/jpeg";
546 msg_Dbg( p_demux_meta
, "Found embedded art (%s) is %i bytes",
547 psz_format
, list
[0].data().size() );
549 TAB_INIT( p_demux_meta
->i_attachments
, p_demux_meta
->attachments
);
550 input_attachment_t
*p_attachment
=
551 vlc_input_attachment_New( "cover", psz_format
, "cover",
552 list
[0].data().data(), list
[0].data().size() );
553 TAB_APPEND_CAST( (input_attachment_t
**),
554 p_demux_meta
->i_attachments
, p_demux_meta
->attachments
,
556 vlc_meta_SetArtURL( p_meta
, "attachment://cover" );
563 * Get the tags from the file using TagLib
564 * @param p_this: the demux object
565 * @return VLC_SUCCESS if the operation success
567 static int ReadMeta( vlc_object_t
* p_this
)
569 vlc_mutex_locker
locker (&taglib_lock
);
570 demux_meta_t
* p_demux_meta
= (demux_meta_t
*)p_this
;
571 demux_t
* p_demux
= p_demux_meta
->p_demux
;
575 p_demux_meta
->p_meta
= NULL
;
576 if( strcmp( p_demux
->psz_access
, "file" ) )
579 char *psz_path
= strdup( p_demux
->psz_file
);
584 wchar_t *wpath
= ToWide( psz_path
);
590 f
= FileRef( wpath
);
593 f
= FileRef( psz_path
);
599 if( !f
.tag() || f
.tag()->isEmpty() )
602 p_demux_meta
->p_meta
= p_meta
= vlc_meta_New();
607 // Read the tags from the file
608 Tag
* p_tag
= f
.tag();
610 #define SET( tag, meta ) \
611 if( !p_tag->tag().isNull() && !p_tag->tag().isEmpty() ) \
612 vlc_meta_Set##meta( p_meta, p_tag->tag().toCString(true) )
613 #define SETINT( tag, meta ) \
617 snprintf( psz_tmp, 10, "%d", p_tag->tag() ); \
618 vlc_meta_Set##meta( p_meta, psz_tmp ); \
622 SET( artist
, Artist
);
624 SET( comment
, Description
);
626 SETINT( year
, Date
);
627 SETINT( track
, TrackNum
);
633 // Try now to read special tags
634 #ifdef TAGLIB_HAVE_APEFILE_H
635 if( APE::File
* ape
= dynamic_cast<APE::File
*>(f
.file()) )
638 ReadMetaFromAPE( ape
->APETag(), p_demux_meta
, p_meta
);
642 #ifdef TAGLIB_HAVE_ASFPICTURE_H
643 if( ASF::File
* asf
= dynamic_cast<ASF::File
*>(f
.file()) )
646 ReadMetaFromASF( asf
->tag(), p_demux_meta
, p_meta
);
650 if( FLAC::File
* flac
= dynamic_cast<FLAC::File
*>(f
.file()) )
652 if( flac
->ID3v2Tag() )
653 ReadMetaFromId3v2( flac
->ID3v2Tag(), p_demux_meta
, p_meta
);
654 else if( flac
->xiphComment() )
655 ReadMetaFromXiph( flac
->xiphComment(), p_demux_meta
, p_meta
);
657 #if defined(TAGLIB_WITH_MP4)
658 else if( MP4::File
*mp4
= dynamic_cast<MP4::File
*>(f
.file()) )
661 ReadMetaFromMP4( mp4
->tag(), p_demux_meta
, p_meta
);
664 else if( MPC::File
* mpc
= dynamic_cast<MPC::File
*>(f
.file()) )
667 ReadMetaFromAPE( mpc
->APETag(), p_demux_meta
, p_meta
);
669 else if( MPEG::File
* mpeg
= dynamic_cast<MPEG::File
*>(f
.file()) )
671 if( mpeg
->ID3v2Tag() )
672 ReadMetaFromId3v2( mpeg
->ID3v2Tag(), p_demux_meta
, p_meta
);
673 else if( mpeg
->APETag() )
674 ReadMetaFromAPE( mpeg
->APETag(), p_demux_meta
, p_meta
);
676 else if( dynamic_cast<Ogg::File
*>(f
.file()) )
678 if( Ogg::FLAC::File
* ogg_flac
= dynamic_cast<Ogg::FLAC::File
*>(f
.file()))
679 ReadMetaFromXiph( ogg_flac
->tag(), p_demux_meta
, p_meta
);
680 else if( Ogg::Speex::File
* ogg_speex
= dynamic_cast<Ogg::Speex::File
*>(f
.file()) )
681 ReadMetaFromXiph( ogg_speex
->tag(), p_demux_meta
, p_meta
);
682 else if( Ogg::Vorbis::File
* ogg_vorbis
= dynamic_cast<Ogg::Vorbis::File
*>(f
.file()) )
683 ReadMetaFromXiph( ogg_vorbis
->tag(), p_demux_meta
, p_meta
);
684 #if defined(TAGLIB_OPUSFILE_H)
685 else if( Ogg::Opus::File
* ogg_opus
= dynamic_cast<Ogg::Opus::File
*>(f
.file()) )
686 ReadMetaFromXiph( ogg_opus
->tag(), p_demux_meta
, p_meta
);
689 else if( dynamic_cast<RIFF::File
*>(f
.file()) )
691 if( RIFF::AIFF::File
* riff_aiff
= dynamic_cast<RIFF::AIFF::File
*>(f
.file()) )
692 ReadMetaFromId3v2( riff_aiff
->tag(), p_demux_meta
, p_meta
);
693 else if( RIFF::WAV::File
* riff_wav
= dynamic_cast<RIFF::WAV::File
*>(f
.file()) )
694 ReadMetaFromId3v2( riff_wav
->tag(), p_demux_meta
, p_meta
);
696 else if( TrueAudio::File
* trueaudio
= dynamic_cast<TrueAudio::File
*>(f
.file()) )
698 if( trueaudio
->ID3v2Tag() )
699 ReadMetaFromId3v2( trueaudio
->ID3v2Tag(), p_demux_meta
, p_meta
);
701 else if( WavPack::File
* wavpack
= dynamic_cast<WavPack::File
*>(f
.file()) )
703 if( wavpack
->APETag() )
704 ReadMetaFromAPE( wavpack
->APETag(), p_demux_meta
, p_meta
);
712 * Write meta information to APE tags
713 * @param tag: the APE tag
714 * @param p_item: the input item
716 static void WriteMetaToAPE( APE::Tag
* tag
, input_item_t
* p_item
)
719 #define WRITE( metaName, keyName ) \
720 psz_meta = input_item_Get##metaName( p_item ); \
723 String key( keyName, String::UTF8 ); \
724 String value( psz_meta, String::UTF8 ); \
725 tag->addValue( key, value, true ); \
729 WRITE( Copyright
, "COPYRIGHT" );
730 WRITE( Language
, "LANGUAGE" );
731 WRITE( Publisher
, "PUBLISHER" );
738 * Write meta information to id3v2 tags
739 * @param tag: the id3v2 tag
740 * @param p_input: the input item
742 static void WriteMetaToId3v2( ID3v2::Tag
* tag
, input_item_t
* p_item
)
745 #define WRITE( metaName, tagName ) \
746 psz_meta = input_item_Get##metaName( p_item ); \
749 ByteVector p_byte( tagName, 4 ); \
750 tag->removeFrames( p_byte ); \
751 ID3v2::TextIdentificationFrame* p_frame = \
752 new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 ); \
753 p_frame->setText( psz_meta ); \
754 tag->addFrame( p_frame ); \
758 WRITE( Copyright
, "TCOP" );
759 WRITE( EncodedBy
, "TENC" );
760 WRITE( Language
, "TLAN" );
761 WRITE( Publisher
, "TPUB" );
764 /* Track Total as Custom Field */
765 psz_meta
= input_item_GetTrackTotal( p_item
);
768 ID3v2::FrameList list
= tag
->frameListMap()["TXXX"];
769 ID3v2::UserTextIdentificationFrame
*p_txxx
;
770 for( ID3v2::FrameList::Iterator iter
= list
.begin(); iter
!= list
.end(); iter
++ )
772 p_txxx
= dynamic_cast<ID3v2::UserTextIdentificationFrame
*>(*iter
);
775 if( !strcmp( p_txxx
->description().toCString( true ), "TRACKTOTAL" ) )
777 p_txxx
->setText( psz_meta
);
778 FREENULL( psz_meta
);
782 if( psz_meta
) /* not found in existing custom fields */
784 ByteVector
p_byte( "TXXX", 4 );
785 p_txxx
= new ID3v2::UserTextIdentificationFrame( p_byte
);
786 p_txxx
->setDescription( "TRACKTOTAL" );
787 p_txxx
->setText( psz_meta
);
789 tag
->addFrame( p_txxx
);
793 /* Write album art */
794 char *psz_url
= input_item_GetArtworkURL( p_item
);
795 if( psz_url
== NULL
)
798 char *psz_path
= make_path( psz_url
);
800 if( psz_path
== NULL
)
803 const char *psz_mime
= vlc_mime_Ext2Mime( psz_path
);
805 FILE *p_file
= vlc_fopen( psz_path
, "rb" );
813 if( vlc_stat( psz_path
, &st
) == -1 )
819 off_t file_size
= st
.st_size
;
823 /* Limit picture size to 10MiB */
824 if( file_size
> 10485760 )
830 char *p_buffer
= new (std::nothrow
) char[file_size
];
831 if( p_buffer
== NULL
)
837 if( fread( p_buffer
, 1, file_size
, p_file
) != (unsigned)file_size
)
845 ByteVector
data( p_buffer
, file_size
);
848 ID3v2::FrameList frames
= tag
->frameList( "APIC" );
849 ID3v2::AttachedPictureFrame
*frame
= NULL
;
850 if( frames
.isEmpty() )
852 frame
= new TagLib::ID3v2::AttachedPictureFrame
;
853 tag
->addFrame( frame
);
857 frame
= static_cast<ID3v2::AttachedPictureFrame
*>( frames
.back() );
860 frame
->setPicture( data
);
861 frame
->setMimeType( psz_mime
);
866 * Write the meta information to XiphComments
867 * @param tag: the Xiph Comment
868 * @param p_input: the input item
870 static void WriteMetaToXiph( Ogg::XiphComment
* tag
, input_item_t
* p_item
)
873 #define WRITE( metaName, keyName ) \
874 psz_meta = input_item_Get##metaName( p_item ); \
877 String key( keyName, String::UTF8 ); \
878 String value( psz_meta, String::UTF8 ); \
879 tag->addField( key, value, true ); \
883 WRITE( TrackNum
, "TRACKNUMBER" );
884 WRITE( TrackTotal
, "TRACKTOTAL" );
885 WRITE( Copyright
, "COPYRIGHT" );
886 WRITE( Publisher
, "ORGANIZATION" );
887 WRITE( Date
, "DATE" );
888 WRITE( EncodedBy
, "ENCODER" );
889 WRITE( Rating
, "RATING" );
890 WRITE( Language
, "LANGUAGE" );
897 * Set the tags to the file using TagLib
898 * @param p_this: the demux object
899 * @return VLC_SUCCESS if the operation success
902 static int WriteMeta( vlc_object_t
*p_this
)
904 vlc_mutex_locker
locker (&taglib_lock
);
905 meta_export_t
*p_export
= (meta_export_t
*)p_this
;
906 input_item_t
*p_item
= p_export
->p_item
;
911 msg_Err( p_this
, "Can't save meta data of an empty input" );
916 wchar_t *wpath
= ToWide( p_export
->psz_file
);
919 f
= FileRef( wpath
);
922 f
= FileRef( p_export
->psz_file
);
925 if( f
.isNull() || !f
.tag() || f
.file()->readOnly() )
927 msg_Err( p_this
, "File %s can't be opened for tag writing",
928 p_export
->psz_file
);
932 msg_Dbg( p_this
, "Writing metadata for %s", p_export
->psz_file
);
934 Tag
*p_tag
= f
.tag();
938 #define SET( a, b ) \
939 psz_meta = input_item_Get ## a( p_item ); \
942 String tmp( psz_meta, String::UTF8 ); \
943 p_tag->set##b( tmp ); \
947 // Saving all common fields
948 // If the title is empty, use the name
949 SET( TitleFbName
, Title
);
950 SET( Artist
, Artist
);
952 SET( Description
, Comment
);
957 psz_meta
= input_item_GetDate( p_item
);
958 if( !EMPTY_STR(psz_meta
) ) p_tag
->setYear( atoi( psz_meta
) );
959 else p_tag
->setYear( 0 );
962 psz_meta
= input_item_GetTrackNum( p_item
);
963 if( !EMPTY_STR(psz_meta
) ) p_tag
->setTrack( atoi( psz_meta
) );
964 else p_tag
->setTrack( 0 );
968 // Try now to write special tags
969 #ifdef TAGLIB_HAVE_APEFILE_H
970 if( APE::File
* ape
= dynamic_cast<APE::File
*>(f
.file()) )
973 WriteMetaToAPE( ape
->APETag(), p_item
);
977 if( FLAC::File
* flac
= dynamic_cast<FLAC::File
*>(f
.file()) )
979 if( flac
->ID3v2Tag() )
980 WriteMetaToId3v2( flac
->ID3v2Tag(), p_item
);
981 else if( flac
->xiphComment() )
982 WriteMetaToXiph( flac
->xiphComment(), p_item
);
984 else if( MPC::File
* mpc
= dynamic_cast<MPC::File
*>(f
.file()) )
987 WriteMetaToAPE( mpc
->APETag(), p_item
);
989 else if( MPEG::File
* mpeg
= dynamic_cast<MPEG::File
*>(f
.file()) )
991 if( mpeg
->ID3v2Tag() )
992 WriteMetaToId3v2( mpeg
->ID3v2Tag(), p_item
);
993 else if( mpeg
->APETag() )
994 WriteMetaToAPE( mpeg
->APETag(), p_item
);
996 else if( dynamic_cast<Ogg::File
*>(f
.file()) )
998 if( Ogg::FLAC::File
* ogg_flac
= dynamic_cast<Ogg::FLAC::File
*>(f
.file()))
999 WriteMetaToXiph( ogg_flac
->tag(), p_item
);
1000 else if( Ogg::Speex::File
* ogg_speex
= dynamic_cast<Ogg::Speex::File
*>(f
.file()) )
1001 WriteMetaToXiph( ogg_speex
->tag(), p_item
);
1002 else if( Ogg::Vorbis::File
* ogg_vorbis
= dynamic_cast<Ogg::Vorbis::File
*>(f
.file()) )
1003 WriteMetaToXiph( ogg_vorbis
->tag(), p_item
);
1004 #if defined(TAGLIB_OPUSFILE_H)
1005 else if( Ogg::Opus::File
* ogg_opus
= dynamic_cast<Ogg::Opus::File
*>(f
.file()) )
1006 WriteMetaToXiph( ogg_opus
->tag(), p_item
);
1009 else if( dynamic_cast<RIFF::File
*>(f
.file()) )
1011 if( RIFF::AIFF::File
* riff_aiff
= dynamic_cast<RIFF::AIFF::File
*>(f
.file()) )
1012 WriteMetaToId3v2( riff_aiff
->tag(), p_item
);
1013 else if( RIFF::WAV::File
* riff_wav
= dynamic_cast<RIFF::WAV::File
*>(f
.file()) )
1014 WriteMetaToId3v2( riff_wav
->tag(), p_item
);
1016 else if( TrueAudio::File
* trueaudio
= dynamic_cast<TrueAudio::File
*>(f
.file()) )
1018 if( trueaudio
->ID3v2Tag() )
1019 WriteMetaToId3v2( trueaudio
->ID3v2Tag(), p_item
);
1021 else if( WavPack::File
* wavpack
= dynamic_cast<WavPack::File
*>(f
.file()) )
1023 if( wavpack
->APETag() )
1024 WriteMetaToAPE( wavpack
->APETag(), p_item
);
1027 // Save the meta data