ffmpeg提取h264视频帧率的源码

时间: 2023-09-07 14:14:22 浏览: 47
以下是使用FFmpeg提取H.264视频帧率的样例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/mathematics.h> int main(int argc, char* argv[]) { AVFormatContext *pFormatCtx = NULL; int i, videoStream; AVCodecContext *pCodecCtxOrig = NULL, *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet; int frameFinished; float fps = 0; if(argc < 2) { printf("Usage: %s <video_file>\n", argv[0]); return -1; } av_register_all(); if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0) { printf("Error: Couldn't open input file!\n"); return -1; } if(avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("Error: Couldn't find stream information!\n"); return -1; } av_dump_format(pFormatCtx, 0, argv[1], 0); videoStream = -1; for(i = 0; i < pFormatCtx->nb_streams; i++) { if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if(videoStream == -1) { printf("Error: Couldn't find a video stream!\n"); return -1; } pCodecCtxOrig = pFormatCtx->streams[videoStream]->codec; pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id); if(pCodec == NULL) { printf("Error: Unsupported codec!\n"); return -1; } pCodecCtx = avcodec_alloc_context3(pCodec); if(avcodec_copy_context(pCodecCtx, pCodecCtxOrig) != 0) { printf("Error: Couldn't copy codec context!\n"); return -1; } if(avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Error: Couldn't open codec!\n"); return -1; } pFrame = av_frame_alloc(); while(av_read_frame(pFormatCtx, &packet) >= 0) { if(packet.stream_index == videoStream) { avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); if(frameFinished) { fps = av_q2d(pFormatCtx->streams[videoStream]->r_frame_rate); break; } } av_free_packet(&packet); } av_free(pFrame); avcodec_close(pCodecCtx); avcodec_close(pCodecCtxOrig); avformat_close_input(&pFormatCtx); printf("FPS: %.2f\n", fps); return 0; } ``` 运行样例代码时,需要将 H.264 视频文件名作为命令行参数传递给程序。程序将会输出视频的帧率。

相关推荐

最新推荐

recommend-type

ffmpeg H264解码decode_mb_cavlc函数参数解释

ffmpeg H264解码slice的decode_mb_cavlc函数参数解释
recommend-type

Linux服务器安装ffmpeg+libx264+libmp3lame

ffmpeg是一个很强大的音视频处理工具,官网是:http://ffmpeg.org/ 官网介绍ffmpeg是:一个完整的、跨平台的解决方案,可以记录、转换和传输音频和视频。...Linux服务器安装ffmpeg+libx264+libmp3lame
recommend-type

使用Java和ffmpeg把音频和视频合成视频的操作方法

主要介绍了使用Java和ffmpeg把音频和视频合成视频,本文通过实例代码给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

python调用系统ffmpeg实现视频截图、http发送

主要为大家详细介绍了python调用系统ffmpeg实现视频截图、http发送,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java使用FFmpeg处理视频文件的方法教程

主要给大家介绍了关于Java使用FFmpeg处理视频文件的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB遗传算法自动优化指南:解放算法调优,提升效率

![MATLAB遗传算法自动优化指南:解放算法调优,提升效率](https://help-static-aliyun-doc.aliyuncs.com/assets/img/zh-CN/8487939061/p208348.png) # 1. MATLAB遗传算法概述** 遗传算法是一种受生物进化启发的优化算法,它模拟了自然选择和遗传的过程。在MATLAB中,遗传算法工具箱提供了丰富的函数和类,用于创建和运行遗传算法。 **1.1 遗传算法的基本原理** 遗传算法的工作原理如下: - **初始化:**创建由随机个体组成的初始种群。 - **评估:**根据目标函数计算每个个体的适应度。 -
recommend-type

failed to register layer: ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header

很抱歉听到这个问题。这个错误通常是由于Docker镜像损坏或Docker存储库中缺少所需的图层引起的。您可以尝试以下几种方法来解决这个问题: 1. 清理Docker镜像和容器:使用命令“docker system prune”清理不再使用的Docker对象。 2. 检查Docker存储库:确保您正在使用可靠的Docker存储库,并检查是否存在所有必需的图层。 3. 重新下载Docker镜像:如果您确定问题是由于损坏的Docker镜像引起的,则可以尝试重新下载Docker镜像。 4. 更新Docker版本:如果您使用的是旧版Docker,则可能会出现此问题。尝试更新到最新版本的Docke
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。