Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 58 additions & 21 deletions src/record_process.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// Copyright (C) 2020 - 2021 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -19,11 +19,13 @@
#include <QStandardPaths>
#include <QtDBus>
#include <QScreen>
#include <QMetaType>

Check warning on line 22 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMetaType> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QClipboard>

Check warning on line 23 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QClipboard> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QUrl>

Check warning on line 24 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QUrl> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QPointer>

Check warning on line 25 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPointer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QtConcurrent>

Check warning on line 26 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtConcurrent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dlfcn.h>

Check warning on line 27 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dlfcn.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <signal.h>

Check warning on line 28 in src/record_process.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <signal.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

const int RecordProcess::RECORD_TYPE_VIDEO = 0;
const int RecordProcess::RECORD_TYPE_GIF = 1;
Expand Down Expand Up @@ -134,33 +136,66 @@
//开始将mp4视频转码成gif
void RecordProcess::onStartTranscode()
{
qInfo() << __LINE__ << __func__ << "正在转码视频(mp4 to gif)...";
QProcess *transcodeProcess = new QProcess(this);
connect(transcodeProcess, QOverload<QProcess::ProcessError>::of(&QProcess::error),
[ = ](QProcess::ProcessError processError) {
qDebug() << "processError: " << processError;
QString captureTempDir = saveTempDir;
QString captureSavePath = savePath;
QPointer<RecordProcess> guard(this);

QtConcurrent::run([captureTempDir, captureSavePath, guard](){
QString cachePalette = captureTempDir + QDir::separator() + "deepin_screen_recorder_palette.png";
QProcess paletteProcess;
paletteProcess.start("ffmpeg", {"-i", captureSavePath, "-vf", "select=not(mod(n\\,30)),palettegen=stats_mode=diff", cachePalette, "-y"});

if (!paletteProcess.waitForFinished(2 * 60 * 1000)) {
qWarning() << "Convert palette failed!" << paletteProcess.errorString()
<< "\nStderr:" << paletteProcess.readAllStandardError();
cachePalette.clear();
}

if (guard) {
QMetaObject::invokeMethod(guard, [guard, cachePalette](){
if (guard) {
guard->onTranscodePaletteFinished(cachePalette);
}
}, Qt::QueuedConnection);
}
});
}

void RecordProcess::onTranscodePaletteFinished(const QString &palettePng)
{
QProcess *transcodeProcess = new QProcess(this);
connect(transcodeProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[ = ](int exitCode, QProcess::ExitStatus exitStatus) {
[this, transcodeProcess](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug() << "exitCode: " << exitCode << " exitStatus: " << exitStatus;
//转换进程是否正常退出
if (exitStatus == QProcess::ExitStatus::NormalExit) {
onTranscodeFinish();
} else {
qDebug() << "m_pTranscodeProcess is CrashExit:!";
}
transcodeProcess->deleteLater();
});

connect(transcodeProcess, &QProcess::errorOccurred, [transcodeProcess](QProcess::ProcessError processError) {
qWarning() << "transcodeProcess error:" << processError << transcodeProcess->errorString();
transcodeProcess->deleteLater();
});
//connect(m_pTranscodeProcess, SIGNAL(finished(int)), this, SLOT(onTranscodeFinish()));
//connect(m_pTranscodeProcess, SIGNAL(finished(int)), m_pTranscodeProcess, SLOT(deleteLater()));
QString path = savePath;

QStringList arg;
arg << "-i";
arg << savePath;
arg << "-r";
arg << "12";
arg << path.replace("mp4", "gif");
arg << "-i" << savePath;

if (!palettePng.isEmpty()) {
arg << "-i" << palettePng;
arg << "-filter_complex";
arg << "fps=12,paletteuse=dither=none:diff_mode=rectangle";
} else {
arg << "-r" << "12";
}

QFileInfo fileInfo(savePath);
QString gifPath = fileInfo.absolutePath() + QDir::separator() + fileInfo.completeBaseName() + ".gif";
arg << gifPath;

transcodeProcess->start("ffmpeg", arg);
//部分hw arm架构的机型需要这样设置
#if defined(__aarch64__)
if (Utils::isWaylandMode) {
qDebug() << "watting transcode gif end!";
Expand All @@ -173,9 +208,11 @@
void RecordProcess::onTranscodeFinish()
{
qInfo() << __LINE__ << __func__ << "已完成转码";
QString path = savePath;
QString gifOldPath = path.replace("mp4", "gif");
QString gifNewPath = QDir(saveDir).filePath(saveBaseName).replace(QString("mp4"), QString("gif"));
// 安全地修改文件后缀名,避免目录名包含 mp4 时被误伤
QFileInfo oldFileInfo(savePath);
QString gifOldPath = oldFileInfo.absolutePath() + QDir::separator() + oldFileInfo.completeBaseName() + ".gif";
QFileInfo newFileInfo(QDir(saveDir).filePath(saveBaseName));
QString gifNewPath = newFileInfo.absolutePath() + QDir::separator() + newFileInfo.completeBaseName() + ".gif";
qDebug() << "" << savePath << gifOldPath << gifNewPath;
QFile::rename(gifOldPath, gifNewPath);
exitRecord(gifNewPath);
Expand Down
9 changes: 7 additions & 2 deletions src/record_process.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// Copyright (C) 2020 - 2021 Uniontech Software Technology Co.,Ltd.
// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -144,6 +144,11 @@ private slots:
*/
void onStartTranscode();

/**
* @brief onTranscodePaletteFinished:调色板生成完成
*/
void onTranscodePaletteFinished(const QString &palettePng);

/**
* @brief onTranscodeFinish:转码完成
*/
Expand Down
Loading