📄 meta.c
字号:
static void __ArtCacheGetFilePath( vlc_object_t *p_obj, char * psz_filename, const char *psz_title, const char *psz_artist, const char *psz_album, const char *psz_extension ){ char psz_dir[PATH_MAX+1]; char * psz_ext; ArtCacheGetDirPath( p_obj, psz_dir, psz_title, psz_artist, psz_album ); if( psz_extension ) { psz_ext = strndup( psz_extension, 6 ); filename_sanitize( psz_ext ); } else psz_ext = strdup( "" ); snprintf( psz_filename, PATH_MAX, "file://%s" DIR_SEP "art%s", psz_dir, psz_ext ); free( psz_ext );}static int __input_FindArtInCache( vlc_object_t *p_obj, input_item_t *p_item ){ char *psz_artist; char *psz_album; char *psz_title; char psz_dirpath[PATH_MAX+1]; char psz_filepath[PATH_MAX+1]; char * psz_filename; DIR * p_dir; psz_artist = input_item_GetArtist( p_item ); psz_album = input_item_GetAlbum( p_item ); psz_title = input_item_GetTitle( p_item ); if( !psz_title ) psz_title = input_item_GetName( p_item ); if( !psz_title && ( !psz_album || !psz_artist ) ) { free( psz_artist ); free( psz_album ); free( psz_title ); return VLC_EGENERIC; } ArtCacheGetDirPath( p_obj, psz_dirpath, psz_title, psz_artist, psz_album ); free( psz_artist ); free( psz_album ); free( psz_title ); /* Check if file exists */ p_dir = utf8_opendir( psz_dirpath ); if( !p_dir ) return VLC_EGENERIC; while( (psz_filename = utf8_readdir( p_dir )) ) { if( !strncmp( psz_filename, "art", 3 ) ) { snprintf( psz_filepath, PATH_MAX, "file://%s" DIR_SEP "%s", psz_dirpath, psz_filename ); input_item_SetArtURL( p_item, psz_filepath ); free( psz_filename ); closedir( p_dir ); return VLC_SUCCESS; } free( psz_filename ); } /* Not found */ closedir( p_dir ); return VLC_EGENERIC;}/** * Download the art using the URL or an art downloaded * This function should be called only if data is not already in cache */int input_DownloadAndCacheArt( playlist_t *p_playlist, input_item_t *p_item ){ int i_status = VLC_EGENERIC; stream_t *p_stream; char psz_filename[PATH_MAX+1]; char *psz_artist = NULL; char *psz_album = NULL; char *psz_title = NULL; char *psz_arturl; char *psz_type; psz_artist = input_item_GetArtist( p_item ); psz_album = input_item_GetAlbum( p_item ); psz_title = input_item_GetTitle( p_item ); if( !psz_title ) psz_title = input_item_GetName( p_item ); if( !psz_title && (!psz_artist || !psz_album) ) { free( psz_title ); free( psz_album ); free( psz_artist ); return VLC_EGENERIC; } psz_arturl = input_item_GetArtURL( p_item ); assert( !EMPTY_STR( psz_arturl ) ); if( !strncmp( psz_arturl , "file://", 7 ) ) { msg_Dbg( p_playlist, "Album art is local file, no need to cache" ); free( psz_arturl ); return VLC_SUCCESS; } else if( !strncmp( psz_arturl , "APIC", 4 ) ) { msg_Warn( p_playlist, "APIC fetch not supported yet" ); free( psz_arturl ); return VLC_EGENERIC; } psz_type = strrchr( psz_arturl, '.' ); if( psz_type && strlen( psz_type ) > 5 ) psz_type = NULL; /* remove extension if it's > to 4 characters */ /* Warning: psz_title, psz_artist, psz_album may change in ArtCache*() */ ArtCacheGetDirPath( p_playlist, psz_filename, psz_title, psz_artist, psz_album ); ArtCacheCreateDir( psz_filename ); ArtCacheGetFilePath( p_playlist, psz_filename, psz_title, psz_artist, psz_album, psz_type ); free( psz_artist ); free( psz_album ); free( psz_title ); p_stream = stream_UrlNew( p_playlist, psz_arturl ); if( p_stream ) { uint8_t p_buffer[65536]; long int l_read; FILE *p_file = utf8_fopen( psz_filename+7, "w" ); if( p_file == NULL ) { msg_Err( p_playlist, "Unable write album art in %s", psz_filename + 7 ); free( psz_arturl ); return VLC_EGENERIC; } int err = 0; while( ( l_read = stream_Read( p_stream, p_buffer, sizeof (p_buffer) ) ) ) { if( fwrite( p_buffer, l_read, 1, p_file ) != 1 ) { err = errno; break; } } if( fclose( p_file ) && !err ) err = errno; stream_Delete( p_stream ); if( err ) { errno = err; msg_Err( p_playlist, "%s: %m", psz_filename ); } else msg_Dbg( p_playlist, "album art saved to %s\n", psz_filename ); input_item_SetArtURL( p_item, psz_filename ); i_status = VLC_SUCCESS; } free( psz_arturl ); return i_status;}void input_ExtractAttachmentAndCacheArt( input_thread_t *p_input ){ input_item_t *p_item = p_input->p->input.p_item; const char *psz_arturl; const char *psz_artist = NULL; const char *psz_album = NULL; const char *psz_title = NULL; char *psz_type = NULL; char psz_filename[PATH_MAX+1]; FILE *f; input_attachment_t *p_attachment; struct stat s; int i_idx; /* TODO-fenrir merge input_ArtFind with download and make it set the flags FETCH * and then set it here to to be faster */ psz_arturl = vlc_meta_Get( p_item->p_meta, vlc_meta_ArtworkURL ); if( !psz_arturl || strncmp( psz_arturl, "attachment://", strlen("attachment://") ) ) { msg_Err( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" ); return; } if( input_item_IsArtFetched( p_item ) ) { /* XXX Weird, we should not have end up with attachment:// art url unless there is a race * condition */ msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" ); input_FindArtInCache( p_input, p_item ); return; } /* */ for( i_idx = 0, p_attachment = NULL; i_idx < p_input->p->i_attachment; i_idx++ ) { if( !strcmp( p_input->p->attachment[i_idx]->psz_name, &psz_arturl[strlen("attachment://")] ) ) { p_attachment = p_input->p->attachment[i_idx]; break; } } if( !p_attachment || p_attachment->i_data <= 0 ) { msg_Warn( p_input, "internal input error with input_ExtractAttachmentAndCacheArt" ); return; } psz_artist = vlc_meta_Get( p_item->p_meta, vlc_meta_Artist ); psz_album = vlc_meta_Get( p_item->p_meta, vlc_meta_Album ); psz_title = vlc_meta_Get( p_item->p_meta, vlc_meta_Title ); if( !strcmp( p_attachment->psz_mime, "image/jpeg" ) ) psz_type = strdup( ".jpg" ); else if( !strcmp( p_attachment->psz_mime, "image/png" ) ) psz_type = strdup( ".png" ); if( !psz_title ) psz_title = p_item->psz_name; if( (!psz_artist || !psz_album ) && !psz_title ) return; ArtCacheGetDirPath( p_input, psz_filename, psz_title, psz_artist, psz_album ); ArtCacheCreateDir( psz_filename ); ArtCacheGetFilePath( p_input, psz_filename, psz_title, psz_artist, psz_album, psz_type ); free( psz_type ); /* Check if we already dumped it */ if( !utf8_stat( psz_filename+7, &s ) ) { vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename ); return; } f = utf8_fopen( psz_filename+7, "w" ); if( f ) { if( fwrite( p_attachment->p_data, p_attachment->i_data, 1, f ) != 1 ) msg_Err( p_input, "%s: %m", psz_filename ); else { msg_Dbg( p_input, "album art saved to %s\n", psz_filename ); vlc_meta_Set( p_item->p_meta, vlc_meta_ArtworkURL, psz_filename ); } fclose( f ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -