FFmpeg的初识

    xiaoxiao2021-03-26  18

    前言:一直以来,都想下决定写博文,但是都因为懒惰没下手,这次被我老大强制要求我每周更新一篇博文,觉得也是一个契机。所以也就借这次机会开始试试。 音视频的编解码,一直给我的感觉是太难。FFmpeg作为国内外使用最为广泛的跨平台的音视频编解码的框架,包括暴风影音、QQ影音的内核都使用的是FFmpeg。FFmpeg学习起来也是非常痛苦的。下面来写一个简单的播放器的实例。(FFmpeg的导入过程我就不详细讲解了,网上很多实例,我这里使用的是iOS平台,代码直接移植应该是没有问题的)。 所有使用到FFmpeg框架的过程中,所有基于FFmpeg的应用程序中第一个被调用的函数是:

    av_register_all();

    这句代码是注册复用器、编码器等。只有调用了这个函数才能使用复用器、编码器等。 第二步:网络流的初始化(如果需要打开网络流的情况下)

    avformat_network_init();

    第三步:打开视频文件,函数原型如下

    int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options)

    该函数的第一个参数ps是一个指针类型,指向的是用户配置的AVFormatContext,第二个参数url是读取的视频地址(本地地址或网络地址都行),第三个参数fmt表示一个特定的输入格式,否则格式为空,一般情况为NULL即可,第四个参数optionls是指向带有AVFormatContext的字典的指针,一般情况为NULL即可 第四步:查找文件的流信息,函数原型如下

    int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);

    该函数是获取视频流信息的方法,该方法的第一个参数为视频的AVFormatContext对象,即第三步中获取到的ps对象。第二个参数一般情况为NULL,如果非空,即是一个ic.nb_streams长数组的指针字典 第五步:输出文件的音、视频流的基本信息,帧率、分辨率、音频采样等等(此步骤为调试函数,可以不添加)

    void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output);

    该函数为获取调试信息的方法,该方法中的第一个参数为视频的AVFormatContext对象,index为需要获取流的索引,url为视频的路径(本地路径或者网络路径都可以),is_output获取输入(0)还是输出(1)参数。 第六步:循环各个流,找到音、视频、字幕等流信息,并记录该流的编码信息

    for (int i = 0; i < pFormatCtx->nb_streams; i++) { switch (pFormatCtx->streams[i]->codecpar->codec_type) { case AVMEDIA_TYPE_VIDEO: { videoindex = i; } break; default: break; } }

    第七步:拷贝结构体信息(最新API才需要做这一步,当然目前按照老方法还是可以使用的)

    int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par);

    该函数是拷贝par中的信息到codec中,因为最新api中废弃了AVFormatContext中的AVCodecContext字段,改为AVCodecParameters了,结构体发生了变化,而很多其他方法中仍然使用的是AVCodecContext,所以这里我们需要做一个简单的转换。 第八步:在库中查找支持该格式的解码器

    AVCodec *avcodec_find_decoder(enum AVCodecID id);

    函数的参数即需要查找流的id,可以从第七步中获取到的AVCodecContext结构体中得到 第九步:打开解码器

    int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);

    该方法的第一个参数avctx即是上面得到的AVCodecContext,codec为第八步查找到的解码器参数,options为包含AVCodecContext和编解码器的字典指针。 第十步:分配指针来存储帧

    av_frame_alloc();

    第十一步:逐帧读取音、视频数据

    while(true) { av_read_frame();//读取一个帧(到最后帧则break) avcodec_decode_video2();//解码该帧 sws_getCachedContext()sws_scale();//把该帧转换(渲染)成RGB SaveFrame();//对前5帧保存成ppm图形文件(这个是自定义函数,非API) av_free_packet();//释放本次读取的帧内存 }

    总结起来的代码如下

    AVFormatContext *pFormatCtx; int videoindex; AVCodecParameters *pCodecParam; AVCodecContext *pCodeCtx; AVCodec *pCodec; AVFrame *pFrame,*pFrameYUV; uint8_t *out_buffer; AVPacket *packet; int ret; const char *filepath = [[[NSBundle mainBundle] pathForResource:@"cuc_ieschool" ofType:@"mp4"] UTF8String]; av_register_all(); avformat_network_init(); pFormatCtx = avformat_alloc_context(); if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0) { NSLog(@"Couldn't open input Stream.\n"); return ; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { NSLog(@"Coundn't find stream.\n"); return; } videoindex = -1; for (int i = 0; i < pFormatCtx->nb_streams; i++) { switch (pFormatCtx->streams[i]->codecpar->codec_type) { case AVMEDIA_TYPE_VIDEO: { videoindex = i; } break; default: break; } } if (videoindex == -1) { NSLog(@"视频读取失败"); return; } //av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &pCodec, 0); pCodecParam = pFormatCtx->streams[videoindex]->codecpar; pCodeCtx = avcodec_alloc_context3(NULL); if (avcodec_parameters_to_context(pCodeCtx, pCodecParam) < 0) { NSLog(@"not convert"); return; } pCodec = avcodec_find_decoder(pCodeCtx->codec_id); if(avcodec_open2(pCodeCtx, pCodec, NULL) < 0){ printf("Couldn't open codec.\n"); return; } if (pCodec==NULL) { NSLog(@"获取解码失败"); return; } pFrame = av_frame_alloc(); pFrameYUV = av_frame_alloc(); out_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecParam->width, pCodecParam->height, 1)); av_image_fill_arrays(pFrame->data, pFrame->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecParam->width, pCodecParam->height, 1); packet = (AVPacket *)av_malloc(sizeof(AVPacket)); av_dump_format(pFormatCtx, 0, filepath, 0); while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == videoindex) { ret = avcodec_send_packet(pCodeCtx, packet); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) { av_packet_unref(packet); return ; } ret = avcodec_receive_frame(pCodeCtx, pFrameYUV); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) { av_packet_unref(packet); return; } else { [playerView displayYUV420pData:pFrameYUV]; } if (pFrameYUV->pict_type == AV_PICTURE_TYPE_I) { const int64_t frameDuration = av_frame_get_pkt_duration(pFrameYUV); NSLog(@"frameduration = %lld", pFrameYUV->pts); } } else { NSLog(@"audio"); } av_packet_unref(packet); } av_frame_free(&pFrameYUV); av_frame_free(&pFrame); avcodec_close(pCodeCtx); avcodec_parameters_free(&pCodecParam); //avformat_close_input(&pFormatCtx);

    该代码实现了逐帧读取视频画面,音频以及时间线控制下次继续聊。

    第一次写博文,不好的地方望大家多指教

    转载请注明原文地址: https://ju.6miu.com/read-658534.html

    最新回复(0)