当前位置: 首页 > news >正文

从H264视频中获取宽、高、帧率、比特率等属性信息

背景

最近整理视频编解码的代码,早前在jetson上封装了jetson multimedia作为视频编解码的类,供其他同事和其他组使用,但该解码接口有一个问题,无法首先获取视频宽高信息,更无法直接获取视频的帧率、比特率等信息。

解决方法

  1. 使用ffmpeg库,命令行参数不适合代码集成
  2. 使用ffmpeg的API接口进行封装

源码实现

// ffmpeg_videoinfo.h#ifndef FFMPEG_VIDEOINFO_H
#define FFMPEG_VIDEOINFO_H#include <iostream>
#include <memory>struct VideoAsset {float width; float height;float fps;float bitrate; /** bit per second */float duration; /** seconds */
};#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */int GetVideoInfo(const char *in_file,  std::shared_ptr<VideoAsset> &info);#ifdef __cplusplus
};
#endif#endif  // FFMPEG_VIDEOINFO_H
// ffmpeg_videoinfo.cpp#include "ffmpeg_videoinfo.h"#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>int GetVideoInfo(const char *in_file,  std::shared_ptr<VideoAsset> &info){// 注册所有格式和编解码器av_register_all();// 创建一个格式上下文(Format Context)AVFormatContext* formatContext = nullptr;if (avformat_open_input(&formatContext, in_file, nullptr, nullptr) != 0) {fprintf(stderr, "Could not open input file '%s'", in_file);return -1;}// 获取流信息if (avformat_find_stream_info(formatContext, nullptr) < 0) {fprintf(stderr, "Could not find stream information '%s'", in_file);return -1;}// 查找视频流int videoStreamIndex = -1;for (unsigned i = 0; i < formatContext->nb_streams; i++) {if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {videoStreamIndex = i;break;}}if (videoStreamIndex == -1) {fprintf(stderr, "Could not find video stream '%s'", in_file);return -1;}// 获取视频流的编码参数AVCodecParameters* codecParameters = formatContext->streams[videoStreamIndex]->codecpar;AVStream* videoStream = formatContext->streams[videoStreamIndex];info->fps = av_q2d(videoStream->avg_frame_rate);info->width = codecParameters->width;info->height = codecParameters->height;int64_t totalSize = 0;int64_t totalDuration = 0;AVPacket packet;while (av_read_frame(formatContext, &packet) >= 0) {if (packet.stream_index == 0) {totalSize += packet.size;totalDuration += packet.duration;}av_packet_unref(&packet);}AVRational timeBase = videoStream->time_base; /** 时间基 */info->duration = (float)totalDuration * av_q2d(timeBase);info->bitrate = (totalDuration > 0) ? (totalSize * 8.0 / info->duration) : 0.0;// 清理avformat_close_input(&formatContext);return 0;
}
#ifdef __cplusplus
};
#endif
// 测试脚本 test_single_videoinfo.cpp#include "ffmpeg_videoinfo.h"int main(int argc, char **argv) {if(argc<2){return 1;}const std::string &input_h264=argv[1];std::shared_ptr<VideoAsset> video_info_ptr = std::make_shared<VideoAsset>();// VideoAsset video_info;int status_code = GetVideoInfo(input_h264.c_str(), video_info_ptr);if (status_code < 0) {printf("GetVideoInfo failed\n");return -1;}printf("bitrate:%f, duration:%f, fps:%f, height:%f, width:%f\n",video_info_ptr->bitrate, video_info_ptr->duration, video_info_ptr->fps, video_info_ptr->height, video_info_ptr->width);return 1;
}
# CMakelist.txt核心add_executable(test_video_info test_single_videoinfo.cpp ffmpeg_videoinfo.h ffmpeg_videoinfo.cpp)
target_compile_features(test_video_info PRIVATE cxx_std_14)
target_link_libraries(test_video_info avcodec avutil avformat)
# 测试./test_video_info /data/videos/l4t.h264# 输出信息示例
[h264 @ 0xaaaac14b16a0] Stream #0: not enough frames to estimate rate; consider increasing probesize
bitrate:30681866.000000, duration:164.490005, fps:20.000000, height:2160.000000, width:3840.000000

后记

本人对ffmpeg接口并不熟悉,以上根据文档及搜索结果进行的实现,不敢保证没有bug,如果各位遇到问题,可以留言交流

http://www.lryc.cn/news/484792.html

相关文章:

  • Cyberchef配合Wireshark提取并解析TCP/FTP流量数据包中的文件
  • Nginx中使用keepalive实现保持上游长连接实现提高吞吐量示例与测试
  • 深度学习-卷积神经网络CNN
  • 241114.学习日志——[CSDIY] [Cpp]零基础速成 [03]
  • 大模型研究报告 | 2024年中国金融大模型产业发展洞察报告|附34页PDF文件下载
  • 数据库SQL——什么是实体-联系模型(E-R模型)?
  • 在 MySQL 8.0 中,SSL 解密失败,在使用 SSL 加密连接时出现了问题
  • React Native 全栈开发实战班 - 第四部分:用户界面进阶之动画效果实现
  • 【CICD】GitLab Runner 和执行器(Executor
  • 实用教程:如何无损修改MP4视频时长
  • mysqldump命令搭配source命令完成数据库迁移备份
  • 生信:TCGA学习(R、RStudio安装与下载、常用语法与常用快捷键)
  • 十三、注解配置SpringMVC
  • 为什么海外服务器IP会被封
  • 图像处理技术椒盐噪声
  • [笔记]L6599的极限工作条件考量
  • 机器学习基础04
  • Ubuntu 20.04 配置开发环境(持续更新)
  • Rocky9/Ubuntu使用pip安装python的库mysqlclient失败解决方式
  • 探索 HTML 和 CSS 实现的 3D旋转相册
  • OpenJudge_ 简单英文题_04:0/1 Knapsack
  • 深入探索离散 Hopfield 神经网络
  • [智能车摄像头是一种安装在汽车上用于辅助驾驶和提高安全性的重要设备]
  • 前端vue 列表中回显并下拉选择修改标签
  • hbase未来的发展趋势
  • Rust 语言学习笔记(二)
  • 【postman】怎么通过curl看请求报什么错
  • Python 编程入门指南(一)
  • macOS 设置固定IP
  • redis实现消息队列的几种方式