vlc_linux/vlc-3.0.16/modules/meta_engine/taglib.cpp
/**
 * Write meta information to id3v2 tags
 * @param tag: the id3v2 tag
 * @param p_input: the input item
 */
Write meta information to id3v2 tags
@param tag: the id3v2 tag
@param p_input: the input item
Write meta information to id3v2 tags @param tag : the id3v2 tag @param p_input : the input item
static void WriteMetaToId3v2( ID3v2::Tag* tag, input_item_t* p_item )
{
    char* psz_meta;
#define WRITE( metaName, tagName )                                            \
    psz_meta = input_item_Get##metaName( p_item );                            \
    if( psz_meta )                                                            \
    {                                                                         \
        ByteVector p_byte( tagName, 4 );                                      \
        tag->removeFrames( p_byte );                                         \
        ID3v2::TextIdentificationFrame* p_frame =                             \
            new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 );       \
        p_frame->setText( psz_meta );                                         \
        tag->addFrame( p_frame );                                             \
    }                                                                         \
    free( psz_meta );
    WRITE( Copyright, "TCOP" );
    WRITE( EncodedBy, "TENC" );
    WRITE( Language,  "TLAN" );
    WRITE( Publisher, "TPUB" );
#undef WRITE
    ID3v2::FrameList list = tag->frameListMap()["TXXX"];
#define WRITETXXX( metaName, txxName )\
    psz_meta = input_item_Get##metaName( p_item );                                       \
    if ( psz_meta )                                                                      \
    {                                                                                    \
        ID3v2::UserTextIdentificationFrame *p_txxx;                                      \
        for( ID3v2::FrameList::Iterator iter = list.begin(); iter != list.end(); iter++ )\
        {                                                                                \
            p_txxx = dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);           \
            if( !p_txxx )                                                                \
                continue;                                                                \
            if( !strcmp( p_txxx->description().toCString( true ), txxName ) )            \
            {                                                                            \
                p_txxx->setText( psz_meta );                                             \
                FREENULL( psz_meta );                                                    \
                break;                                                                   \
            }                                                                            \
        }                                                                                \
        if( psz_meta ) /* not found in existing custom fields */                         \
        {                                                                                \
            ByteVector p_byte( "TXXX", 4 );                                              \
            p_txxx = new ID3v2::UserTextIdentificationFrame( p_byte );                   \
            p_txxx->setDescription( txxName );                                           \
            p_txxx->setText( psz_meta );                                                 \
            free( psz_meta );                                                            \
            tag->addFrame( p_txxx );                                                     \
        }                                                                                \
    }
    WRITETXXX( TrackTotal, "TRACKTOTAL" );
