QProcess运行ffmpeg进程录屏笔记

因为使用ffmpeg库去实现录屏,不稳定因素太多,而且难度有点大,所以取巧使用命令行进程方式录制;

进程类

 QProcess* m_pProcess = { NULL };

初始化并连接信号与槽

 if (!m_pProcess)
    {
        m_pProcess = new QProcess(this);    
        // 错误回调
        connect(m_pProcess,SIGNAL(errorOccurred(QProcess::ProcessError)),this,SLOT(slot_errorOccurred(QProcess::ProcessError)));
        // 进程结束回调 
        connect(m_pProcess, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slot_finished(int , QProcess::ExitStatus)));
        // 外部程序错误输出
        connect(m_pProcess, SIGNAL(readyReadStandardError()), this, SLOT(slot_readyReadStandardError()));
        // 外部程序标准输出
        connect(m_pProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slot_readyReadStandardOutput()));
        // 程序开始执行
        connect(m_pProcess, SIGNAL(started()), this, SLOT(slot_started()));
        // 外部程序发生变动时
        connect(m_pProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slot_stateChanged(QProcess::ProcessState)));
    }

读取错误输入和普通输出

void record_01::slot_readyReadStandardError()
{
    log(erro, u8"运行出错");
    QByteArray errorOutput = m_pProcess->readAllStandardError();
    log(erro, errorOutput);
}

void record_01::slot_readyReadStandardOutput()
{
    log(warn, u8"运行输出");
    QByteArray errorOutput = m_pProcess->readAllStandardOutput();\
    log(warn, errorOutput);
}

执行录屏程序

 QString spath = QApplication::applicationDirPath() + "/ffmpeg.exe";
    spath += " -f gdigrab -i desktop -f mp4 d:/out.mp4";
    m_pProcess->start(spath); // ffmpeg.exe -f gdigrab -i desktop -f mp4 d:/out.mp

备注:

(1)必须在start()函数的程序主体中包含命令语句;

(2)ffmpeg打印的日志,再QProcess看来都是错误输出类型,所以需要自己去再错误输出中解析需要的信息;

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值