hdmi视频数据录像保存的qt源代码
时间: 2023-03-20 13:02:59 浏览: 325
以下是保存HDMI视频数据录像的Qt源代码示例:
```C++
#include <QCoreApplication>
#include <QThread>
#include <QDateTime>
#include <QFile>
#include <QDataStream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 初始化ffmpeg库
av_register_all();
avdevice_register_all();
AVInputFormat *inputFormat = av_find_input_format("dshow");
AVDictionary *options = NULL;
av_dict_set(&options, "video_size", "1920x1080", 0);
av_dict_set(&options, "pixel_format", "uyvy422", 0);
av_dict_set(&options, "framerate", "30", 0);
av_dict_set(&options, "buffer_size", "1048576", 0);
AVFormatContext *formatContext = NULL;
int ret = avformat_open_input(&formatContext, "video=HD Webcam:audio=麦克风 (HD Webcam)", inputFormat, &options);
if (ret != 0) {
qDebug() << "avformat_open_input error" << av_err2str(ret);
return 1;
}
// 查找视频流
int videoStreamIndex = -1;
for (int i = 0; i < formatContext->nb_streams; i++) {
if (formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStreamIndex = i;
break;
}
}
if (videoStreamIndex == -1) {
qDebug() << "can't find video stream";
return 1;
}
// 打开视频解码器
AVCodecParameters *videoCodecpar = formatContext->streams[videoStreamIndex]->codecpar;
AVCodec *videoCodec = avcodec_find_decoder(videoCodecpar->codec_id);
AVCodecContext *videoCodecContext = avcodec_alloc_context3(videoCodec);
ret = avcodec_parameters_to_context(videoCodecContext, videoCodecpar);
if (ret != 0) {
qDebug() << "avcodec_parameters_to_context error" << av_err2str(ret);
return 1;
}
ret = avcodec_open2(videoCodecContext, videoCodec, NULL);
if (ret != 0) {
qDebug() << "avcodec_open2 error" << av_err2str(ret);
return 1;
}
// 准备视频帧
AVFrame *videoFrame = av_frame_alloc();
if (videoFrame == NULL) {
qDebug() << "av_frame_alloc error";
return 1;
}
// 打开输出文件
QDateTime currentDateTime = QDateTime::currentDateTime();
QString fileName = currentDateTime.toString("yyyyMMdd_hhmmss") + ".avi";
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "open file error" << fileName;
return 1;
}
// 写入文件头
AVOutputFormat *outputFormat = av_guess_format("avi", NULL, NULL);
AVFormatContext *outFormatContext = NULL;
ret = avformat_alloc_output_context2(&outFormatContext, outputFormat, NULL, fileName.toUtf8().data());
if (ret != 0) {
qDebug() << "avformat_alloc_output_context2 error"
阅读全文