我正在开发一个依赖于FFmpeg来检索音频元数据的
Android应用程序.我知道可以使用FFMpeg以编程方式检索专辑封面.但是,一旦您解码了艺术(MP3中的视频帧),如何生成图像文件(PNG)以便在应用程序中使用?我搜遍了所有但似乎无法找到一个有效的例子.
编辑,这是解决方案:
#include
#include
void retrieve_album_art(const char *path,const char *album_art_file) {
int i,ret = 0;
if (!path) {
printf("Path is NULL\n");
return;
}
AVFormatContext *pFormatCtx = avformat_alloc_context();
printf("opening %s\n",path);
// open the specified path
if (avformat_open_input(&pFormatCtx,path,NULL,NULL) != 0) {
printf("avformat_open_input() Failed");
goto fail;
}
// read the format headers
if (pFormatCtx->iformat->read_header(pFormatCtx) < 0) {
printf("could not read the format header\n");
goto fail;
}
// find the first attached picture,if available
for (i = 0; i < pFormatCtx->nb_streams; i++)
if (pFormatCtx->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
AVPacket pkt = pFormatCtx->streams[i]->attached_pic;
FILE* album_art = fopen(album_art_file,"wb");
ret = fwrite(pkt.data,pkt.size,1,album_art);
fclose(album_art);
av_free_packet(&pkt);
break;
}
if (ret) {
printf("Wrote album art to %s\n",album_art_file);
}
fail:
av_free(pFormatCtx);
// this line crashes for some reason...
//avformat_free_context(pFormatCtx);
}
int main() {
avformat_network_init();
av_register_all();
const char *path = "some url";
const char *album_art_file = "some path";
retrieve_album_art(path,album_art_file);
return 0;
}