一、 环境搭建
1. 下载
QT:window 5.7.0版本
FFmpeg: ffmpeg-20200522-38490cb-win32-dev
注意:这里下载 32位dev版本,要和编译器对应(我的mingw是32位的)
2. 加载库
在FFmpeg的解压目录里
- 将 include/ 的头文件拷贝到自己的项目的工程路径下
- 从 lib/ 拷贝所需要的库到自己的项目里
#这里是我所用到的库和 pro里的配置
//avcodec-58.dll
//avdevice-58.dll
//avfilter-7.dll
//avformat-58.dll
//avutil-56.dll
//potproc-55.dll
//swresample-3.dll
//swscale-5.dll
INCLUDEPATH += $$PWD/include/
LIBS += -L$$PWD/lib -lavutil-56 -lavformat-58 -lavcodec-58 -lavdevice-58 -lavfilter-7 -lpostproc-55 -lswresample-3 -lswscale-5
#到这里配置就完成了
二、实战演练
1. 功能介绍
源码链接: https://github.com/AutoCatFuuuu/QT/tree/master/ffmpeg
- 音视频播放
- 调节音量,跳转进度
- 支持弹幕功能
实际效果图
2. 音视频操作流程图
AFFmepg.h
[AFFmpeg类] 音视频操作功能封装
操作流程:
- open();
打开音视频文件
读取音视频流
读取音视频的解码器
初始化音视频的缓存区
获取音视频的时长,时基- readVideo(); readAudio();
解码音视频流- getFrame(); getAudio();
读取解码后的音视频流数据- seek();
跳转音视频流- getDeviation();
计算当前音视频帧的时间差
===========================================
在open()成功之后开始两个定时器分别执行
<1> readVideo() getFrame() [视频]
<2> readAudio() getAudio() [音频]
即可。
#ifndef AFFMPEG_H
#define AFFMPEG_H
#ifdef __cplusplus
extern "C"{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "libswresample/swresample.h"
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <pthread.h>
}
#endif
#include "afilter.h"
class AFFmpeg
{
public:
AFFmpeg();
~AFFmpeg();
bool open(const char* filepath,bool isaudio = true);//打开文件
int readVideo(); //读取视频
int readAudio(); //读取音频
int seek(float pos); //跳转
AVFrame* getFrame(){
return pVideoFrameRGB;} //读取图像
int getAudio(char **buf); //读取音频
int getDuration(){
return duration;} //获取总时长
double getCDuration(){
return cduration;} //获取当前时长
int getFPS() {
return fps.num;} //获取帧率
double getDeviation(); //获取当前视频和音频的时差
void setAFilterOpen() {
isAFilteropen = !isAFilteropen; } //弹幕开关
private:
//视频相关
AVFormatContext *pFormatContext; //文件内容相关信息
AVCodecContext *pVideoCodecContext;//编码信息
AVCodec *pVideoCodec; //解码器
AVFrame *pVideoFrame; //一帧视频 源
AVFrame *pVideoFrameRGB; //一帧视频 RGB格式
AVRational fps; //帧率
struct SwsContext *pSwsContext; //转换格式用的结构体
unsigned char *out_buffer; //数据流初始化用的
int videoindex; //视频流索引
AVPacket *pVCPacket