#undef WRITETXXX
    char *psz_url = input_item_GetArtworkURL( p_item );
    if( psz_url == NULL )
        return;
    char *psz_path = vlc_uri2path( psz_url );
    free( psz_url );
    if( psz_path == NULL )
        return;
    const char *psz_mime = vlc_mime_Ext2Mime( psz_path );
    FILE *p_file = vlc_fopen( psz_path, "rb" );
    if( p_file == NULL )
    {
        free( psz_path );
        return;
    }
    struct stat st;
    if( vlc_stat( psz_path, &st ) == -1 )
    {
        free( psz_path );
        fclose( p_file );
        return;
    }
    off_t file_size = st.st_size;
    free( psz_path );
    if( file_size > 10485760 )
    {
      fclose( p_file );
      return;
    }
    char *p_buffer = new (std::nothrow) char[file_size];
    if( p_buffer == NULL )
    {
        fclose( p_file );
        return;
    }
    if( fread( p_buffer, 1, file_size, p_file ) != (unsigned)file_size )
    {
        fclose( p_file );
        delete[] p_buffer;
        return;
    }
    fclose( p_file );
    ByteVector data( p_buffer, file_size );
    delete[] p_buffer;
    ID3v2::FrameList frames = tag->frameList( "APIC" );
    ID3v2::AttachedPictureFrame *frame = NULL;
    if( frames.isEmpty() )
    {
        frame = new TagLib::ID3v2::AttachedPictureFrame;
        tag->addFrame( frame );
    }
    else
    {
        frame = static_cast<ID3v2::AttachedPictureFrame *>( frames.back() );
    }
    frame->setPicture( data );
    frame->setMimeType( psz_mime );
}
static void WriteMetaToId3v2 ( ID3v2 :: Tag * tag , input_item_t * p_item ) { char * psz_meta ; #define WRITE ( metaName , tagName )                                             \
    psz_meta = input_item_Get##metaName( p_item );                            \
    if( psz_meta )                                                            \
    {                                                                         \
        ByteVector p_byte( tagName, 4 );                                      \
        tag->removeFrames( p_byte );                                         \
        ID3v2::TextIdentificationFrame* p_frame =                             \
            new ID3v2::TextIdentificationFrame( p_byte, String::UTF8 );       \
        p_frame->setText( psz_meta );                                         \
        tag->addFrame( p_frame );                                             \
    }                                                                         \
    free( psz_meta ); WRITE ( Copyright , " " ) ; WRITE ( EncodedBy , " " ) ; WRITE ( Language , " " ) ; WRITE ( Publisher , " " ) ; #undef  WRITE ID3v2 :: FrameList list = tag -> frameListMap ( ) [ " " ] ; #define WRITETXXX ( metaName , txxName ) \
    psz_meta = input_item_Get##metaName( p_item );                                       \
    if ( psz_meta )                                                                      \
    {                                                                                    \
        ID3v2::UserTextIdentificationFrame *p_txxx;                                      \
        for( ID3v2::FrameList::Iterator iter = list.begin(); iter != list.end(); iter++ )\
        {                                                                                \
            p_txxx = dynamic_cast<ID3v2::UserTextIdentificationFrame*>(*iter);           \
            if( !p_txxx )                                                                \
                continue;                                                                \
            if( !strcmp( p_txxx->description().toCString( true ), txxName ) )            \
            {                                                                            \
                p_txxx->setText( psz_meta );                                             \
                FREENULL( psz_meta );                                                    \
                break;                                                                   \
            }                                                                            \
        }                                                                                \
        if( psz_meta ) /* not found in existing custom fields */                         \
        {                                                                                \
            ByteVector p_byte( "TXXX", 4 );                                              \
            p_txxx = new ID3v2::UserTextIdentificationFrame( p_byte );                   \
            p_txxx->setDescription( txxName );                                           \
            p_txxx->setText( psz_meta );                                                 \
            free( psz_meta );                                                            \
            tag->addFrame( p_txxx );                                                     \
        }                                                                                \
    } WRITETXXX ( TrackTotal , " " ) ; #undef  WRITETXXX char * psz_url = input_item_GetArtworkURL ( p_item ) ; if ( psz_url == NULL ) return ; char * psz_path = vlc_uri2path ( psz_url ) ; free ( psz_url ) ; if ( psz_path == NULL ) return ; const char * psz_mime = vlc_mime_Ext2Mime ( psz_path ) ; FILE * p_file = vlc_fopen ( psz_path , " " ) ; if ( p_file == NULL ) { free ( psz_path ) ; return ; } struct stat st ; if ( vlc_stat ( psz_path , & st ) == -1 ) { free ( psz_path ) ; fclose ( p_file ) ; return ; } off_t file_size = st . st_size ; free ( psz_path ) ; if ( file_size > 10485760 ) { fclose ( p_file ) ; return ; } char * p_buffer = new ( std :: nothrow ) char [ file_size ] ; if ( p_buffer == NULL ) { fclose ( p_file ) ; return ; } if ( fread ( p_buffer , 1 , file_size , p_file ) != ( unsigned ) file_size ) { fclose ( p_file ) ; delete [ ] p_buffer ; return ; } fclose ( p_file ) ; ByteVector data ( p_buffer , file_size ) ; delete [ ] p_buffer ; ID3v2 :: FrameList frames = tag -> frameList ( " " ) ; ID3v2 :: AttachedPictureFrame * frame = NULL ; if ( frames . isEmpty ( ) ) { frame = new TagLib :: ID3v2 :: AttachedPictureFrame ; tag -> addFrame ( frame ) ; } else { frame = static_cast < ID3v2 :: AttachedPictureFrame * > ( frames . back ( ) ) ; } frame -> setPicture ( data ) ; frame -> setMimeType ( psz_mime ) ; }
Write meta information to id3v2 tags
 @param tag: the id3v2 tag
 @param p_input: the input item
Write meta information to id3v2 tags @param tag : the id3v2 tag @param p_input : the input item
/* Known TXXX frames */ /* Write album art */ /* Limit picture size to 10MiB */