C语言结合ffmpeg如何打印音视频信息
发表于:2024-10-04 作者:千家信息网编辑
千家信息网最后更新 2024年10月04日,今天就跟大家聊聊有关C语言结合ffmpeg如何打印音视频信息,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。一、通过此文可以得到什么通过此练习:
千家信息网最后更新 2024年10月04日C语言结合ffmpeg如何打印音视频信息
今天就跟大家聊聊有关C语言结合ffmpeg如何打印音视频信息,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一、通过此文可以得到什么
通过此练习:
1、知道了如何计算一个音频和视频的播放时间;
2、知道了音视频解码的思路的大体流程,之后无非就是在这个流程上进行扩充细节;
3、知道了如何通过C语言或者C++编程语言结合ffmpeg拿到一些音视频的关键信息,例如:帧率等;
二、实现思路
三、实现效果
zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ makemake allmake[1]: 进入目录"/data/project/VSCProject/ffmpegStudy/src"Compiling main.cpp to main.o ..cp hello /data/project/VSCProject/ffmpegStudy/src/../# make 【hello】 finish !!! make[1]: 离开目录"/data/project/VSCProject/ffmpegStudy/src"zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ zhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$ ./hello Hello World !decode video fileName=/home/zhenghui/视频/1080P.mp4 [NULL @ 0x5654cc332d80] Opening '/home/zhenghui/视频/1080P.mp4' for reading[file @ 0x5654cc333380] Setting default whitelist 'file,crypto,data'[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Format mov,mp4,m4a,3gp,3g2,mj2 probed with size=2048 and score=100[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] ISO: File Type Major Brand: isom[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Unknown dref type 0x206c7275 size 12[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Processing st: 0, edit list 0 - media time: 1072, duration: 3621888[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Offset DTS by 1072 to make first pts zero.[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Setting codecpar->delay to 2 for stream st: 0[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Unknown dref type 0x206c7275 size 12[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Processing st: 1, edit list 0 - media time: 0, duration: 9990149[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 29.916667 0.006250[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 30.000000 0.000066[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 60.000000 0.000265[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 120.000000 0.001061[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 240.000000 0.004244[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 29.970030 0.000868[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] rfps: 59.940060 0.003473[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] Before avformat_find_stream_info() pos: 87425182 bytes read:315205 seeks:1 nb_streams:2[h364 @ 0x5654cc3340c0] nal_unit_type: 7(SPS), nal_ref_idc: 3[h364 @ 0x5654cc3340c0] nal_unit_type: 8(PPS), nal_ref_idc: 3[h364 @ 0x5654cc3340c0] nal_unit_type: 7(SPS), nal_ref_idc: 3[h364 @ 0x5654cc3340c0] nal_unit_type: 8(PPS), nal_ref_idc: 3[h364 @ 0x5654cc3340c0] nal_unit_type: 6(SEI), nal_ref_idc: 0[h364 @ 0x5654cc3340c0] nal_unit_type: 5(IDR), nal_ref_idc: 3[h364 @ 0x5654cc3340c0] Format yuv420p chosen by get_format().[h364 @ 0x5654cc3340c0] Reinit context to 1920x1088, pix_fmt: yuv420p[h364 @ 0x5654cc3340c0] no picture [mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] All info found[mov,mp4,m4a,3gp,3g2,mj2 @ 0x5654cc332d80] After avformat_find_stream_info() pos: 53231 bytes read:393067 seeks:2 frames:4video_index=0 audio_index=1 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/zhenghui/视频/1080P.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.29.100 description : Packed by Bilibili XCoder v2.0.2 Duration: 00:03:46.53, start: 0.000000, bitrate: 3087 kb/s Stream #0:0[0x1](und), 3, 1/16000: Video: h364 (High), 1 reference frame (avc1 / 0x31637661), yuv420p(progressive, left), 1920x1080 (1920x1088) [SAR 1:1 DAR 16:9], 0/1, 2951 kb/s, 30 fps, 30 tbr, 16k tbn (default) Metadata: handler_name : VideoHandler vendor_id : [0][0][0][0] Stream #0:1[0x2](und), 1, 1/44100: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default) Metadata: handler_name : SoundHandler vendor_id : [0][0][0][0]media name =/home/zhenghui/视频/1080P.mp4 stream number: 2 media average ratio: 3015 kbpsmedia total time: 0:3:46 ######## Video info: ######## stream index : 0 fps:29.999914 fpsvideo codec : H264 video_time : 00:03:46######## Audio info: ######## stream index : 1 stream sample_rate : 44100 Hz stream format : AV_SAMPLE_FMT_FLTP channels number:2 audio codec:AAC audio_time : 00:03:46[AVIOContext @ 0x5654cc33b7c0] Statistics: 393067 bytes read, 2 seekszhenghui@zh-pc:/data/project/VSCProject/ffmpegStudy$
四、实现源代码
视频教程
#include#include #ifdef __cplusplus //表示是一个c++程序extern "C"{#endif#include #include #include #include #include #include #include #ifdef __cplusplus}#endif//g++ -I ../include/ hello_world.cpp -o hello_world -L../lib/ -lavcodec -lavdevice -lavfilter -lavformat -lavutilvoid decode(){ char *fileName = "/home/zhenghui/视频/1080P.mp4"; printf("decode video fileName=%s \n",fileName); int ret = 0; // 1、分配解复用器上下文 AVFormatContext *ctx = NULL; ctx = avformat_alloc_context(); // 2、根据url打开本地文件或网络流 ret = avformat_open_input(&ctx,fileName,NULL,NULL); if(ret < 0) { printf("打开%s失败 ! \n",fileName); return; } // 3、读取媒体的部分数据包,获取码流信息 ret = avformat_find_stream_info(ctx,NULL); if(ret < 0){ printf("获取码流信息失败 !\n"); return; } // 3、获取音频流和视频流的索引 int video_index = -1; int audio_index = -1; video_index = av_find_best_stream(ctx,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0); printf("video_index=%d \n",video_index); audio_index = av_find_best_stream(ctx,AVMEDIA_TYPE_AUDIO,-1,-1,NULL,0); printf("audio_index=%d \n",audio_index); // 打印流信息 av_dump_format(ctx,0,fileName,0); //打印媒体文件url printf("media name =%s \n",ctx->url); //打印流数量 printf("stream number: %d \n",ctx->nb_streams); //打印码率 printf("media average ratio: %lld kbps\n",(int64_t)(ctx->bit_rate/1024)); //打印时长 int total_seconds,hour,minute,second; //ctx->duration的单位是微妙,先转换成秒,再进行转换成其他的时间单位就比较简单了 total_seconds = (ctx->duration) / AV_TIME_BASE; hour = total_seconds / 3600; minute = (total_seconds % 3600) /60; second = (total_seconds % 60); // 文件总时长 printf("media total time: %d:%d:%d \n",hour,minute,second); //老版本查找流索引 for(int i = 0;i < ctx->nb_streams;i++){ AVStream *av_stream = ctx->streams[i]; //判断流是什么类型 //视频流 if( AVMEDIA_TYPE_VIDEO == av_stream->codecpar->codec_type) { printf("######## Video info: ######## \n"); // 流索引 printf("stream index : %d \n",av_stream->index); //帧率 printf("fps:%lf fps\n",av_q2d(av_stream->avg_frame_rate)); if(AV_CODEC_ID_MPEG4 == av_stream->codecpar->codec_id) { printf("video codec : MPEG4 \n"); } else if(AV_CODEC_ID_H264 == av_stream->codecpar->codec_id) { printf("video codec : H264 \n"); }else { printf("video codec other value=%d \n", av_stream->codecpar->codec_id); } //获取视频总时长 if(AV_NOPTS_VALUE != av_stream->duration) { int video_time =av_stream->duration * av_q2d(av_stream->time_base); printf("video_time : d:d:d\n" , (video_time / 3600), (video_time % 3600) / 60, (video_time % 60) ); } else{ printf("audio duration unknown ! \n"); } } //音频流 else if(AVMEDIA_TYPE_AUDIO == av_stream->codecpar->codec_type) { printf("######## Audio info: ######## \n"); // 流索引 printf("stream index : %d \n",av_stream->index); // 音频编解码器的采样率 单位Hz printf("stream sample_rate : %d Hz \n",av_stream->codecpar->sample_rate); //音频采样格式 if(AV_SAMPLE_FMT_FLTP == av_stream->codecpar->format) { printf("stream format : AV_SAMPLE_FMT_FLTP \n"); } else if(AV_SAMPLE_FMT_S16P == av_stream->codecpar->format) { printf("stream format : AV_SAMPLE_FMT_S16P \n"); }else{ printf("stream format : other; value=%d \n",av_stream->codecpar->format); } // 音频信道数目 printf("channels number:%d \n",av_stream->codecpar->channels); //音频压缩编码格式 if(AV_CODEC_ID_AAC == av_stream->codecpar->codec_id) { printf("audio codec:AAC \n"); }else if(AV_CODEC_ID_MP3 == av_stream->codecpar->codec_id) { printf("audio codec:MP3 \n"); }else{ printf("audio codec : other; value=%d \n",av_stream->codecpar->codec_id); } //获取视频总时长 if(AV_NOPTS_VALUE != av_stream->duration) { int audio_time =av_stream->duration * av_q2d(av_stream->time_base); printf("audio_time : d:d:d\n" , (audio_time / 3600), (audio_time % 3600) / 60, (audio_time % 60) ); } else{ printf("audio duration unknown ! \n"); } audio_index = i; } } if(ctx) avformat_close_input(&ctx);}int main() { // 设置控制台输出级别 av_log_set_level(AV_LOG_DEBUG); // // 打印输出字符串Hello World ! av_log(NULL, AV_LOG_DEBUG, "%s", "Hello World !\n"); decode(); return 0;}
看完上述内容,你们对C语言结合ffmpeg如何打印音视频信息有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。
视频
音频
信息
语言
音视
时长
内容
单位
索引
思路
文件
时间
格式
流程
目录
输出
微妙
上下
上下文
信道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全保护策略制定情况
快30了做软件开发
元器件国产替代数据库
计算机网络技术要不要去学
网络安全为人民手抄报六年级
山西hp服务器虚拟化多少钱
电子邮箱无法联接服务器
湛江精益软件开发
焦作匠心网络技术服务有限公司
三精药品广告数据库
鄞州安卓软件开发平台
网络安全应用项目
网络安全设备销售员
关于网络安全的一副画
网络技术产生的原因和特点
ros 日志服务器
铁岭供热站自动化控制软件开发
数据库技术刷题软件
c 创建数据库表名为变量
软件开发不是所有人都能学的
上海软件开发中心
网络安全分为哪些
知识管理数据库
国泰安数据库roa的区别
jsp连接数据库 增删改查
如何查看vue代理服务器的请求
洪山定制软件开发方案
学生校园网络安全教育承诺书
异质性有效汇率数据库
台州制造软件开发计划