RecordManager.h
#ifndef RECORD_MANAGER_H
#define RECORD_MANAGER_H
#include <QWidget>
#include <QtGui>
#include "base/common.h"
#include <QProcess>
class RecordManager :public QObject
{
Q_OBJECT
public:
void recordScreen();
void endRecordScreen();
void playback();
~RecordManager();
private:
RecordManager();
private:
QProcess m_process;
QString videopath;
public:
static RecordManager& GetInstance()
{
static RecordManager instance;
return instance;
}
};
#endif // !RECORD_MANAGER_H
RecordManager.cpp
#include "RecordManager.h"
#include <QDesktopWidget>
#include <QApplication>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QtGlobal>
RecordManager::RecordManager()
{
QString path = QDir::currentPath();//当前工作路径
videopath = path + "/image/screenVideo";
int index = videopath.lastIndexOf("/");
QString levelabovPath = videopath.left(index);
QDir dir(levelabovPath);
if (!dir.exists()) {
bool ok = dir.mkdir(levelabovPath);//只创建一级子目录,即必须保证上级目录存在
}
dir.setPath(videopath);
if (!dir.exists()) {
bool ok = dir.mkdir(videopath);//只创建一级子目录,即必须保证上级目录存在
}
}
RecordManager::~RecordManager()
{
}
void RecordManager::recordScreen()
{
#ifdef Q_OS_LINUX
// linux
#endif
#ifdef Q_OS_WIN32
// win
videopath = QDir::currentPath() + "/image/screenVideo" + "/" +
QDateTime::currentDateTime().toString("yyyy-MM-dd....hh-mm-ss") + ".mp4";
m_process.setProcessChannelMode(QProcess::MergedChannels);
QDir dir("D:/ffmpeg-master-latest-win64-gpl-shared");
if (!dir.exists()) {
//日志记录
//弹窗提示
}
QString cmd = QString("D:/ffmpeg-master-latest-win64-gpl-shared/bin/ffmpeg -f gdigrab -i desktop \
-framerate 60 -offset_x 0 -offset_y 0 -video_size 1600x900 -pix_fmt yuv420p -vcodec libx264 -crf 18 %1").arg(videopath);
m_process.start(cmd);
QString errorstring = m_process.errorString();
#endif
}
void RecordManager::endRecordScreen()
{
if (m_process.isOpen())
{
m_process.write("q");
}
}
void RecordManager::playback()
{
QDesktopWidget* desktop = QApplication::desktop();
QString path = QDir::currentPath();//当前工作路径
QString videopath = path + "/image/screenVideo/";
QDir dir(videopath);
QString cou = QString::number(dir.count() - 2);
QString videoNamePath = videopath + "Videotest" + cou + ".avi";
QMediaPlayer* mediaPlayer = new QMediaPlayer;
QRect desktopRect = QApplication::desktop()->availableGeometry();
mediaPlayer->setMedia(QUrl(videoNamePath));
QVideoWidget* videoWidget = new QVideoWidget;
mediaPlayer->setVideoOutput(videoWidget);
videoWidget->resize(desktop->width() - 10, desktop->height() - 80);
videoWidget->move((desktop->width() - videoWidget->width() - 10) / 2, (desktop->height() - videoWidget->height() - 70) / 2);
videoWidget->show();
mediaPlayer->play();
}