项目实战:Qt+FFmpeg录屏应用(支持帧率、清晰度设置)

若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/109827936
各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究

红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)

Qt开发专栏:项目实战(点击传送门)


需求

  实现录屏功能。


原理

  使用抓屏模块按照指定范围和帧率抓屏,同时使用录像模块按照指定图像大小和帧率录制。


Demo

  
  
  


体验下载地址

  CSDN:https://download.csdn.net/download/qq21497936/13126842
  QQ群:1047134658(点击“文件”搜索“ffmpegRecord”,群内与博文同步更新)


v1.0.0

项目实战:Qt+FFmpeg录屏应用(支持帧率、清晰度设置)_第1张图片
项目实战:Qt+FFmpeg录屏应用(支持帧率、清晰度设置)_第2张图片
项目实战:Qt+FFmpeg录屏应用(支持帧率、清晰度设置)_第3张图片

#ifndef RECORDWIDGET_H
#define RECORDWIDGET_H

#include 
#include 
#include 
#include 
#include "GrabWindowManager.h"
#include "FFmpegRecordManager.h"

namespace Ui {
class RecordWidget;
}

class RecordWidget : public QWidget
{
    Q_OBJECT

public:
    explicit RecordWidget(QWidget *parent = 0);
    ~RecordWidget();

protected slots:
    void slot_timeChange(int time);

private slots:
    void on_pushButton_startRecord_clicked();
    void on_pushButton_stopRecord_clicked();
    void on_pushButton_browser_clicked();

private:
    Ui::RecordWidget *ui;

    GrabWindowManager *_pGrabWindowManager;         // 抓屏管理类
    QThread *_pGrabWindowManagerThread;             // 抓屏线程

    FFmpegRecordManager *_pFFmpegRecordManager;     // 录制管理类
    QThread *_pFFmpegRecordManagerThread;           // 录制线程
};

#endif // RECORDWIDGET_H
#include "RecordWidget.h"
#include "ui_RecordWidget.h"
#include 

RecordWidget::RecordWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecordWidget),
    _pGrabWindowManager(0),
    _pGrabWindowManagerThread(0)
{
    ui->setupUi(this);

    QString version = "v1.0.0";
    setWindowTitle(QString("录屏Demo %1(作者:红胖子(AAA红模仿) QQ:21497936 微信:yangsir198808 博客地址:blog.csdn.net/qq21497936)").arg(version));

    // 初始化抓屏线程
    _pGrabWindowManagerThread = new QThread();
    _pGrabWindowManager = new GrabWindowManager();
    _pGrabWindowManager->moveToThread(_pGrabWindowManagerThread);
    connect(_pGrabWindowManager, SIGNAL(signal_timeChange(int)),
            this, SLOT(slot_timeChange(int)));
    _pGrabWindowManagerThread->start();
    // 初始化录制线程
    _pFFmpegRecordManagerThread = new QThread();
    _pFFmpegRecordManager = new FFmpegRecordManager();
    _pFFmpegRecordManager->moveToThread(_pFFmpegRecordManagerThread);
    connect(_pFFmpegRecordManagerThread, SIGNAL(started()),
            _pFFmpegRecordManager, SLOT(slot_start()));
    _pFFmpegRecordManagerThread->start();
    // 关联信号
    connect(_pGrabWindowManager, SIGNAL(signal_grapWindow(QImage)),
            _pFFmpegRecordManager, SLOT(slot_encoderOneFrame(QImage)));
    // 按键状态初始化
    ui->pushButton_startRecord->setEnabled(true);
    ui->pushButton_stopRecord->setEnabled(false);
    ui->lineEdit_dirPath->setReadOnly(true);
    ui->lineEdit_dirPath->setText(QCoreApplication::applicationDirPath());
    ui->lineEdit_value->setValidator(new QIntValidator(1, 100));
    ui->lineEdit_fps->setValidator(new QIntValidator(5, 50));
}

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

void RecordWidget::slot_timeChange(int time)
{
    ui->label_time->setText(QString("%1%2:%3%4").arg(time/60/10)
                                                 .arg(time/60%10)
                                                 .arg(time%60/10)
                                                 .arg(time%10));
}

void RecordWidget::on_pushButton_startRecord_clicked()
{
    slot_timeChange(0);

    QScreen *pScreen = QGuiApplication::primaryScreen();
    QRect rect = pScreen->availableGeometry();

    _pFFmpegRecordManager->setFps(ui->lineEdit_fps->text().toInt());
    _pFFmpegRecordManager->setWidthIn(rect.width());
    _pFFmpegRecordManager->setHeightIn(rect.height());
    _pFFmpegRecordManager->setWidthOut(rect.width());
    _pFFmpegRecordManager->setHeightOut(rect.height());
    _pFFmpegRecordManager->setValue(ui->lineEdit_value->text().toInt());
    _pFFmpegRecordManager->slot_startEncoder(QString("%1/%2.mp4")
                                             .arg(ui->lineEdit_dirPath->text())
                                             .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh_mm_ss")),
                                             AV_PIX_FMT_BGRA);

    _pGrabWindowManager->slot_startGrabWindow(rect.x(),
                                              rect.y(),
                                              rect.width(),
                                              rect.height(),
                                              ui->lineEdit_fps->text().toInt());

    ui->pushButton_startRecord->setEnabled(false);
    ui->pushButton_stopRecord->setEnabled(true);
    ui->pushButton_browser->setEnabled(false);
    ui->lineEdit_fps->setEnabled(false);
    ui->lineEdit_value->setEnabled(false);
}

void RecordWidget::on_pushButton_stopRecord_clicked()
{
    _pGrabWindowManager->slot_stopGrabWindow();
    _pFFmpegRecordManager->slot_stopEncoder();
    ui->pushButton_startRecord->setEnabled(true);
    ui->pushButton_stopRecord->setEnabled(false);
    ui->pushButton_browser->setEnabled(true);
    ui->lineEdit_fps->setEnabled(true);
    ui->lineEdit_value->setEnabled(true);
}

void RecordWidget::on_pushButton_browser_clicked()
{
    QString dir = QFileDialog::getExistingDirectory(0, "保存到文件夹", ".");
    if(dir.isEmpty())
    {
        return;
    }
    ui->lineEdit_dirPath->setText(dir);
}

若该文为原创文章,转载请注明原文出处
本文章博客地址:https://blog.csdn.net/qq21497936/article/details/109827936

你可能感兴趣的:(项目实战,音视频,#,FFmpeg,ffmpeg,录屏)