Qt使用opencv,进行视频录制,功能打开、关闭摄像头,开始、结束录制视频,暂停、继续录制,并保存视频文件

1.效果图

2 代码实现

2.1 .h文件

cpp 复制代码
#ifndef VIDEORECORDWIDGET_H
#define VIDEORECORDWIDGET_H

#include <QWidget>

#include<QFileDialog>

#include <QImage>
#include <QLabel>
#include <QTimer>
#include <opencv2/opencv.hpp>

using namespace cv;
namespace Ui {
class VideoRecordWidget;
}

class VideoRecordWidget : public QWidget
{
    Q_OBJECT

public:
    explicit VideoRecordWidget(QWidget *parent = nullptr);
    ~VideoRecordWidget();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_4_clicked();

    void updateFrame();

    void on_pushButton_6_clicked();

private:
    Ui::VideoRecordWidget *ui;

    VideoCapture capture;
    // QTimer timer;

    VideoWriter videoWriter;

    bool m_video_open;
    bool m_video_record;

    QTimer timer_open;
    QTimer timer_record;

    String recordViedo_fileName;


};

#endif // VIDEORECORDWIDGET_H

2.2 .cpp文件

cpp 复制代码
#include "videorecordwidget.h"
#include "ui_videorecordwidget.h"

VideoRecordWidget::VideoRecordWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::VideoRecordWidget)
{
    ui->setupUi(this);

    m_video_open=false;
    m_video_record=false;

    // connect(&timer, &QTimer::timeout, this, &VideoRecordWidget::updateFrame);

}

VideoRecordWidget::~VideoRecordWidget()
{
    delete ui;
}

//开启摄像头
void VideoRecordWidget::on_pushButton_clicked()
{
    // 打开摄像头
    capture.open(0);
    if (!capture.isOpened()) {
        qDebug("Failed to open camera.");
        return;
    }

    qDebug()<<"摄像头开启中";
    // 开始定时器,以固定间隔刷新显示视频图像
    timer_open.start(33); // 控制帧率为30fps
    m_video_open=true;
    connect(&timer_open, &QTimer::timeout, this, &VideoRecordWidget::updateFrame);
}

//关闭摄像头
void VideoRecordWidget::on_pushButton_2_clicked()
{
    // 关闭摄像头
    qDebug()<<"摄像头关闭中";
    capture.release();
    ui->label->clear();
    ui->label->setText("视频录制器");
    timer_open.stop();
    m_video_open=false;

    if(m_video_record){
        qDebug()<<"结束录制";
        m_video_record=false;
        timer_record.stop();
        videoWriter.release();
    }


}


//开始录制
void VideoRecordWidget::on_pushButton_3_clicked()
{
    if(m_video_open){
        if(videoWriter.isOpened()){
            qDebug()<<"已经有录制项目:"<<recordViedo_fileName<<"请先结束录制,再操作";
            return;
        }
        // 获取当前时间作为视频文件名
        std::time_t time = std::time(0);
        std::ostringstream oss;
        oss << "video_" << time << ".avi";
        recordViedo_fileName=oss.str();
        // std::string filename = oss.str();

        ui->lineEdit->setText(recordViedo_fileName.c_str());


        qDebug()<<"摄像头开启中-并进行录制,文件名:"<<recordViedo_fileName;
        timer_record.start(1000/25); // 控制帧率为30fps
        m_video_record=true;

        cv::Mat frame;
        capture >> frame; // 从视频流中捕获当前帧

        int codec = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
        double fps = 25.0;
        cv::Size frameSize(frame.cols, frame.rows);
        // if(videoWriter.isOpened()){
        //     videoWriter.write(frame);
        //     return;
        // }
        videoWriter.open(recordViedo_fileName, codec, fps, frameSize);


        connect(&timer_record, &QTimer::timeout, this, &VideoRecordWidget::updateFrame);
    }else{
        qDebug()<<"请先打开摄像头";
    }


}

//暂停录制
void VideoRecordWidget::on_pushButton_5_clicked()
{
    qDebug()<<"暂停录制";
    m_video_record=false;

}

//结束录制
void VideoRecordWidget::on_pushButton_4_clicked()
{
    qDebug()<<"结束录制";
    m_video_record=false;
    timer_record.stop();
    videoWriter.release();

}

void VideoRecordWidget::updateFrame()
{
    if(m_video_open){
        cv::Mat frame;
        capture >> frame; // 从视频流中捕获当前帧

        if (frame.empty()) {
            return;
        }

        // 将OpenCV的Mat图像转换为Qt的QImage
        QImage qimage(frame.data, frame.cols, frame.rows, static_cast<int>(frame.step), QImage::Format_BGR888);
        QPixmap pixmap = QPixmap::fromImage(qimage);

        // 设置QLabel显示图像
        ui->label->setPixmap(pixmap.scaled(ui->label->size(), Qt::KeepAspectRatio));

        if(m_video_record){
            qDebug()<<"录制中";
            // 创建 VideoWriter 对象


            // 检查是否成功打开视频文件
            if (!videoWriter.isOpened())
            {
                qDebug() << "无法打开视频文件.";
                return;
            }
            videoWriter.write(frame);

        }

    }
}
//继续录制
void VideoRecordWidget::on_pushButton_6_clicked()
{
    qDebug()<<"继续录制";
    m_video_record=true;

}
相关推荐
王家视频教程图书馆5 小时前
vue3从本地选择一个视频 展示到视频组件中
前端·javascript·音视频
新科技事物6 小时前
AI编曲软件帮原创音乐人,用清唱歌词的音频快速做出专业的歌曲编曲伴奏
人工智能·音视频
新科技事物6 小时前
AI编曲软件提升出歌效率,原创音乐人凭清唱歌词的音频快速作编曲伴奏成歌
人工智能·音视频
AI科技9 小时前
清唱歌词的音频直接用,原创音乐人用AI编曲软件直接生成完整歌曲的编曲伴奏
人工智能·音视频
wefly20179 小时前
M3U8 播放调试天花板!m3u8live.cn纯网页无广告,音视频开发效率直接拉满
java·前端·javascript·python·音视频
凉拌菜11 小时前
医用录像机在手术影像系统中的作用:视频采集与存储架构
音视频
WJSKad123511 小时前
[特殊字符] Mimi音频神经网络编解码器:高保真声音处理的突破
人工智能·神经网络·音视频
凉拌菜12 小时前
手术摄像系统的视频延迟是如何产生的?从采集到网络传输的技术解析
网络·音视频·医疗视频·4k视频·术野摄像机
ai产品老杨12 小时前
源码交付与异构算力破局:基于GB28181/RTSP的Docker化AI视频平台架构实战
人工智能·docker·音视频
Fleshy数模1 天前
玩转OpenCV:视频椒盐噪声处理与图像形态学操作实战
人工智能·opencv·音视频