AVFormatContext协议层:理论与实战(二)

简介: AVFormatContext协议层:理论与实战(二)

AVFormatContext协议层:理论与实战(一)https://developer.aliyun.com/article/1473889

这里有 avformat_alloc_context 操作,但是没有 avformat_free_context()操作,既然官方用例都这么做,那我也索性按照官方测试用例的方法来。

我看到网上相关的资料要求必须要有这个 free 操作,但这里一旦加了 free 操作就会出现这样的错误,这个问题暂时放在这儿吧,也希望有懂得兄弟可以解释一下。

屏蔽 avformat_free_context():

再次运行,可以看到如下输出结果:

hello,ffmpeg
hello,avformat_alloc_context
hello,avformat_open_input
open stream success!
av_find_stream_info success 
******nb_streams=2

可以看到输出信息为找到了两路流,分别是视频流与音频流,用 MediaInfo 查看 test.mp4 可以看到一致的效果

②、解决方法 2

使用时可以通过 avformat_alloc_context 分配后使用,也可以直接 avformat_open_input。

>//1、方法一
AVFormatContext *fmt_ctx = NULL;
string filename = "test.avi" ;
fmt_ctx = avformat_alloc_context();
avformat_open_input(&fmt_ctx, ilename.c_str(), NULL, NULL);
avformat_close_input(&fmt_ctx);
>//2、方法二
AVFormatContext *fmt_ctx = NULL;
string filename = "test.avi" ;
int ret = avformat_open_input(&fmt_ctx, filename.c_str(), NULL, NULL);
avformat_close_input(&fmt_ctx);

推荐使用方法二,因为若传进 avformat_open_input 的 fmt_ctx 为 NULL,该函数内部会调用 avformat_alloc_context 函数。相应的 avformat_close_input 内部会调用 avformat_free_context。

修改后的源码:

extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libavformat/avio.h>
    #include <libavutil/file.h>
};
int main(int argc, char* argv[])
{
    /av_register_all();
    avformat_network_init();
    printf("hello,ffmpeg\n");
    AVFormatContext* pFormatCtx = NULL;
    printf("hello,avformat_open_input\n");
    //打开本地文件或网络直播流
    //rtsp://127.0.0.1:8554/rtsp1
    //./debug/test.mp4
    if (avformat_open_input(&pFormatCtx, "./debug/test.mp4", NULL, NULL) < 0) {
        printf("avformat open failed.\n");
        goto quit;
    }
    else {
        printf("open stream success!\n");
    }
    if (avformat_find_stream_info(pFormatCtx, NULL)<0)
    {
        printf("av_find_stream_info error \n");
        goto quit;
    }
    else {
        printf("av_find_stream_info success \n");
        printf("******nb_streams=%d\n",pFormatCtx->nb_streams);
    }
quit:
    avformat_close_input(&pFormatCtx);
    avformat_network_deinit();
    return 0;

运行结果:

hello,ffmpeg
hello,avformat_open_input
open stream success!
av_find_stream_info success 
******nb_streams=2

四、avio 实战 2:自定义 AVIO

本次实战的目的与实战 1 的目的一致,均是分析输入文件的流数量,只不过本次实战重点突出使用我们自定义的 AVIO 来打开文件。

重点理解 AVFormatContext 的 pb 字段指向一个 AVIOContext,如何完成的关联和绑定

1、示例源码

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
int read_func(void* ptr, uint8_t* buf, int buf_size)
{
    FILE* fp = (FILE*)ptr;
    size_t size = fread(buf, 1, buf_size, fp);
    int ret = size;
    printf("Read Bytes:%d\n", size);
    return ret;
}
int64_t seek_func(void *opaque, int64_t offset, int whence)
{
    int64_t ret;
    FILE *fp = (FILE*)opaque;
    if (whence == AVSEEK_SIZE) {
        return -1;
    }
    fseek(fp, offset, whence);
    return ftell(fp);
}
int main(int argc, char *argv[]){
    ///av_register_all();
    printf("hello,ffmpeg\n");
    int ret = 0;
    FILE* fp = fopen("./debug/test.mp4", "rb");
    int nBufferSize = 1024;
    unsigned char* pBuffer = (unsigned char*)malloc(nBufferSize);
    AVFormatContext* pFormatCtx = NULL;
    AVInputFormat *piFmt = NULL;
    printf("hello,avio_alloc_context\n");
    // Allocate the AVIOContext:
    AVIOContext* pIOCtx = avio_alloc_context(
        pBuffer,
        nBufferSize,
        0,
        fp,
        read_func,
        0,
        seek_func);
    printf("hello,avformat_alloc_context\n");
    // Allocate the AVFormatContext:
    pFormatCtx = avformat_alloc_context();
    // Set the IOContext:
    pFormatCtx->pb = pIOCtx;//关联,绑定
    pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
    printf("hello,avformat_open_input\n");
    //打开流
    if (avformat_open_input(&pFormatCtx, "", piFmt, NULL) < 0) {
        printf("avformat open failed.\n");
        goto quit;
    }
    else {
        printf("open stream success!\n");
    }
    if (avformat_find_stream_info(pFormatCtx, NULL)<0)
    {
        printf("av_find_stream_info error \n");
        goto quit;
    }
    else {
        printf("av_find_stream_info success \n");
        printf("******nb_streams=%d\n",pFormatCtx->nb_streams);
    }
quit:
    //avformat_free_context(pFormatCtx);
    avformat_close_input(&pFormatCtx);
    free(pBuffer);
    return 0;
}
  • avio_alloc_context():用于分配和初始化 AVIOContext 结构体。AVIOContext 结构体表示用于读取或写入数据的 I/O 上下文,它提供了一个抽象的层次,用于访问各种类型的数据。

2、运行结果

10:38:51: Starting D:\Project\Qt_Project\1-2_fmpeg431s5qtcodes\build-HelloFFmpeg-Desktop_Qt_5_14_2_MinGW_32_bit-Debug\debug\HelloFFmpeg.exe ...
hello,ffmpeg
hello,avio_alloc_context
hello,avformat_alloc_context
hello,avformat_open_input
Read Bytes:2048
Read Bytes:1024
Read Bytes:11457
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:21496
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:1024
Read Bytes:580
open stream success!
Read Bytes:1024
Read Bytes:105165
av_find_stream_info success 
******nb_streams=2

AVFormatContext协议层:理论与实战(三)https://developer.aliyun.com/article/1473891

目录
相关文章
|
Web App开发 JavaScript 程序员
如何去掉或者修改网页中的遮罩层,DIV等元素?
起因: 前几天女友在查资料写文章,打开一个网页,发现网页的被一个半透明的遮罩层给覆盖了(一个已经过期的组织活动 的网页,H5就将它遮盖了),就来问我怎么弄。
2439 0
|
2月前
|
人工智能 数据挖掘 BI
2026 年 QwenWork 千问办公解读:六大核心能力一览,Qwen3.8 赋能企业流程自动化,附计费指南
综合来看,千问办公QwenWork依托Qwen3.8基座,依靠六大核心能力打通文档处理、数据分析、浏览器自动化、自定义技能编排、多端Agent协同、企业业务系统,将AI能力从简单对话问答升级为完整业务任务交付,解决企业大量重复性办公工作。但办公智能体本质属于效率辅助工具,不能够完全替代业务人员专业判断,财务、法务这类关键业务输出,一定要经过人工审核校验。企业落地实施过程中区分上层SaaS产品和底层API两条落地路线,简单业务直接使用QwenWork平台,高并发高度定制化业务选择底层API自建系统,合理完成版本选型以及积分成本管控,充分释放办公自动化带来的生产力提升效果。
361 0
|
4月前
|
人工智能 自然语言处理 机器人
阿里云智启AI普惠权益活动解析:至高享 1亿+免费 tokens,加速AI应用落地
本文聚焦阿里云“智启 AI 普惠权益”活动,面向开发者与企业用户推出至高1亿+免费Tokens的核心普惠权益,覆盖六大核心板块:150余款直供模型全模型通享低至4.5折、支持多智能体并发的灵活Token Plan订阅方案、Qwen3.7-Max限时5折与HappyHorse限时6折专属优惠、全尺寸全模态千问旗舰模型探索专区、通义万相与Qwen-Image图片视频生成资源包,以及开箱即用的大模型创新场景应用。整套权益从模型调用、订阅方案到落地应用形成完整链路,大幅降低AI使用门槛,助力不同规模的用户以低成本迈入AI生产力时代。
|
5月前
|
存储 人工智能 自然语言处理
AI大模型重塑企业知识底座:从RAG到知识智能的技术全景解读
本文从技术决策者视角,系统梳理AI大模型驱动下企业知识库的演进脉络:从目录管理、关键词搜索、向量检索,到RAG架构支撑的“知识智能”时代;深度解析查询理解、多路检索、知识切片、答案溯源等关键技术,并探讨知识图谱、行业垂直化与安全可控架构趋势,为企业技术选型提供务实参考。(239字)
653 0
|
大数据 测试技术 API
AVFormatContext协议层:理论与实战(一)
AVFormatContext协议层:理论与实战(一)
390 0
|
11月前
|
JavaScript 前端开发 Shell
技术实践:快速批量下载网站图片资源实现方法
本文介绍了三种批量下载图片的方法:使用HTML页面、Node.js脚本和Bash结合Wget命令。重点推荐第三种,仅需几行命令即可高效完成下载,无需编程环境,兼容性强,适合快速批量获取网络图片资源。
1421 0
|
9月前
|
人工智能 Linux API
AI龙虾员工🦞从孵化到创收指南:OpenClaw全平台部署(阿里云/Win11/MacOS/Linux)+百炼API配置+技能解锁+FAQ
“睡觉时自动开发功能,醒来收获万元月收入”——这不是科幻情节,而是开发者AlexFinn通过OpenClaw实现的真实案例。2026年,这款被称为“小龙虾”的开源AI智能体以14.5万GitHub星标、超100万独立部署实例的成绩,成为AI领域的现象级工具。它彻底打破传统AI的“被动响应”局限,凭借自我改进、主动工作、完全定制三大核心能力,从“聊天机器人”进化为真正的“数字员工”。
574 2
|
编解码 缓存 Ubuntu
在Ubuntu平台搭建RTMP直播服务器使用SRS简要指南
这个简要指南提供了搭建基本 RTMP 直播服务所需知道信息. SRS 还提供许多高级特性如 DVR、转码、边缘缓存等. 若要利用这些特性, 需要进一步阅读官方文档并相应地调整配置文件.
1197 0
|
JavaScript Java 关系型数据库
基于springboot的高校运动会系统
本系统基于Spring Boot、Vue与MySQL,实现高校运动会报名、赛程安排及成绩管理的全流程信息化,提升组织效率,杜绝信息错漏与冒名顶替,推动体育赛事智能化发展。
|
11月前
|
自然语言处理 监控 数据可视化
​​​​​​​挖掘用户心声:利用京东评论API驱动产品与服务优化
本文介绍如何通过京东评论API获取商品用户评价数据,结合数据分析与NLP技术,挖掘用户真实反馈,进行评分分布、情感分析、关键词提取与主题建模,并通过可视化手段呈现洞察,最终驱动产品优化、服务升级与用户体验提升,实现数据驱动的精细化运营。(238字)

热门文章

最新文章