win32 {
contains(QT_ARCH, x86_64) {
message("win64")
FFMPEG_HOME=$${PWD}/three/ffmpeg-6.1.1-win64
INCLUDEPATH += $${FFMPEG_HOME}/include
DEPENDPATH += $${FFMPEG_HOME}/include
LIBS += -L$${FFMPEG_HOME}/lib \
-lavcodec \
-lavdevice \
-lavfilter \
-lavformat \
-lavutil \
-lpostproc \
-lswresample \
-lswscale
} else {
message("win32")
FFMPEG_HOME=$${PWD}/three/ffmpeg-4.0.2-win32
INCLUDEPATH += $${FFMPEG_HOME}/include
DEPENDPATH += $${FFMPEG_HOME}/include
LIBS += -L$${FFMPEG_HOME}/lib \
-lavcodec \
-lavdevice \
-lavfilter \
-lavformat \
-lavutil \
-lpostproc \
-lswresample \
-lswscale
OPENCV_HOME=$${PWD}/three/opencv_4.1.0-win32
INCLUDEPATH += $${OPENCV_HOME}/include
LIBS += -L$${OPENCV_HOME}/x86/mingw/bin \
-lopencv_core410 \
-lopencv_videoio410 \
-lopencv_highgui410 \
-lopencv_imgproc410
}
}
// 初始化FFmpeg
av_register_all();
AVFormatContext* formatContext = nullptr;
// 打开视频文件,路径为绝对路径
const char* filePath = "D:/Qt/Code/ENDOSCOPE/a111.mp4";
if (avformat_open_input(&formatContext, filePath, nullptr, nullptr) != 0) {
qDebug()<<"打开视频文件失败";
return;
}
// 获取视频流信息
if (avformat_find_stream_info(formatContext, nullptr) < 0) {
avformat_close_input(&formatContext);
qDebug()<<"获取流信息失败";
return;
}
// 寻找视频流索引
int videoStreamIndex = -1;
videoStreamIndex = av_find_best_stream(formatContext, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
if (videoStreamIndex == -1) {
avformat_close_input(&formatContext);
qDebug()<<"没有找到视频流";
return;
}
qDebug()<<"视频宽度"<<formatContext->streams[videoStreamIndex]->codecpar->width;
qDebug()<<"视频高度"<<formatContext->streams[videoStreamIndex]->codecpar->height;
qDebug()<<"视频码率"<<formatContext->streams[videoStreamIndex]->codecpar->bit_rate;
// 获取视频解码器参数
AVCodecParameters* codecParameters = formatContext->streams[videoStreamIndex]->codecpar;
// 查找并打开视频解码器
AVCodec* codec = avcodec_find_decoder(codecParameters->codec_id);
if (!codec) {
avformat_close_input(&formatContext);
qDebug()<<"找不到解码器";
return;
}
AVCodecContext* codecContext = avcodec_alloc_context3(codec);
if (!codecContext) {
avformat_close_input(&formatContext);
qDebug()<<"分配解码器上下文失败";
return;
}
if (avcodec_parameters_to_context(codecContext, codecParameters) < 0) {
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
qDebug()<<"设置解码器上下文参数失败";
return;
}
// 打开视频解码器
if (avcodec_open2(codecContext, codec, nullptr) < 0) {
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);
qDebug()<<"打开解码器失败";
return;
}
AVPacket packet;
av_init_packet(&packet);
AVFrame* frame = av_frame_alloc();
int frameNumber = 0;
// 循环读取视频帧数据并进行截取
while (av_read_frame(formatContext, &packet) >= 0) {
if (packet.stream_index == videoStreamIndex) {
// 解码视频帧数据
int ret = avcodec_send_packet(codecContext, &packet);
if (ret < 0 && ret != AVERROR(EAGAIN)) {
break;
}
ret = avcodec_receive_frame(codecContext, frame);
if (ret == 0) {
// 初始化SWS上下文
struct SwsContext *sws_ctx = sws_getContext(codecContext->width, codecContext->height,
codecContext->pix_fmt, codecContext->width, codecContext->height,
AV_PIX_FMT_RGB32, SWS_BICUBIC, nullptr, nullptr, nullptr);
// 分配图像缓冲区
uint8_t *buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_RGB32, codecContext->width, codecContext->height));
AVPicture pict;
avpicture_fill((AVPicture *)&pict, buffer, AV_PIX_FMT_RGB32, codecContext->width, codecContext->height);
sws_scale(sws_ctx, frame->data, frame->linesize, 0, frame->height,
pict.data, pict.linesize);
QImage img = QImage((uchar*)buffer, codecContext->width, codecContext->height,
QImage::Format_ARGB32);
qDebug()<<"____img______"<<img.size();
img.save("test.png");
break;
}
}
av_packet_unref(&packet);
}
// 清理资源
av_frame_free(&frame);
avcodec_free_context(&codecContext);
avformat_close_input(&formatContext);