因为使用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看来都是错误输出类型,所以需要自己去再错误输出中解析需要的信息;