最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt6+QML實(shí)現(xiàn)Windows屏幕錄制功能

 更新時(shí)間:2025年04月18日 09:37:54   作者:luoyayun361  
Qt6提供了很多豐富的多媒體支持類(lèi),本文將為大家詳細(xì)介紹一下Qt6如何結(jié)合QML實(shí)現(xiàn)Windows屏幕錄制功能,文中的示例代碼簡(jiǎn)潔易懂,有需要的小伙伴可以參考一下

前言

Qt6提供了更豐富的多媒體支持類(lèi),使用Qt6 QMediaCaptureSession、QScreenCapture、QMediaRecorder,來(lái)實(shí)現(xiàn)一個(gè)屏幕錄制的demo,其中QScreenCapture 最低版本 Qt6.5。支持錄制的清晰度設(shè)置,選擇視頻保存位置,UI使用QML來(lái)實(shí)現(xiàn)。

Qt6還有一個(gè)比較好用的類(lèi) QWindowCapture, 可以針對(duì)窗口錄屏。使用靜態(tài)函數(shù) QList<QCapturableWindow> capturableWindows()

可以獲取當(dāng)前可用的錄制窗口,選擇窗口進(jìn)行錄制??梢栽诒綿emo的基礎(chǔ)上進(jìn)行擴(kuò)展。

效果圖

本demo使用Qt6.8 MinGW進(jìn)行編譯,注意QScreenCapture最低支持Qt6.5,所以版本不能低于6.5.

完整代碼

主要使用Qt6 QMediaCaptureSession、QScreenCapture、QMediaRecorder這三個(gè)關(guān)鍵的多媒體類(lèi)來(lái)實(shí)現(xiàn)。

關(guān)鍵代碼:

開(kāi)始錄制和結(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();
    
    // 開(kāi)始錄制
    qDebug() << __FUNCTION__ << "Starting recorder...";
    m_recorder.record();
    
    // 啟動(dòng)計(jì)時(shí)器
    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...";
    
    // 獲取當(dāng)前輸出位置,用于驗(yàn)證
    QUrl outputLocation = m_recorder.outputLocation();
    qDebug() << __FUNCTION__ << "Output location:" << outputLocation.toLocalFile();
    
    // 停止錄制
    m_recorder.stop();
    
    // 停止計(jì)時(shí)器
    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è)置捕獲會(huì)話(huà)
    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";
        // 嘗試使用第一個(gè)可用的編解碼器
        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();
    
    // 應(yīng)用當(dāng)前質(zhì)量設(shè)置
    updateQualitySettings();
    
    // 連接信號(hào)
    connect(&m_recorder, &QMediaRecorder::recorderStateChanged,
            this, &ScreenRecorder::handleRecorderStateChanged);
    connect(&m_recorder, &QMediaRecorder::errorOccurred,
            this, &ScreenRecorder::handleError);
            
    qDebug() << __FUNCTION__ << "Recorder setup complete";
}

該功能是Qt結(jié)合ffmpeg來(lái)實(shí)現(xiàn)的,運(yùn)行時(shí)會(huì)輸出相關(guān)信息:qt.multimedia.ffmpeg: Using Qt multimedia with FFmpeg version 7.1 LGPL version 2.1 or later
Qt6還提供了很多非常好用的多媒體類(lèi),可以實(shí)現(xiàn)很多豐富的功能。

到此這篇關(guān)于Qt6+QML實(shí)現(xiàn)Windows屏幕錄制功能的文章就介紹到這了,更多相關(guān)Qt6屏幕錄制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

兴化市| 荣成市| 普宁市| 日喀则市| 潮安县| 田林县| 卢龙县| 河津市| 社会| 江津市| 石首市| 公主岭市| 新民市| 阜新| 汉沽区| 彝良县| 安陆市| 绥化市| 喜德县| 蛟河市| 犍为县| 包头市| 宾阳县| 扎兰屯市| 灵丘县| 卫辉市| 阜平县| 济宁市| 藁城市| 江山市| 五峰| 诸暨市| 玛多县| 永修县| 黔东| 高邮市| 开阳县| 湟中县| 奉化市| 河南省| 荥经县|