libav(ffmpeg)简明教程(1)

    突然发现又有好久没有写技术blog了,主要原因是最近时间都用来研究libav去了(因为api极类似ffmpeg,虽然出自同一份代码的另外一个分支,因项目选用libav,故下文均用libav代替),其实要从知道这个库的时候已经很久了,早在加入avplayer开源社区的已经略有耳闻,看着他们讨论我却一直不知这个库能具体帮我做到哪些功能,插不上嘴呢,更强迫了我学习它的热情,下面就来一一解惑,希望就能帮到类似几个月前的我那样的同行。

1、提供API解码、编码市面上主流几乎全部的视频、音频格式文件。

2、通用视频转换命令行工具ffmpeg、avconv,可以帮我们快速将媒体文件格式进行转换,且做一些简单的resize或者resample,其工具提供了非常强大的filter,各种变幻根据参数都能实现,没有你想不到的,只有你找不到的。

3、简单的播放器avplay命令,这个播放器支持libav所有能够支持的video codec,算个简单的万能播放器了,虽然seek功能弱爆了,并且还没有pause、stop、显示时间等等功能,不过有些应急时候绝对首选它了。

4、libav还提供avprobe命令,可以让你瞬间了解这个媒体文件其中真实的video/audio编码(不会受到文件扩展名的误导)、拥有哪些stream(一般MP4分为视频、音频、字幕),一目了然。

    读到此你一定会很感激我,不像大多数技术博客那样直接贴上很多大段大段的代码一下吓走好多初学者....我不能保证接下来一定不贴代码上来,但是我会尽量克制自己的....

    本文主要将以第1点API解码编码的介绍为主。因为libav是基于C实现的,调用习惯全是基于函数式的,这样的优点就是跨平台好吧,缺点就是会使client代码比较臃肿,到处充斥着free、alloc等等。如果你是一个纯面向对象发烧支持者,请不要往下看,以免伤身且药还不能停。

libav提供一个函数avformat_open_input,即打开一个媒体文件,用AVFormatContext指针接受返回结果,代码看起来就是这样:

AVFormatContext* pformat_context = avformat_alloc_context();
if(avformat_open_input(&pformat_context, file.c_str(), nullptr, 0) != 0)
{
     printf("can't open the file %s\n", file.c_str());
     return false;
}

然后你要做的是将所打开的FormatContext读取其中的stream,其中会有各种各样的stream类型,你需要做的事情就是将这个stream的index记录下来。

shared_ptr<AVFormatContext> format_context(pformat_context, [](AVFormatContext*& p){ avformat_close_input(&p); });
    if(avformat_find_stream_info(format_context.get(), nullptr) < 0)
    {
        printf("can't find suitable codec parameters\n");
        return false;
    }

    // find out the audio and video stream
    int video_stream_index = -1, audio_stream_index = -1;
    for(unsigned int i = 0; i < format_context->nb_streams && (video_stream_index == -1 || audio_stream_index == -1); i++)
    {
        if(format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            video_stream_index = i;
        }
        else if (format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            audio_stream_index = i;
        }
    }

    if(video_stream_index == -1 && audio_stream_index == -1)
    {
        printf("input file contains no video stream or audio stream.\n");
        return false;
    }

对照stream可以使用avprobe命令查看视频文件本身的内容。
获取到stream信息之后,你就需要创建decoder来解码视频啦~ libav提供一个函数avcodec_find_decoder根据你自己找到的video index和audio index去寻找codec_id作为参数得到AVCodec指针,再使用函数avcodec_open2传入这个指针即可。

// open the video decoder
    AVCodecContext* video_codec_context = nullptr;
    if (video_stream_index != -1)
    {
        video_codec_context = format_context->streams[video_stream_index]->codec;
        AVCodec* video_codec = avcodec_find_decoder(video_codec_context->codec_id);
        if(video_codec == nullptr)
        {
            printf("can't find suitable video decoder\n");
            return false;
        }
        if(avcodec_open2(video_codec_context, video_codec, nullptr) < 0)
        {
            printf("can't open the video decoder\n");
            return false;
        }
    }

    // open the audio decoder
    AVCodecContext* audio_codec_context = nullptr;
    if (audio_stream_index != -1)
    {
        audio_codec_context = format_context->streams[audio_stream_index]->codec;
        AVCodec* audio_codec = avcodec_find_decoder(audio_codec_context->codec_id);
        if (audio_codec == nullptr)
        {
            printf("can't find suitable audio decoder\n");
            return false;
        }
        if (avcodec_open2(audio_codec_context, audio_codec, nullptr) < 0)
        {
            printf("can't open the audio decoder\n");
            return false;
        }
    }

 注意,open之后一定要调用对应的api close,比方avformat_close_input这些都是必备的,就不全贴出来了。

    接下来讲这课程最重要的部分——decode video,先创建用于接收av_read_frame读出来的数据包,

AVPacket packet = {0};
av_init_packet(&packet);

然后使用一个循环调用av_read_frame,查注释你会知道return>=0为成功,然后判断packet的stream_index是video_stream_index还是audio_stream_index,从而使用不同的decode函数(avcodec_decode_video2 / avcodec_decode_audio4)做解码,视频如果是MP4将得到AV_PIX_FMT_YUV420P数据,音频将得到原始音频AV_SAMPLE_FMT_FLTP采样数据。

但是我们一般不会使用YUV420P进行图像、视频处理,而是使用bitmap来进行处理,所以需要在这里借助另外一个函数sws_scale,第一个参数查看源码了解到是一个结构体struct,并不需要手动填充它,而且你也没办法手动填充它,libav并不希望你这么做(没有将细节写在include中),因此有一个sws_getContext函数是专门做这件事情的。

struct SwsContext *sws_getContext(
       int srcW, int srcH, enum AVPixelFormat srcFormat,
       int dstW, int dstH, enum AVPixelFormat dstFormat,
       int flags, SwsFilter *srcFilter,
       SwsFilter *dstFilter, const double *param);

看到参数你就能很容易的猜到,你需要提供原视频的尺寸和格式,可以在已打开的视频的codec中获得,目标视频尺寸你自己随便设置都可以,dstFormat可以设置为:AV_PIX_FMT_BGRA,更多可以参见:pixfmt.h 中的 enum AVPixelFormat,如果是BGRA,图片则为32位,包含透明通道,方便之后叠加图层处理。如果读者跟着我的步骤走,应该就能达到连续输出图片的功能了,再加入图像识别的更多功能:脸谱识别、手势识别、车牌识别,就直接可以用了,是不是很激动?

posted @ 2014-07-01 18:11  重庆Debug  阅读(4347)  评论(2编辑  收藏  举报