QT 6 使用相机录制视频
使用Qt 6框架的MultimediaWidgets
模块QMediaRecorder
类进行视频录制。
-
创建一个基于
QMainWindow
应用程序
-
首先,确保你的Qt项目中包含了多媒体模块。这通常在项目的.pro文件中进行配置,通过添加
QT += multimedia multimediawidgets
来启用多媒体模块。 -
添加成员变量和函数方法。为
VideoRecorder
类添加必要的成员变量和方法来处理视频录制,添加视频录制开始,暂停,停止等功能。 -
视频录制:使用
VideoRecorder
类来录制视频。配置好录制的参数后,使用VideoRecorder::record()
方法可以开始录制。录制完成后,可以通过VideoRecorder::stop()
方法来停止录制。 -
处理事件:为你的主窗口添加事件处理函数,如开始录制和停止录制的按钮点击事件,以及处理录制的开始和结束等。
首先,确保你的项目文件.pro
包含多媒体模块:
QT += core gui multimedia multimediawidgets
然后,为VideoRecorder
类添加成员变量和函数:
- C++头文件
"videorecorder.h"
内容如下:
#ifndef VIDEORECORDER_H
#define VIDEORECORDER_H
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QComboBox>
#include <QVideoWidget>
#include <QCamera>
#include <QMediaRecorder>
#include <QAudioInput>
#include <QMediaCaptureSession>
#include <QMediaDevices>
#include <QCameraDevice>
class VideoRecorder : public QMainWindow
{
Q_OBJECT
public:
VideoRecorder(QWidget *parent = nullptr);
~VideoRecorder();
private slots:
void onDurationChanged(qint64 duration);
void selectCameraDevice(int index);
void on_btn_start_clicked();
void on_btn_pause_clicked();
void on_btn_stop_clicked();
void on_btn_close_clicked();
private:
void createLayout();
QPushButton *btnStart;
QPushButton *btnPause;
QPushButton *btnStop;
QPushButton *btnClose;
QComboBox *comboBoxInput;
QLabel *label1;
QLabel *labelTime;
QVideoWidget *videoWidget;
QCamera *camera;
QMediaRecorder *recorder;
QAudioInput *audioInput;
QMediaCaptureSession *mediaCaptureSession;
QMediaDevices *mediaDevices;
QList<QCameraDevice> cameraDevices;
};
#endif // VIDEORECORDER_H
- C++源文件 widget.cpp 内容如下:
#include "videorecorder.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <QUrl>
#include <QCoreApplication>
#include <QVideoSink>
VideoRecorder::VideoRecorder(QWidget *parent)
: QMainWindow(parent)
{
createLayout();
camera = new QCamera(QMediaDevices::defaultVideoInput());
audioInput = new QAudioInput(this);
recorder = new QMediaRecorder(this);
mediaCaptureSession = new QMediaCaptureSession(this);
mediaCaptureSession->setAudioInput(audioInput);
mediaCaptureSession->setRecorder(recorder);
mediaCaptureSession->setVideoOutput(videoWidget);
recorder->setQuality(QMediaRecorder::HighQuality);
recorder->setOutputLocation(QUrl::fromLocalFile(
QCoreApplication::applicationDirPath() + "/demo.mp4"));
recorder->setEncodingMode(QMediaRecorder::ConstantQualityEncoding);
recorder->setAudioChannelCount(recorder->audioChannelCount());
connect(recorder,&QMediaRecorder::durationChanged,
this,&VideoRecorder::onDurationChanged);
cameraDevices = QMediaDevices::videoInputs();
comboBoxInput->addItem("<Node>");
foreach (const QCameraDevice & cam, cameraDevices) {
comboBoxInput->addItem(cam.description());
}
connect(comboBoxInput,&QComboBox::currentIndexChanged,
this,&VideoRecorder::selectCameraDevice);
}
void VideoRecorder::createLayout()
{
label1 = new QLabel(tr("Recorded Time(s):"));
labelTime = new QLabel;
labelTime->setFrameStyle(QFrame::Box);
btnClose = new QPushButton(tr("关闭"));
btnStart = new QPushButton(tr("开始"));
btnStop = new QPushButton(tr("停止"));
btnPause = new QPushButton(tr("暂停"));
comboBoxInput = new QComboBox;
videoWidget = new QVideoWidget;
connect(btnStart,&QPushButton::clicked,this,&VideoRecorder::on_btn_start_clicked);
connect(btnPause,&QPushButton::clicked,this,&VideoRecorder::on_btn_pause_clicked);
connect(btnStop,&QPushButton::clicked,this,&VideoRecorder::on_btn_stop_clicked);
connect(btnClose,&QPushButton::clicked,this,&VideoRecorder::on_btn_close_clicked);
QVBoxLayout *vTopRight = new QVBoxLayout;
vTopRight->addWidget(comboBoxInput);
QGridLayout *gLayoutMain = new QGridLayout;
gLayoutMain->addWidget(videoWidget,0,0,5,3);
gLayoutMain->addLayout(vTopRight,0,3);
gLayoutMain->addWidget(btnStart,1,3);
gLayoutMain->addWidget(btnPause,2,3);
gLayoutMain->addWidget(btnStop,3,3);
gLayoutMain->addWidget(label1,5,0);
gLayoutMain->addWidget(labelTime,5,1,1,2);
gLayoutMain->addWidget(btnClose,5,3);
resize(480,320);
setMinimumSize(480,320);
setMaximumSize(480,320);
setWindowTitle(tr("视频录像机"));
QWidget *wi = new QWidget;
wi->setLayout(gLayoutMain);
setCentralWidget(wi);
}
void VideoRecorder::onDurationChanged(qint64 duration){
labelTime -> setText(QString("%1 s").arg(QString::number(duration / 1000)));
}
void VideoRecorder::selectCameraDevice(int )
{
//遍历当前选择的设备是否等于可利用的设备,然后进行设备的配置
for(const QCameraDevice &cam : cameraDevices){
if(cam.description() == comboBoxInput->currentText()){
camera -> setCameraDevice(cam);
mediaCaptureSession -> setCamera(camera);
camera -> start();
break;
}
}
}
void VideoRecorder::on_btn_start_clicked(){
recorder -> record();
}
void VideoRecorder::on_btn_pause_clicked(){
recorder -> pause();
}
void VideoRecorder::on_btn_stop_clicked(){
recorder -> stop();
labelTime -> setText("0");
}
void VideoRecorder::on_btn_close_clicked(){
close();
}
VideoRecorder::~VideoRecorder() {}
- 主程序main.cpp 内容如下
#include "videorecorder.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoRecorder w;
w.show();
return a.exec();
}
小结
这段代码演示了如何使用QT 6的QMediaRecorder
类进行视频录制。创建一个QMediaRecorder
对象并设置了音视频输入设备、输出位置和输出格式。然后,通过调用record()方法开始录制,通过调用stop()方法停止录制。本例程在Windows 10环境QT 6.7框架上成功运行。