1、Qt开发环境搭建
搭建Qt环境使用一种比较简单的方式,使用Qt online 版本,使用Qt Maintenance Tools进行后期的维护升级增删模块也很方便
2、FFmpeg环境搭建
首先下载ffmpeg-win32-dev ffmpeg-win32-shared ffmpeg-win32-static三个文件
可以前往本人自愿下载寻找文件。
解压后建立一个文件夹ffmpeg将dev下的include与lib文件夹与shared下的bin文件夹拷贝进去,然后将ffmpeg放在相应的目录下,这里我放在了自己的C:盘下,将bin添加到系统path中此处不添加详细步骤。
新建qt工程在.pro文件中添加下面代码
win32 {
# ffmpeg 3.0
FFMPEG_PATH = C:/ffmpeg
INCLUDEPATH += $$FFMPEG_PATH/include
LIBS += $$FFMPEG_PATH/lib/avcodec.lib \
$$FFMPEG_PATH/lib/avdevice.lib \
$$FFMPEG_PATH/lib/avfilter.lib \
$$FFMPEG_PATH/lib/avformat.lib \
$$FFMPEG_PATH/lib/avutil.lib \
$$FFMPEG_PATH/lib/postproc.lib \
$$FFMPEG_PATH/lib/swresample.lib \
$$FFMPEG_PATH/lib/swscale.lib
}
在要使用的源文件中添加下面代码
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavformat/version.h>
#include <libavutil/time.h>
#include <libavutil/mathematics.h>
}
到此为止环境已经搭建好了
3、创建代码测试
qDebug() << "[FFmpeg Log] Hello FFmpeg ,version info is"<<av_version_info();
qDebug() << "[FFmpeg Log] avutil_configuration is"<<avutil_configuration();
qDebug() <<"[FFmpeg Log] version is"<<avcodec_version();
添加上面代码运行即可在应用程序输出中看到相应的调试信息了
[FFmpeg Log] Hello FFmpeg ,version info is git-2019-11-23-d65aaf8
[FFmpeg Log] avutil_configuration is --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
[FFmpeg Log] version is 3817060
4、添加视频编辑相关代码
QString path_string = QCoreApplication::applicationDirPath()+"/11.mp4";
QByteArray video_file_path;
video_file_path = QByteArray::fromStdString(path_string.toStdString());
//av_register_all(); //老版本首先注册ffmpeg库,新版本可以去掉
AVFormatContext *afc = nullptr;
//打开视频文件
int nRet = avformat_open_input(&afc, video_file_path, nullptr, nullptr);
if (nRet < 0){
qDebug() << "找不到视频文件" << endl;
return;
}else{
qDebug() << "视频打开成功" << endl;
}
int64_t durTime = afc->duration / AV_TIME_BASE; //视频时间
unsigned int numberOfStream = afc->nb_streams; //包含流的个数2:一个视频流一个音频流
qDebug() <<QString("视频播放时长: %1s;视频流为: %2").arg(durTime).arg(numberOfStream)<<endl;
for (uint i = 0; i < afc->nb_streams; i++)
{
AVCodecContext *acc = afc->streams[i]->codec;
//AVCodecParameters *par = afc->streams[i]->codecpar;
//avcodec_parameters_to_context(acc,par);
if (acc->codec_type == AVMEDIA_TYPE_VIDEO) //如果是视频类型
{
qDebug() <<"视频"<<endl;
AVCodec *codec = avcodec_find_decoder(acc->codec_id);
if (!codec)
{
qDebug() << "没有该类型解码器" << endl;
}
int ret = avcodec_open2(acc, codec, nullptr);
if (ret != 0)
{
char buf[1024] = { 0 };
av_strerror(ret, buf, sizeof(buf));
}
qDebug() << "解码器打开成功" << endl;
}else if(acc->codec_type == AVMEDIA_TYPE_AUDIO){
qDebug() <<"音频"<<endl;
}
}
if (afc)
{
avformat_close_input(&afc); //关闭视频流
}
需要说明的有几个点
a、路径问题想要使用char path[] = "视频.mp4"的路径需要将视频文件放在debug文件的前一个目录。
b、将QString转换成char *类型,看源代码使用了QByteArray。
4关于编译警告
4.1 函数或者结构体变量 is deprecated警告的说明
先来介绍点相关的指示
(1)__GNUC__ 、__GNUC_MINOR__ 、__GNUC_PATCHLEVEL__分别代表gcc的主版本号,次版本号,修正版本号,
(2)关于__attribute__((deprecated)) 详细介绍可以查看博文 https://blog.csdn.net/weaiken/article/details/88085360
#if AV_GCC_VERSION_AT_LEAST(3,1) //判断GUNC版本
# define attribute_deprecated __attribute__((deprecated))
#elif defined(_MSC_VER)
# define attribute_deprecated __declspec(deprecated)
#else
# define attribute_deprecated
#endif
4.2