Qt之AVI录屏

/******** CScreenShot.h **************/
#ifndef __CSCREENSHOT_H
#define __CSCREENSHOT_H

#include <QRunnable>
#include <QPixmap>
#include <QMutex>
#include <QWaitCondition>
#include <QThread>
#define CollectGarbage(className) class className##Collect{ public: ~className##Collect(){ if ( NULL != instance ){delete instance;instance = NULL;}}}; static className##Collect GarCollect;
class CScreenShot : public QThread
{
    Q_OBJECT
public:
    ~CScreenShot();
    static CScreenShot* getSingletonInstance();   ///< 单例
    void setControl(bool bStop);
    void getPixMap(const QPixmap& pix);
protected:
    void run();
private:
    Q_DISABLE_COPY(CScreenShot);
    CollectGarbage(CScreenShot);
    CScreenShot();
    static CScreenShot* instance;
    bool m_bStop;
    char* frameBuffer;
    QPixmap m_pix;
    QMutex mutex;
    QWaitCondition wait;
};
#endif// cscreenshot.h

/******** CScreenShot.cpp **************/
#ifdef __cplusplus
extern "C"
{
#include <avilib.h> // avilib库的头文件
}
#endif
#include "CScreenShot.h"
#include <QBuffer>
#include <QDateTime>
#include <QThread>
#include <QDir>
#include <QString>
#include <QByteArray>
CScreenShot* CScreenShot::instance = NULL;
CScreenShot* CScreenShot::getSingletonInstance()
{
    if(instance == NULL)
    {
        instance = new CScreenShot();
    }
    return instance;
}
CScreenShot::CScreenShot() :m_bStop(false)
{
}
CScreenShot::~CScreenShot(){}

void CScreenShot::getPixMap(const QPixmap& pix){
    m_pix = pix.scaled(400,300);
    wait.wakeOne();// 获取一次图片就唤醒一次线程,制作视频
}
void CScreenShot::run(){
    QString tmpFile= QDir::currentPath() + "/" + QDateTime::currentDateTime().toString("hhmmss")+".avi";
    QByteArray byte = tmpFile.toUtf8();
    char* filename = byte.data();//以当前时间定义文件名和路径
    avi_t* out_fd = AVI_open_output_file(filename); //把文件描述符绑定到此文件上
    if(out_fd == NULL){ return;}
    AVI_set_video(out_fd,160,120,70,"MJPG");//设置视频文件的格式
    int keyfame = 1;
    while(!m_bStop)
    {
        QMutexLocker lock(&mutex);
        wait.wait(&mutex); // 等待新的图片过来
        QByteArray frame;
        QBuffer buffer(&frame);
        buffer.open(QIODevice::WriteOnly);
        m_pix.save(&buffer, "jpg"); 
        //m_pix.save(QDateTime::currentDateTime().toString("hhmmss") + ".jpg","jpg");
        if(frame.data() != NULL && frame.length() !=0)
        {
            if(AVI_write_frame(out_fd,frame.data(),frame.length(),keyfame) < 0) {}
            else{ keyfame = 0;}
        }
    }
   AVI_close(out_fd);
}
/** 在其他页面构建函数体内定时将当前centraLWidget页面截取传给线程处理成AVI视频即可 **/
    mytimer = new QTimer(this);
    mytimer->setInterval(150);
    mytimer->start();
    CScreenShot::getSingletonInstance()->start();
    connect(mytimer, &QTimer::timeout,this,[&](){
    if (!mytimer->isActive()){mytimer->start();}
    QPixmap pix = QPixmap::grabWindow(CentralWidget->winId());// Qt 4.x就用这个方法,这里为了少写个函数用lambda表达式替代。
    CScreenShot::getSingletonInstance()->getPixMap(pix);});
}

资料:
Avilib库文件链接
qt超易实现录屏程序的方法
Qt5和Qt4的简单截屏

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!要在Qt中使用FFmpeg进行屏幕和音频录制,您需要以下步骤: 1. 首先,确保您已经正确安装了FFmpeg库,并且在Qt项目中配置了相应的链接器设置。 2. 创建一个Qt项目,并在需要录制屏幕和音频的位置添加以下代码段: ```cpp #include <QProcess> void startRecording() { QString outputFileName = "output.mp4"; // 设置输出文件名和路径 // 构建FFmpeg命令行参数 QStringList ffmpegArgs; ffmpegArgs << "-f" << "x11grab" << "-s" << "1366x768" << "-i" << ":0.0" << "-f" << "pulse" << "-ac" << "2" << "-i" << "default" << "-vcodec" << "libx264" << "-preset" << "ultrafast" << "-acodec" << "aac" << "-strict" << "experimental" << outputFileName; // 启动FFmpeg进程进行录制 QProcess ffmpegProcess; ffmpegProcess.start("ffmpeg", ffmpegArgs); ffmpegProcess.waitForStarted(); } void stopRecording() { // 停止FFmpeg进程 QProcess::execute("pkill", QStringList() << "ffmpeg"); } ``` 上述代码片段定义了两个函数 `startRecording` 和 `stopRecording`,分别用于开始录制和停止录制。 `startRecording` 函数中,我们设置了输出文件名和路径,并构建了一个包含必要参数的FFmpeg命令行参数列表。然后,我们使用`QProcess`类启动FFmpeg进程,并等待它开始。 `stopRecording` 函数中,我们使用`QProcess::execute`函数停止FFmpeg进程。 请注意,上述代码是一个简单示例,您可能需要根据您的特定需求进行修改和调整。 希望这能帮到您!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值