1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-09-15 06:52:34 +02:00

Add working kill of ffmpeg process via event loop inside of the seperate thread

This commit is contained in:
kelteseth 2018-12-01 17:15:52 +01:00
parent 742fee688d
commit 615f23e861
2 changed files with 92 additions and 50 deletions

View File

@ -6,6 +6,7 @@ Create::Create(Settings* st, QMLUtilities* util, QObject* parent)
m_settings = st;
m_utils = util;
m_futureWatcher.setFuture(m_future);
qRegisterMetaType<Create::State>();
qmlRegisterType<Create>("net.aimber.create", 1, 0, "Create");
@ -50,8 +51,10 @@ bool Create::copyRecursively(const QString& srcFilePath, const QString& tgtFileP
void Create::createWallpaperStart(QString videoPath)
{
videoPath.remove("file:///");
qDebug() << this;
QtConcurrent::run([=]() {
m_future = QtConcurrent::run([=]() {
qDebug() << this;
QDir dir;
dir.cd(this->m_settings->localStoragePath().toString());
@ -83,6 +86,7 @@ void Create::createWallpaperStart(QString videoPath)
bool Create::createWallpaperInfo()
{
qDebug() << this;
// Get video info
QStringList args;
args.append("-print_format");
@ -162,9 +166,59 @@ bool Create::createWallpaperInfo()
bool Create::createWallpaperVideoPreview()
{
qDebug() << m_createWallpaperData.length << m_createWallpaperData.framerate;
/*
*
* Create png
*
*/
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImage);
QStringList args;
// args.append("-hide_banner");
args.clear();
args.append("-y");
args.append("-stats");
args.append("-ss");
args.append("00:00:02");
args.append("-i");
args.append(m_createWallpaperData.videoPath);
args.append("-vframes");
args.append("1");
args.append("-q:v");
args.append("2");
args.append(m_createWallpaperData.exportPath + "/preview.png");
QScopedPointer<QProcess> proConvertImage(new QProcess());
proConvertImage.data()->setArguments(args);
qDebug() << "Start converting video to preview gif";
#ifdef Q_OS_WIN
proConvertImage.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
proConvertImage.data()->start();
while (!proConvertImage.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit
{
QCoreApplication::processEvents();
}
QString tmpErrImg = proConvertImage.data()->readAllStandardError();
if (!tmpErrImg.isEmpty()) {
QFile previewImg(m_createWallpaperData.exportPath + "/preview.png");
if (!previewImg.exists() && !(previewImg.size() > 0)) {
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImageError);
return false;
}
}
this->processOutput(proConvertImage.data()->readAll());
proConvertImage.data()->close();
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImageFinished);
/*
*
* Create preview mp4
*
*/
args.append("-loglevel");
args.append("error");
args.append("-y");
@ -190,8 +244,13 @@ bool Create::createWallpaperVideoPreview()
#endif
emit createWallpaperStateChanged(Create::State::ConvertingPreviewVideo);
connect(this, &Create::abortCreateWallpaper, proConvertPreviewMP4.data(), &QProcess::kill);
proConvertPreviewMP4.data()->start();
proConvertPreviewMP4.data()->waitForFinished(-1);
while (!proConvertPreviewMP4.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit
{
QCoreApplication::processEvents();
}
disconnect(this, &Create::abortCreateWallpaper, proConvertPreviewMP4.data(), &QProcess::kill);
if (proConvertPreviewMP4.data()->exitStatus() == QProcess::NormalExit) {
qDebug() << "normal exit";
} else {
@ -234,8 +293,14 @@ bool Create::createWallpaperVideoPreview()
#ifdef Q_OS_WIN
proConvertGif.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
connect(this, &Create::abortCreateWallpaper, proConvertGif.data(), &QProcess::kill);
proConvertGif.data()->start();
proConvertGif.data()->waitForFinished(-1);
while (!proConvertGif.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit
{
QCoreApplication::processEvents();
}
disconnect(this, &Create::abortCreateWallpaper, proConvertGif.data(), &QProcess::kill);
QString tmpErrGif = proConvertGif.data()->readAllStandardError();
if (!tmpErrGif.isEmpty()) {
QFile previewGif(m_createWallpaperData.exportPath + "/preview.gif");
@ -249,47 +314,6 @@ bool Create::createWallpaperVideoPreview()
proConvertGif.data()->close();
emit createWallpaperStateChanged(Create::State::ConvertingPreviewGifFinished);
/*
*
* Create png
*
*/
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImage);
args.clear();
args.append("-y");
args.append("-stats");
args.append("-ss");
args.append("00:00:02");
args.append("-i");
args.append(m_createWallpaperData.videoPath);
args.append("-vframes");
args.append("1");
args.append("-q:v");
args.append("2");
args.append(m_createWallpaperData.exportPath + "/preview.png");
QScopedPointer<QProcess> proConvertImage(new QProcess());
proConvertImage.data()->setArguments(args);
qDebug() << "Start converting video to preview gif";
#ifdef Q_OS_WIN
proConvertImage.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
proConvertImage.data()->start();
proConvertImage.data()->waitForFinished(-1);
QString tmpErrImg = proConvertImage.data()->readAllStandardError();
if (!tmpErrImg.isEmpty()) {
QFile previewImg(m_createWallpaperData.exportPath + "/preview.png");
if (!previewImg.exists() && !(previewImg.size() > 0)) {
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImageError);
return false;
}
}
this->processOutput(proConvertImage.data()->readAll());
proConvertImage.data()->close();
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImageFinished);
return true;
}
@ -347,9 +371,14 @@ bool Create::createWallpaperVideo()
#ifdef Q_OS_WIN
//proConvertVideo.data()->execute(QApplication::applicationDirPath() + "/ffmpeg.exe", args);
#endif
connect(this, &Create::abortCreateWallpaper, proConvertVideo.data(), &QProcess::kill);
proConvertVideo.data()->start(QIODevice::ReadOnly);
proConvertVideo.data()->waitForFinished(-1);
disconnect(proConvertVideo.data(), &QProcess::readyReadStandardOutput, 0, 0);
while (!proConvertVideo.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit
{
QCoreApplication::processEvents();
}
disconnect(this, &Create::abortCreateWallpaper, proConvertVideo.data(), &QProcess::kill);
disconnect(proConvertVideo.data(), &QProcess::readyReadStandardOutput, nullptr, nullptr);
QString out = proConvertVideo.data()->readAllStandardOutput();
@ -395,3 +424,10 @@ bool Create::createWallpaperProjectFile(const QString title, const QString descr
configFile.close();
return true;
}
void Create::abort()
{
emit abortCreateWallpaper();
m_futureWatcher.cancel();
}

View File

@ -3,6 +3,8 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFuture>
#include <QFutureWatcher>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
@ -17,6 +19,7 @@
#include <QTimer>
#include <QUrl>
#include <QtMath>
#include <QTimer>
#include "qmlutilities.h"
#include "settings.h"
@ -64,7 +67,6 @@ public:
};
Q_ENUM(State)
QString workingDir() const
{
return m_workingDir;
@ -79,8 +81,8 @@ signals:
void createWallpaperStateChanged(Create::State state);
void processOutput(QString text);
void workingDirChanged(QString workingDir);
void progressChanged(float progress);
void abortCreateWallpaper();
public slots:
void copyProject(QString relativeProjectPath, QString toPath);
@ -94,6 +96,8 @@ public slots:
bool createWallpaperVideo();
bool createWallpaperProjectFile(const QString title, const QString description);
void abort();
void setWorkingDir(QString workingDir)
{
if (m_workingDir == workingDir)
@ -119,4 +123,6 @@ private:
CreateWallpaperData m_createWallpaperData;
QString m_workingDir;
float m_progress = 0.0f;
QFuture<void> m_future;
QFutureWatcher<void> m_futureWatcher;
};