ffmpeg结合SDL编写播放器(二)
我们将对帧数据做一些处理,比如将每一帧的 图像转为jpg或者bmp或者ppm等格式保存下来。
举例:在ffmpeg-2.8.8文件夹下编写test.c程序
/* test.c */ #include<stdio.h> #include<libavcodec/avcodec.h> #include<libavformat/avformat.h> #include<libswscale/swscale.h> int main(int argc, char *argv[]) { AVFormatContext *pFormatCtx = NULL; if (argc < 2) { printf("Please provide a movie file\n"); return -1; } av_register_all(); if (avformat_open_input(&pFormatCtx,argv[1], NULL, NULL) != 0) { return -1; } if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { return -1; } av_dump_format(pFormatCtx, 0, argv[1], 0); avformat_close_input(&pFormatCtx); return 0; }
注册ffmpeg库
av_register_all();
这会将所有可用的文件格式和编解码器注册到我们的库中,以便在打开具有相应格式/编解码器的文件时自动使用。 注意,我们只需要 av_register_all()一次,所以在 main()中调用。 如果你喜欢,可以只注册某些文件格式和编解码器,但是这样比较麻烦。
打开文件
AVFormatContext *pFormatCtx = NULL; if (avformat_open_input(&pFormatCtx,argv[1], NULL, NULL) != 0) { return -1; }
我们从第一个参数获取文件名。 此函数读取文件头并将有关文件格式的信息存储在我们给出的 AVFormatContext 结构中。 最后三个参数用于指定文件格式,缓冲区大小和格式选项,但通过将其设置为NULL或0,libavformat将自动检测这些。 此函数只查看头文件,所以接下来我们需要检查文件中的流信息:
if (avformat_find_stream_info(pFormatCtx, NULL) < 0)
此函数获取有效的数据流,没有就返回 -1。接下来编写一个方便的调试功能,可以显示我们资源的数据信息
av_dump_format(pFormatCtx, 0, argv[1], 0); //打印资源信息
makefile如下:
DIR_INC = -I/usr/local/include DIR_LIB = -L/usr/local/lib LIBS = -lavformat\ -lavcodec\ -lva-x11 \ -lva \ -lxcb-shm \ -lxcb-xfixes \ -lxcb-render \ -lxcb-shape \ -lxcb -lX11 \ -lasound \ -lz \ -lswresample \ -lswscale \ -lavutil \ -lm \ -pthread FLAGS = -Wall -ggdb test : test.c gcc test.c ${FLAGS} ${DIR_INC} ${DIR_LIB} ${LIBS} -o test .PHONY:clean clean: rm test
运行:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)