本文介绍了使用FFmpeg的检索专辑封面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我开发依赖于FFmpeg的检索音频的元数据的Andr​​oid应用程序。我知道这是可能检索专辑封面以编程方式使用FFmpeg的。但是,一旦你去codeD艺术(一个MP3中的视频帧)如何生成一个应用程序中的图像文件(PNG)中使用?我已经寻遍,但似乎无法找到工作的例子。

编辑,这里是解决方案:

 的#include< libav codeC / AV codec.h>
#包括<了libavformat / avformat.h>

无效retrieve_album_art(常量字符*路径,为const char * album_art_file){
    INT I,RET = 0;

    如果(!路径){
        的printf(Path是空的\ n);
        返回;
    }

    AVFormatContext * pFormatCtx = avformat_alloc_context();

    的printf(打开%s \ N,路径);

    //打开指定路径
    如果(avformat_open_input(安培; pFormatCtx,路径,NULL,NULL)!= 0){
        的printf(avformat_open_input()失败);
        GOTO失败;
    }

    //读取格式头
    如果(pFormatCtx-> iformat-> read_header(pFormatCtx)℃,){
        的printf(无法读取的格式头\ N);
        GOTO失败;
    }

    //找到第一个附加的图片,如果有
    对于(i = 0; I< pFormatCtx-> nb_streams;我++)
        如果(pFormatCtx->流[I]  - >处置&安培; AV_DISPOSITION_ATTACHED_PIC){
            AVPacket PKT = pFormatCtx->流[I]  - > attached_pic;
            FILE * album_art = FOPEN(album_art_file,世行);
            RET = FWRITE(pkt.data,pkt.size,1,album_art);
            fclose函数(album_art);
            av_free_packet(安培; PKT);
            打破;
        }

    如果(RET){
        的printf(中写道专辑封面到%s \ N,album_art_file);
    }

    失败:
        av_free(pFormatCtx);
        //这一行崩溃出于某种原因...
        // avformat_free_context(pFormatCtx);
}

诠释的main(){
    avformat_network_init();
    av_register_all();

    为const char * PATH =某些URL;
    为const char * album_art_file =某些路径;

    retrieve_album_art(路径,album_art_file);

    返回0;
}
 

解决方案

要以编程方式使用的ffmpeg,我想你将不得不调用的的()中libavformat流(这是的ffmpeg的一部分)。

在命令行中,可以明显地做到这一点:

 的ffmpeg -i input.mp3 -an -v codeC副本cover.jpg
 

的命令行行为意味着,封面图像被看作只是一个视频流(只包含一帧),因此,使用了libavformat以通常的方式将到DEMUX一个流的视频部分应产生该图像。

样品$ C $下多路分解:的ffmpeg /文档/例子/ demuxing.c 第一(只),将被从多路分解视频流中的MP3将包含JPEG文件(仍然连接codeD为JPEG,不是去codeD)获得AVPacket。

  AVFormatContext * fmt_ctx;
//设置fmt_ctx阅读第一视频流
AVPacket PKT;
av_read_frame(fmt_ctx,和放大器; PKT);
FILE *的image_file =的fopen(image.jpg的,世行);
INT结果= FWRITE(pkt.data,pkt.size,1,的image_file);
fclose函数(的image_file);
 

如果有多个图像,我想他们会被视为独立的视频流,而不是在同一个流作为单独的数据包。第一物流将是一个具有最大分辨率。

这一切都可能在内部read_apic来实现的()。

在ID3v2的规范允许任何图像格式,但建议JPEG或PNG。在实践ID3所有的图像是JPEG。

修改:将一些不太实用的位后记:

P.S。 的ffmpeg -i input.mp3 -f ffmetadata metadata.txt 将产生包含元数据的INI样的文件,但图像甚至没有提及在那里,所以这是不是一个有用的方法。

P.S。可能有多个图片在ID3v2标签。您可能需要处理的情况下,当有一个以上的图象或多于一种类型的图像present

P.S。 ffmpeg的可能不是这样做的最好的软件。使用 id3lib 标签库,或者一ID3 其他实现​​。这些可以被用来作为库(从您所选择的语言调用)或命令行实用程序。有样品C ++ $ C $下的TagLib这里:How我用标签库来读取不同的音频格式/写封面作品和id3lib在这里:的获得专辑封面。

I'm developing an Android application that relies on FFmpeg to retrieve audio metadata. I know it's possible to retrieve album art programmatically using FFMpeg. However, once you have decoded the art (a video frame within an MP3) how do generate an image file (a PNG) for use within an application? I've search all over but can't seem to find a working example.

Edit, here is the solution:

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

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;
}
解决方案

To use ffmpeg programmatically, I think you would have to call read_apic() in libavformat (which is part of ffmpeg).

From the commandline, you can apparently do this:

ffmpeg -i input.mp3 -an -vcodec copy cover.jpg

The commandline behaviour implies that the cover art image is seen as just another video stream (containing just one frame), so using libavformat in the usual way you would to demux the video part of a stream should produce that image.

Sample code for demuxing: ffmpeg/docs/examples/demuxing.c The first (and only) AVPacket that would be obtained from demuxing the video stream in an mp3 would contain the JPEG file (still encoded as JPEG, not decoded).

AVFormatContext* fmt_ctx;
// set up fmt_ctx to read first video stream
AVPacket pkt;
av_read_frame(fmt_ctx, &pkt);
FILE* image_file = fopen("image.jpg", "wb");
int result = fwrite(pkt.data, pkt.size, 1, image_file);
fclose(image_file);

If there are multiple images, I think they would be seen as separate video streams, rather than as separate packets in the same stream. The first stream would be the one with the largest resolution.

All this is probably implemented internally in terms of read_apic().

The ID3v2 spec allows for any image format, but recommends JPEG or PNG. In practice all images in ID3 are JPEG.

EDIT: Moved some of the less useful bits to postscript:

P.S. ffmpeg -i input.mp3 -f ffmetadata metadata.txt will produce an ini-like file containing the metadata, but the image is not even referred to in there, so that is not a useful approach.

P.S. There may be multiple images in an ID3v2 tag. You may have to handle the case when there is more than one image or more than one type of image present.

P.S. ffmpeg is probably not the best software for this. Use id3lib, TagLib, or one of the other implementations of ID3. These can be used either as libraries (callable from the language of your choice) or as commandline utilities. There is sample C++ code for TagLib here: How do I use TagLib to read/write coverart in different audio formats? and for id3lib here: How to get album art from audio files using id3lib.

这篇关于使用FFmpeg的检索专辑封面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:28