关于ffmpeg:项目实战QtFFmpeg录屏应用支持帧率清晰度设置

33次阅读

共计 4302 个字符,预计需要花费 11 分钟才能阅读完成。

若该文为原创文章,转载请注明原文出处
本文章博客地址: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



#ifndef RECORDWIDGET_H
#define RECORDWIDGET_H

#include <QWidget>
#include <QThread>
#include <QFileDialog>
#include <QDateTime>
#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 <QDateTime>

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

正文完
 0