1、前言
从这本文开始,我将逐步深入FFmpeg API的使用方法,而不仅仅使用ffmpeg命令行这种虽然简便强大但是有一定局限性的方法。使用命令行我们可以轻松完成自己的一部分特定工作,但是如果我们要使用FFmpeg将处理的结果返回给其他用户,或者我们的项目中需要嵌入FFmpeg,那么我们就必须要学会使用FFmpeg提供的API写代码完成工作。
回到本文主要内容。首先,我们对视频进行分析之前经常需要知道媒体文件所包含的视频流信息,比如文件格式、播放时长、视频码率、视频帧率、视频编解码格式、音频码率、音频采样率等等信息。如果使用命令行,那么只需要ffmpeg -i input.mp4
即可获取以上信息。但是,如何使用FFmpeg 提供的API来获取这些信息,这就是本文要讲述的内容。
首先,放一张运行结果图。从图中可以看出已经获取了以上提到的信息。
2、代码环境
我使用windows10 + qt creator
的开发环境,编译环境为MSVC2017
。
需要说明的是,在使用FFmpeg API写代码之前先将要用到的FFmpeg添加到qt项目的工程中。B站有一个视频讲解怎么配置环境,讲解的还算清楚,可以作为参考。如果有遇到任何问题,欢迎评论区留言,我会及时回复。
B站 qt creator+windows+ffmpeg配置地址:https://www.bilibili.com/video/BV1zN411d7st?p=4
3、代码
我将上述提到的工作封装成了一个类,如果你环境搭建好了,那么可以直接将代码复制,然后运行。先跑起来看到效果,然后再仔细研究代码。
- 类头文件代码(videoinformation.h)
// Copyright (c) 2021 LucasNan <[email protected]> <www.kevinnan.org.cn>
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef VIDEOINFORMATION_H
#define VIDEOINFORMATION_H
#include <iostream>
#include <string>
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}
/* 视频有哪些信息?
* (1) 总体
* - 格式 o
* - 编解码器
* - 文件大小
* - 时长 o
* - 总体码率模式
* - 总体码率
* - 日期
* (2) 对于视频
* - ID o
* - 格式 o
* - 码率 o
* - 最大码率
* - 宽度 o
* - 高度 o
* - 帧率模式
* - 帧率 o
* - 色彩空间
* - 色度抽样
* - 位深
* - 流大小 o
* (3) 对于音频
* - 格式 o
* - 码率 o
* - 声道数 o
* - 采样率 o
* - 压缩模式
* - 流大小 o
*/
class VideoInformation
{
public:
VideoInformation();
~VideoInformation(){
avformat_close_input(&input_AVFormat_context_);
}
//@brief: 得到视频信息
//@param: file_path: 文件路径
//@ret : void
//@birth: created by LucasNan on 20210218
void getVideoInfo(std::string file_path);
//@brief: 得到格式
//@param: void
//@ret : 视频格式
//@birth: created by LucasNan on 20210219
std::string getFormat(){
return this->format_;
}
//@brief: 得到视频长度
//@param: void
//@ret : 视频长度
//@birth: created by LucasNan on 20210219
std::string getDuration(){
return this->duration_;
}
//@brief: 得到视频帧率
//@param: void
//@ret : 视频帧率
//@birth: created by LucasNan on 20210219
int getFrameRate(){
return this->frame_rate_;
}
//@brief: 得到视频码率
//@param: void
//@ret : 视频平均码率
//@birth: created by LucasNan on 20210219
int getVideoAverageBitRate(){
return this->video_average_bit_rate_;
}
//@brief: 得到视频宽度
//@param: void
//@ret : 视频宽度
//@birth: created by LucasNan on 20210219
int getWidth(){
return this->width_;
}
//@brief: 得到视频高度
//@param: void
//@ret : 视频高度
//@birth: created by LucasNan on 20210219
int getHeight()