1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-22 18:52:30 +01: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_settings = st;
m_utils = util; m_utils = util;
m_futureWatcher.setFuture(m_future);
qRegisterMetaType<Create::State>(); qRegisterMetaType<Create::State>();
qmlRegisterType<Create>("net.aimber.create", 1, 0, "Create"); 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) void Create::createWallpaperStart(QString videoPath)
{ {
videoPath.remove("file:///"); videoPath.remove("file:///");
qDebug() << this;
QtConcurrent::run([=]() { m_future = QtConcurrent::run([=]() {
qDebug() << this;
QDir dir; QDir dir;
dir.cd(this->m_settings->localStoragePath().toString()); dir.cd(this->m_settings->localStoragePath().toString());
@ -83,6 +86,7 @@ void Create::createWallpaperStart(QString videoPath)
bool Create::createWallpaperInfo() bool Create::createWallpaperInfo()
{ {
qDebug() << this;
// Get video info // Get video info
QStringList args; QStringList args;
args.append("-print_format"); args.append("-print_format");
@ -162,9 +166,59 @@ bool Create::createWallpaperInfo()
bool Create::createWallpaperVideoPreview() bool Create::createWallpaperVideoPreview()
{ {
qDebug() << m_createWallpaperData.length << m_createWallpaperData.framerate; /*
*
* Create png
*
*/
emit createWallpaperStateChanged(Create::State::ConvertingPreviewImage);
QStringList args; 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("-loglevel");
args.append("error"); args.append("error");
args.append("-y"); args.append("-y");
@ -190,8 +244,13 @@ bool Create::createWallpaperVideoPreview()
#endif #endif
emit createWallpaperStateChanged(Create::State::ConvertingPreviewVideo); emit createWallpaperStateChanged(Create::State::ConvertingPreviewVideo);
connect(this, &Create::abortCreateWallpaper, proConvertPreviewMP4.data(), &QProcess::kill);
proConvertPreviewMP4.data()->start(); 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) { if (proConvertPreviewMP4.data()->exitStatus() == QProcess::NormalExit) {
qDebug() << "normal exit"; qDebug() << "normal exit";
} else { } else {
@ -234,8 +293,14 @@ bool Create::createWallpaperVideoPreview()
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
proConvertGif.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe"); proConvertGif.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif #endif
connect(this, &Create::abortCreateWallpaper, proConvertGif.data(), &QProcess::kill);
proConvertGif.data()->start(); 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(); QString tmpErrGif = proConvertGif.data()->readAllStandardError();
if (!tmpErrGif.isEmpty()) { if (!tmpErrGif.isEmpty()) {
QFile previewGif(m_createWallpaperData.exportPath + "/preview.gif"); QFile previewGif(m_createWallpaperData.exportPath + "/preview.gif");
@ -249,47 +314,6 @@ bool Create::createWallpaperVideoPreview()
proConvertGif.data()->close(); proConvertGif.data()->close();
emit createWallpaperStateChanged(Create::State::ConvertingPreviewGifFinished); 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; return true;
} }
@ -347,9 +371,14 @@ bool Create::createWallpaperVideo()
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
//proConvertVideo.data()->execute(QApplication::applicationDirPath() + "/ffmpeg.exe", args); //proConvertVideo.data()->execute(QApplication::applicationDirPath() + "/ffmpeg.exe", args);
#endif #endif
connect(this, &Create::abortCreateWallpaper, proConvertVideo.data(), &QProcess::kill);
proConvertVideo.data()->start(QIODevice::ReadOnly); proConvertVideo.data()->start(QIODevice::ReadOnly);
proConvertVideo.data()->waitForFinished(-1); while (!proConvertVideo.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit
disconnect(proConvertVideo.data(), &QProcess::readyReadStandardOutput, 0, 0); {
QCoreApplication::processEvents();
}
disconnect(this, &Create::abortCreateWallpaper, proConvertVideo.data(), &QProcess::kill);
disconnect(proConvertVideo.data(), &QProcess::readyReadStandardOutput, nullptr, nullptr);
QString out = proConvertVideo.data()->readAllStandardOutput(); QString out = proConvertVideo.data()->readAllStandardOutput();
@ -395,3 +424,10 @@ bool Create::createWallpaperProjectFile(const QString title, const QString descr
configFile.close(); configFile.close();
return true; return true;
} }
void Create::abort()
{
emit abortCreateWallpaper();
m_futureWatcher.cancel();
}

View File

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