ffmpeg 4.4 代码学习

读音视频文件流程:

  1. 打开文件

char *inUrl = "D:\\TestFiles\\test.mp4";

AVFormatContext *ictx = NULL;

avformat_open_input(&ictx, inUrl, 0, 0);

2. 查看和显示流信息(如果需要)

ret = avformat_find_stream_info(ictx, 0);

av_dump_format(ictx, 0, inUrl, 0);  

3. 找音视频流

int vstreamindex = -1, astreamindex = -1;

    for(int i=0; i< ictx->nb_streams; i++)
    {
        AVStream *instream= ictx->streams[i];
        if ( instream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            vstreamindex = i;
        }
        else if (instream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            astreamindex = i;
        }
    }

4. 得到解码器

AVCodec *avcodec_find_decoder(enum AVCodecID id);
AVCodec *avcodec_find_encoder(enum AVCodecID id);

AVCodec *vcodec;
    vcodec = avcodec_find_decoder(ictx->streams[vstreamindex]->codecpar->codec_id);

5. 先得到解码器后,再得到AVCodecContext

AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);

AVCodecContext *vctx;

vctx = avcodec_alloc_context3(vcodec);

6. 将音频视参数传送给AVCodecContext

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

avcodec_parameters_to_context(vctx, ictx->streams[vstreamindex]->codecpar);

7. 打开解码器

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

avcodec_open2(vctx,vcodec,NULL);

8. 分配packet和frame指针

  AVFrame *pFrame;
    AVPacket *packet;

    packet = av_packet_alloc();
    pFrame = av_frame_alloc();

9. 开始while循环读数据流

while (av_read_frame(ictx, packet) >=0)

{

        if (packet->stream_index == vstreamindex) {
                avcodec_send_packet(vctx, packet);
                ret = avcodec_receive_frame(vctx, pFrame);

                if (ret == 0 ) {
                    av_log(vctx,AV_LOG_DEBUG,"vframe count:%d\n",count++);
                }

av_packet_unref(packet);

}

10. 完成后释放所有指针。
    av_frame_free(&pFrame);
    av_packet_free(&packet);
    avcodec_close(vctx);
    avcodec_free_context(&vctx);
    avformat_close_input(&ictx);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值