From 5692d6cf6d4fcee9f81b0b5bbc83fa419e409228 Mon Sep 17 00:00:00 2001 From: Elias Date: Thu, 14 Mar 2019 20:02:44 +0100 Subject: [PATCH] Cleanup converting video --- .../CreateWallpaperVideoImport.qml | 2 +- .../CreateWallpaperVideoImportConvert.qml | 16 +++---- .../CreateWallpaper/CreateWallpaperWizard.qml | 9 ++-- ScreenPlay/src/create.cpp | 43 ++++++++++--------- ScreenPlay/src/create.h | 5 +-- ScreenPlay/src/createimportvideo.cpp | 15 ++++--- ScreenPlay/src/createimportvideo.h | 10 ++--- 7 files changed, 52 insertions(+), 48 deletions(-) diff --git a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImport.qml b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImport.qml index d134f009..e8414bf4 100644 --- a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImport.qml +++ b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImport.qml @@ -236,7 +236,7 @@ Item { Connections { target: screenPlayCreate onCreateWallpaperStateChanged: { - if (state === Create.State.ConvertingVideoFinished) { + if (state === CreateImportVideo.State.ConvertingVideoFinished) { btnFinish.state = "enabled" } } diff --git a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml index 040d9b5a..544bce85 100644 --- a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml +++ b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperVideoImportConvert.qml @@ -92,7 +92,7 @@ Item { target: screenPlayCreate onCreateWallpaperStateChanged: { - if (state === Create.State.ConvertingPreviewImageFinished) { + if (state === CreateImportVideo.State.ConvertingPreviewImageFinished) { imgPreview.source = "file:///" + screenPlayCreate.workingDir + "/preview.png" imgPreview.visible = true @@ -100,30 +100,30 @@ Item { "Converting Video preview mp4") } - if (state === Create.State.ConvertingPreviewVideo) { + if (state === CreateImportVideo.State.ConvertingPreviewVideo) { txtConvert.text = qsTr( "Generating preview video...") } - if (state === Create.State.ConvertingPreviewGif) { + if (state === CreateImportVideo.State.ConvertingPreviewGif) { txtConvert.text = qsTr( "Generating preview gif...") } - if (state === Create.State.ConvertingPreviewGifFinished) { + if (state === CreateImportVideo.State.ConvertingPreviewGifFinished) { imgPreview.source = "file:///" + screenPlayCreate.workingDir + "/preview.gif" imgPreview.visible = true imgPreview.playing = true } - if (state === Create.State.ConvertingAudio) { + if (state === CreateImportVideo.State.ConvertingAudio) { txtConvert.text = qsTr("Converting Audio...") } - if (state === Create.State.ConvertingVideo) { + if (state === CreateImportVideo.State.ConvertingVideo) { txtConvert.text = qsTr("Converting Video...") } - if (state === Create.State.Finished) { + if (state === CreateImportVideo.State.Finished) { imgSuccess.source = "file:///" + screenPlayCreate.workingDir + "/preview.gif" } @@ -287,7 +287,7 @@ Item { Connections { target: screenPlayCreate onCreateWallpaperStateChanged: { - if (state === Create.State.ConvertingVideoFinished) { + if (state === CreateImportVideo.State.ConvertingVideoFinished) { btnFinish.state = "enabled" } } diff --git a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml index 00e7d609..e6fe9598 100644 --- a/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml +++ b/ScreenPlay/qml/Create/Wizards/CreateWallpaper/CreateWallpaperWizard.qml @@ -4,6 +4,7 @@ import QtQuick.Controls 2.2 import QtQuick.Controls.Material 2.3 import Qt.labs.platform 1.0 import QtQuick.Layouts 1.3 + import net.aimber.create 1.0 Item { @@ -34,10 +35,10 @@ Item { target: screenPlayCreate onCreateWallpaperStateChanged: { - if (state === Create.State.ConvertingPreviewGifError - || state === Create.State.ConvertingPreviewVideoError - || state === Create.State.ConvertingPreviewImageError - || state === Create.State.AnalyseVideoError) { + if (state === CreateImportVideo.State.ConvertingPreviewGifError + || state === CreateImportVideo.State.ConvertingPreviewVideoError + || state === CreateImportVideo.State.ConvertingPreviewImageError + || state === CreateImportVideo.State.AnalyseVideoError) { createNew.state = "error" } } diff --git a/ScreenPlay/src/create.cpp b/ScreenPlay/src/create.cpp index 6b26726b..bfab73a6 100644 --- a/ScreenPlay/src/create.cpp +++ b/ScreenPlay/src/create.cpp @@ -9,6 +9,7 @@ Create::Create(Settings* st, QMLUtilities* util, QObject* parent) qRegisterMetaType(); qmlRegisterType("net.aimber.create", 1, 0, "Create"); + qmlRegisterType("net.aimber.create", 1, 0, "Create"); } void Create::copyProject(QString relativeProjectPath, QString toPath) @@ -69,31 +70,31 @@ void Create::createWallpaperStart(QString videoPath) m_createWallpaperData.exportPath = dir.path() + "/" + folderName; setWorkingDir(m_createWallpaperData.exportPath); - auto* thread = new QThread; + m_createImportVideoThread = new QThread; m_createImportVideo = new CreateImportVideo(videoPath, m_createWallpaperData.exportPath); - connect(m_createImportVideo, SIGNAL(error(QString)), this, SLOT(errorString(QString))); - connect(thread, &QThread::started, m_createImportVideo, &CreateImportVideo::process); - connect(m_createImportVideo, &CreateImportVideo::finished, thread, &QThread::quit); + //connect(m_createImportVideo, SIGNAL(error(QString)), this, SLOT(errorString(QString))); + connect(m_createImportVideoThread, &QThread::started, m_createImportVideo, &CreateImportVideo::process); + connect(m_createImportVideo, &CreateImportVideo::finished, m_createImportVideoThread, &QThread::quit); connect(m_createImportVideo, &CreateImportVideo::finished, m_createImportVideo, &QObject::deleteLater); - connect(thread, &QThread::finished, thread, &QObject::deleteLater); - m_createImportVideo->moveToThread(thread); - thread->start(); + connect(m_createImportVideoThread, &QThread::finished, m_createImportVideoThread, &QObject::deleteLater); + connect(m_createImportVideoThread, &QThread::destroyed, this, [this]() { + QDir exportPath(m_createWallpaperData.exportPath); + + if (exportPath.exists()) { + if (!exportPath.removeRecursively()) { + emit createWallpaperStateChanged(CreateImportVideo::State::AbortCleanupError); + qWarning() << "Could not delete temp exportPath: " << exportPath; + } else { + qDebug() << "cleanup " << m_createWallpaperData.exportPath; + } + } + + }); + m_createImportVideo->moveToThread(m_createImportVideoThread); + m_createImportVideoThread->start(); } void Create::abortAndCleanup() { - emit abortCreateWallpaper(); - - - - QDir exportPath(m_createWallpaperData.exportPath); - - if (exportPath.exists()) { - if (!exportPath.removeRecursively()) { - emit createWallpaperStateChanged(CreateImportVideo::State::AbortCleanupError); - qWarning() << "Could not delete temp exportPath: " << exportPath; - } else { - qDebug() << "cleanup " << m_createWallpaperData.exportPath; - } - } + m_createImportVideoThread->requestInterruption(); } diff --git a/ScreenPlay/src/create.h b/ScreenPlay/src/create.h index 864ed6da..7e373586 100644 --- a/ScreenPlay/src/create.h +++ b/ScreenPlay/src/create.h @@ -35,8 +35,6 @@ struct CreateWallpaperData { int framerate = 0; }; - - class Create : public QObject { Q_OBJECT public: @@ -90,11 +88,12 @@ public slots: } private: - CreateImportVideo* m_createImportVideo; Settings* m_settings; + QThread* m_createImportVideoThread; QMLUtilities* m_utils; CreateWallpaperData m_createWallpaperData; QString m_workingDir; float m_progress = 0.0f; + }; diff --git a/ScreenPlay/src/createimportvideo.cpp b/ScreenPlay/src/createimportvideo.cpp index 30b10f26..e9c8206e 100644 --- a/ScreenPlay/src/createimportvideo.cpp +++ b/ScreenPlay/src/createimportvideo.cpp @@ -1,5 +1,10 @@ #include "createimportvideo.h" +CreateImportVideo::CreateImportVideo(QObject* parent) + : QObject(parent) +{ +} + CreateImportVideo::CreateImportVideo(QString videoPath, QString exportPath, QObject* parent) : QObject(parent) { @@ -140,7 +145,7 @@ bool CreateImportVideo::createWallpaperVideoPreview() #endif emit createWallpaperStateChanged(CreateImportVideo::State::ConvertingPreviewVideo); - //connect(this, &Create::abortCreateWallpaper, proConvertPreviewWebM.data(), &QProcess::kill); + proConvertPreviewWebM.data()->start(); while (!proConvertPreviewWebM.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit { @@ -154,7 +159,7 @@ bool CreateImportVideo::createWallpaperVideoPreview() } QCoreApplication::processEvents(); } - //disconnect(this, &Create::abortCreateWallpaper, proConvertPreviewWebM.data(), &QProcess::kill); + QString tmpErr = proConvertPreviewWebM.data()->readAllStandardError(); if (!tmpErr.isEmpty()) { QFile previewVideo(m_exportPath + "/preview.webm"); @@ -195,7 +200,7 @@ bool CreateImportVideo::createWallpaperGifPreview() #ifdef Q_OS_MACOS proConvertGif.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg"); #endif - //connect(this, &Create::abortCreateWallpaper, proConvertGif.data(), &QProcess::kill); + proConvertGif.data()->start(); while (!proConvertGif.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit { @@ -209,7 +214,7 @@ bool CreateImportVideo::createWallpaperGifPreview() } QCoreApplication::processEvents(); } - //disconnect(this, &Create::abortCreateWallpaper, proConvertGif.data(), &QProcess::kill); + QString tmpErrGif = proConvertGif.data()->readAllStandardError(); if (!tmpErrGif.isEmpty()) { QFile previewGif(m_exportPath + "/preview.gif"); @@ -311,7 +316,6 @@ bool CreateImportVideo::extractWallpaperAudio() pro.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg"); #endif - //connect(this, &Create::abortCreateWallpaper, proConvertAudio.data(), &QProcess::kill); proConvertAudio.data()->start(QIODevice::ReadOnly); while (!proConvertAudio.data()->waitForFinished(100)) //Wake up every 100ms and check if we must exit { @@ -325,7 +329,6 @@ bool CreateImportVideo::extractWallpaperAudio() } QCoreApplication::processEvents(); } - //disconnect(this, &Create::abortCreateWallpaper, proConvertAudio.data(), &QProcess::kill); QString tmpErrImg = proConvertAudio.data()->readAllStandardError(); if (!tmpErrImg.isEmpty()) { diff --git a/ScreenPlay/src/createimportvideo.h b/ScreenPlay/src/createimportvideo.h index 41c5ade0..79a9ecaf 100644 --- a/ScreenPlay/src/createimportvideo.h +++ b/ScreenPlay/src/createimportvideo.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include #include #include @@ -8,16 +10,15 @@ #include #include #include -#include -#include -#include #include -#include +#include +#include #include class CreateImportVideo : public QObject { Q_OBJECT public: + CreateImportVideo(QObject* parent = nullptr); explicit CreateImportVideo(QString videoPath, QString exportPath, QObject* parent = nullptr); enum class State { @@ -58,7 +59,6 @@ public slots: void process(); void requestInterruption() { - } bool createWallpaperInfo();