Qt6+QML實現(xiàn)Windows屏幕錄制功能
前言
Qt6提供了更豐富的多媒體支持類,使用Qt6 QMediaCaptureSession、QScreenCapture、QMediaRecorder,來實現(xiàn)一個屏幕錄制的demo,其中QScreenCapture 最低版本 Qt6.5。支持錄制的清晰度設(shè)置,選擇視頻保存位置,UI使用QML來實現(xiàn)。
Qt6還有一個比較好用的類 QWindowCapture, 可以針對窗口錄屏。使用靜態(tài)函數(shù) QList<QCapturableWindow> capturableWindows()
可以獲取當前可用的錄制窗口,選擇窗口進行錄制??梢栽诒綿emo的基礎(chǔ)上進行擴展。
效果圖
本demo使用Qt6.8 MinGW進行編譯,注意QScreenCapture最低支持Qt6.5,所以版本不能低于6.5.


完整代碼
主要使用Qt6 QMediaCaptureSession、QScreenCapture、QMediaRecorder這三個關(guān)鍵的多媒體類來實現(xiàn)。
關(guān)鍵代碼:
開始錄制和結(jié)束錄制:
void ScreenRecorder::startRecording()
{
if (m_isRecording) {
qDebug() << __FUNCTION__ << "Already recording, ignoring request";
return;
}
qDebug() << __FUNCTION__ << "Starting recording process...";
// 選擇保存文件
QString defaultPath = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
QString defaultFileName = QString("%1/ScreenRecording_%2.mp4").arg(defaultPath).arg(timestamp);
qDebug() << __FUNCTION__ << "Default save path:" << defaultFileName;
QString filePath = QFileDialog::getSaveFileName(
nullptr,
tr("Save Recording"),
defaultFileName,
tr("Video Files (*.mp4)"));
if (filePath.isEmpty()) {
qDebug() << __FUNCTION__ << "User cancelled file selection";
m_statusMessage = tr("Recording cancelled");
emit statusMessageChanged();
return;
}
qDebug() << __FUNCTION__ << "Selected file path:" << filePath;
// 確保目錄存在
QFileInfo fileInfo(filePath);
QDir dir = fileInfo.dir();
if (!dir.exists()) {
qDebug() << __FUNCTION__ << "Creating directory:" << dir.path();
if (!dir.mkpath(".")) {
qDebug() << __FUNCTION__ << "Failed to create directory";
m_statusMessage = tr("Error: Could not create directory");
emit statusMessageChanged();
return;
}
}
// 設(shè)置輸出位置
QUrl fileUrl = QUrl::fromLocalFile(filePath);
qDebug() << __FUNCTION__ << "Setting output location:" << fileUrl.toString();
m_recorder.setOutputLocation(fileUrl);
// 更新質(zhì)量設(shè)置
updateQualitySettings();
// 開始錄制
qDebug() << __FUNCTION__ << "Starting recorder...";
m_recorder.record();
// 啟動計時器
m_elapsedTimer.start();
m_timer.start(1000); // 每秒更新一次
m_isRecording = true;
m_statusMessage = tr("Recording started");
emit isRecordingChanged();
emit statusMessageChanged();
qDebug() << __FUNCTION__ << "Recording started successfully";
}
void ScreenRecorder::stopRecording()
{
if (!m_isRecording) {
return;
}
qDebug() << __FUNCTION__ << "Stopping recording...";
// 獲取當前輸出位置,用于驗證
QUrl outputLocation = m_recorder.outputLocation();
qDebug() << __FUNCTION__ << "Output location:" << outputLocation.toLocalFile();
// 停止錄制
m_recorder.stop();
// 停止計時器
m_timer.stop();
// 檢查文件是否存在
QString filePath = outputLocation.toLocalFile();
QFileInfo fileInfo(filePath);
if (fileInfo.exists()) {
qDebug() << __FUNCTION__ << "File saved successfully at:" << filePath;
qDebug() << __FUNCTION__ << "File size:" << fileInfo.size() << "bytes";
m_statusMessage = tr("Recording saved to %1").arg(filePath);
} else {
qDebug() << __FUNCTION__ << "Error: File not created at:" << filePath;
m_statusMessage = tr("Error: Recording file not created");
}
m_isRecording = false;
emit isRecordingChanged();
emit statusMessageChanged();
}設(shè)置錄制器:
void ScreenRecorder::setupRecorder()
{
qDebug() << __FUNCTION__ << "Setting up recorder...";
// 設(shè)置捕獲會話
m_captureSession.setScreenCapture(&m_screenCapture);
m_captureSession.setRecorder(&m_recorder);
// 設(shè)置屏幕捕獲
m_screenCapture.setScreen(QGuiApplication::primaryScreen());
m_screenCapture.setActive(true); // 激活屏幕捕獲
qDebug() << __FUNCTION__ << "Screen set to:" << QGuiApplication::primaryScreen()->name();
qDebug() << __FUNCTION__ << "Screen capture active:" << m_screenCapture.isActive();
// 設(shè)置錄制器
QMediaFormat format;
format.setFileFormat(QMediaFormat::FileFormat::MPEG4);
format.setVideoCodec(QMediaFormat::VideoCodec::H264);
// 檢查編解碼器是否支持
QList<QMediaFormat::VideoCodec> supportedCodecs = format.supportedVideoCodecs(QMediaFormat::Encode);
qDebug() << __FUNCTION__ << "Supported video codecs:" << supportedCodecs;
if (!supportedCodecs.contains(QMediaFormat::VideoCodec::H264)) {
qDebug() << __FUNCTION__ << "Warning: H264 codec may not be supported";
// 嘗試使用第一個可用的編解碼器
if (!supportedCodecs.isEmpty()) {
format.setVideoCodec(supportedCodecs.first());
qDebug() << __FUNCTION__ << "Using alternative codec:" << supportedCodecs.first();
}
}
m_recorder.setMediaFormat(format);
qDebug() << __FUNCTION__ << "Media format set:" << format.fileFormat() << format.videoCodec();
// 應用當前質(zhì)量設(shè)置
updateQualitySettings();
// 連接信號
connect(&m_recorder, &QMediaRecorder::recorderStateChanged,
this, &ScreenRecorder::handleRecorderStateChanged);
connect(&m_recorder, &QMediaRecorder::errorOccurred,
this, &ScreenRecorder::handleError);
qDebug() << __FUNCTION__ << "Recorder setup complete";
}該功能是Qt結(jié)合ffmpeg來實現(xiàn)的,運行時會輸出相關(guān)信息:qt.multimedia.ffmpeg: Using Qt multimedia with FFmpeg version 7.1 LGPL version 2.1 or later
Qt6還提供了很多非常好用的多媒體類,可以實現(xiàn)很多豐富的功能。
到此這篇關(guān)于Qt6+QML實現(xiàn)Windows屏幕錄制功能的文章就介紹到這了,更多相關(guān)Qt6屏幕錄制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中二維數(shù)組作為函數(shù)參數(shù)來傳遞的三種方法
這篇文章主要給大家介紹了關(guān)于C語言中二維數(shù)組作為函數(shù)參數(shù)來傳遞的三種方法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用C語言有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09
C語言順序表的基本操作(初始化,插入,刪除,查詢,擴容,打印,清空等)
這篇文章主要介紹了C語言順序表的基本操作(初始化,插入,刪除,查詢,擴容,打印,清空等),具有很好的參考價值,希望對大家有所幫助。2023-02-02
jQuery移動頁面開發(fā)中主題按鈕的設(shè)計示例
這篇文章主要介紹了jQuery移動頁面開發(fā)中主題按鈕的設(shè)計示例,jQuery是當今最具人氣的JavaScript開發(fā)類庫,需要的朋友可以參考下2015-12-12

