1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-07 03:22:33 +01:00

Rewrote create to be more modular

This commit is contained in:
kelteseth 2018-10-14 16:16:40 +02:00
parent 3c38d0593b
commit ba4e9d2607
6 changed files with 385 additions and 354 deletions

View File

@ -122,5 +122,6 @@
<file>qml/Create/Wizards/CreateWallpaper/Page_0.qml</file>
<file>qml/Create/Wizards/CreateWallpaper/Page_1.qml</file>
<file>qml/Create/Wizards/CreateWallpaper/Page_2.qml</file>
<file>qml/Create/Wizards/CreateWallpaper/NextButton.qml</file>
</qresource>
</RCC>

View File

@ -4,11 +4,11 @@ import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import Qt.labs.platform 1.0
import QtQuick.Layouts 1.3
import net.aimber.create 1.0
Item {
id: createNew
anchors.fill: parent
Component.onCompleted: state = "in"
state: "out"
property string filePath
@ -18,6 +18,20 @@ Item {
anchors.fill: parent
}
Timer {
interval: 1000
triggeredOnStart: false
running: true
repeat: false
onTriggered: {
screenPlayCreate.createWallpaperStart(filePath)
}
}
Component.onCompleted: {
state = "in"
}
RectangularGlow {
id: effect
anchors {
@ -70,6 +84,7 @@ Item {
id: view
clip: true
currentIndex: 0
interactive: false
onCurrentIndexChanged: {
}
@ -84,10 +99,24 @@ Item {
topMargin: 0
}
interactive: false
Page_0 {
id: firstPage
id: page_0
onCanNextChanged: {
if (canNext) {
btnNext.state = "enabledPage0"
} else {
if (gifCreated) {
btnNext.state = "diabledPage0NoTitle"
} else {
btnNext.state = "diabledPage0"
}
}
}
onGifCreatedChanged: {
if (gifCreated) {
btnNext.state = "diabledPage0NoTitle"
}
}
}
Page_1 {
id: secondPage
@ -148,7 +177,7 @@ Item {
}
Row {
width: 160
width: childrenRect.width
height: 50
spacing: 20
anchors {
@ -171,16 +200,14 @@ Item {
view.setCurrentIndex(view.currentIndex - 1)
}
}
Button {
text: qsTr("Next")
Material.background: Material.Orange
Material.foreground: "white"
icon.source: "qrc:/assets/icons/icon_arrow_right.svg"
icon.color: "white"
icon.width: 16
icon.height: 16
NextButton {
id: btnNext
state: "diabledPage0"
onClicked: {
if (!page_0.canNext && !page_0.gifCreated )
return
if (view.currentIndex < view.count - 1)
view.setCurrentIndex(view.currentIndex + 1)
}

View File

@ -0,0 +1,62 @@
import QtQuick 2.9
import QtGraphicalEffects 1.0
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.2
import Qt.labs.platform 1.0
import QtQuick.Layouts 1.3
Button {
id: root
text: qsTr("Next")
Material.background: Material.Orange
Material.foreground: "white"
icon.source: "qrc:/assets/icons/icon_arrow_right.svg"
icon.color: "white"
icon.width: 16
icon.height: 16
states: [
State {
name: "enabled"
PropertyChanges {
target: root
}
},
State {
name: "diabledPage0"
PropertyChanges {
target: root
text: qsTr("Creating preview")
Material.background: Material.Grey
}
},
State {
name: "diabledPage0NoTitle"
PropertyChanges {
target: root
text: qsTr("No title set!")
Material.background: Material.Grey
}
},
State {
name: "enabledPage0"
PropertyChanges {
target: root
text: qsTr("Next")
Material.background: Material.Orange
}
}
]
transitions: [
Transition {
from: "*"
to: "*"
NumberAnimation {
property: "width"
easing.type: Easing.InOutQuad
duration: 2000
}
}
]
}

View File

@ -1,12 +1,15 @@
import QtQuick 2.9
import QtQuick.Controls 2.3
import QtGraphicalEffects 1.0
import net.aimber.create 1.0
Rectangle {
id: root
property bool allNecessaryConfigsSet: false
Rectangle {
property bool canNext: false
property bool gifCreated: false
Item {
id: rectangle1
width: parent.width * .5
anchors {
@ -33,7 +36,6 @@ Rectangle {
cornerRadius: 15
}
Rectangle {
id: imgWrapper
width: parent.width * .9
@ -46,8 +48,58 @@ Rectangle {
height: 200
color: "gray"
Image {
BusyIndicator {
id: busyIndicator
anchors.centerIn: parent
running: true
}
Text {
id: text1
color: "white"
text: qsTr("Generating preview...")
font.pixelSize: 14
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
bottomMargin: 30
}
}
Connections {
target: screenPlayCreate
onCreateWallpaperStateChanged: {
if (state === Create.State.ConvertingPreviewGifFinished) {
imgPreview.source = "file:///"
+ screenPlayCreate.workingDir + "/preview.gif"
imgPreview.visible = true
gifCreated = true
}
}
}
AnimatedImage {
id: imgPreview
asynchronous: true
playing: true
visible: false
anchors.fill: parent
}
}
ScrollView {
anchors {
top: imgWrapper.bottom
right: parent.right
bottom: parent.bottom
left: parent.left
margins: 20
}
Text {
id: txtOut
width: parent.width
}
}
}
@ -72,7 +124,13 @@ Rectangle {
placeholderText: qsTr("Name")
anchors.right: parent.right
anchors.left: parent.left
onTextChanged: {
if (textField.text.length >= 3 && gifCreated) {
canNext = true
} else {
canNext = false
}
}
}
TextField {
@ -98,9 +156,3 @@ Rectangle {
}
}
}
/*##^## Designer {
D{i:0;autoSize:true;height:768;width:1366}D{i:4;anchors_height:200;anchors_x:200;anchors_y:0}
D{i:7;anchors_height:400;anchors_width:200}D{i:3;anchors_height:200;anchors_width:683}
}
##^##*/

View File

@ -6,17 +6,11 @@ Create::Create(Settings* st, QMLUtilities* util, QObject* parent)
m_settings = st;
m_utils = util;
qRegisterMetaType<Create::State>();
qmlRegisterType<Create>("net.aimber.create", 1, 0, "Create");
}
Create::Create()
{
}
Create::~Create()
{
}
void Create::copyProject(QString relativeProjectPath, QString toPath)
{
if (toPath.contains("file:///")) {
@ -53,264 +47,56 @@ bool Create::copyRecursively(const QString& srcFilePath, const QString& tgtFileP
return true;
}
void Create::importVideo(QString title, QUrl videoPath, QUrl previewPath, int videoDuration)
void Create::createWallpaperStart(QString videoPath)
{
if (m_importState != Create::State::Idle) {
return;
}
QtConcurrent::run([=]() {
emit localWorkshopCreationStatusChanged(State::Started);
QString fromVideoPath = QString(videoPath.toString()).replace("file:///", "");
QString fromImagePath = QString(previewPath.toString()).replace("file:///", "");
QString toPath = m_settings->localStoragePath().toString() + "/" + title;
QString toPathWithVideoFile = toPath + "/" + videoPath.fileName();
QString toPathWithImageFile = toPath + "/" + previewPath.fileName();
if (QDir(toPath).exists()) {
if (!QDir(toPath).isEmpty()) {
emit localWorkshopCreationStatusChanged(State::ErrorFolder);
return;
} else {
//if(!QDir(toPath + ))
}
} else {
//TODO: Display Error
if (!QDir().mkdir(toPath)) {
emit localWorkshopCreationStatusChanged(State::ErrorFolderCreation);
return;
}
}
//Copy Video File
if (QFile::copy(fromVideoPath, toPathWithVideoFile)) {
emit localWorkshopCreationStatusChanged(State::CopyVideoFinished);
} else {
emit localWorkshopCreationStatusChanged(State::ErrorCopyVideo);
}
//Copy Image File
if (QFile::copy(fromImagePath, toPathWithImageFile)) {
emit localWorkshopCreationStatusChanged(State::CopyImageFinished);
} else {
emit localWorkshopCreationStatusChanged(State::ErrorCopyImage);
}
//Copy Project File
QFile configFile(toPath + "/" + "project.json");
if (!configFile.open(QIODevice::ReadWrite | QIODevice::Text)) {
emit localWorkshopCreationStatusChanged(State::ErrorCopyConfig);
return;
}
QTextStream out(&configFile);
QJsonObject configObj;
configObj.insert("file", videoPath.fileName());
configObj.insert("preview", previewPath.fileName());
//TODO
configObj.insert("description", "");
configObj.insert("title", title);
QJsonDocument configJsonDocument(configObj);
out << configJsonDocument.toJson();
configFile.close();
emit localWorkshopCreationStatusChanged(State::Finished);
});
}
void Create::importVideo(QString title, QUrl videoPath, int timeStamp, int videoDuration, int videoFrameRate)
{
if (m_importState != Create::State::Idle) {
return;
}
qDebug() << title << videoPath << timeStamp;
QString tmpPath = videoPath.toString();
tmpPath.remove("file:///");
QString tmpTimeStamp = QString::number(timeStamp);
QString ffmpegTimeStamp;
QString sHours;
QString sMinutes;
QString sSeconds;
int intTmpTimeStamp;
if (tmpTimeStamp.length() > 3) {
tmpTimeStamp.remove(tmpTimeStamp.length() - 3, 3);
intTmpTimeStamp = tmpTimeStamp.toInt();
const int cseconds_in_day = 86400;
const int cseconds_in_hour = 3600;
const int cseconds_in_minute = 60;
const int cseconds = 1;
int32 hours = (intTmpTimeStamp % cseconds_in_day) / cseconds_in_hour;
int32 minutes = ((intTmpTimeStamp % cseconds_in_day) % cseconds_in_hour) / cseconds_in_minute;
int32 seconds = (((intTmpTimeStamp % cseconds_in_day) % cseconds_in_hour) % cseconds_in_minute) / cseconds;
if (hours < 10) {
sHours = "0" + QString::number(hours);
} else {
sHours = QString::number(hours);
}
if (minutes < 10) {
sMinutes = "0" + QString::number(minutes);
} else {
sMinutes = QString::number(minutes);
}
if (seconds < 10) {
sSeconds = "0" + QString::number(seconds);
} else {
sSeconds = QString::number(seconds);
}
} else {
ffmpegTimeStamp = "00:00:00";
}
QString tmpVideoDuration = QString::number(videoDuration);
if (tmpVideoDuration.length() > 3)
tmpVideoDuration.remove(tmpVideoDuration.length() - 3, 3);
int videoDurationInSeconds = tmpVideoDuration.toInt();
ffmpegTimeStamp = sHours + ":" + sMinutes + ":" + sSeconds;
if (intTmpTimeStamp == 0 || videoFrameRate == 0)
qWarning() << "Cannot be zero!";
QtConcurrent::run([=]() {
QStringList args;
// Seek first because otherwise it will load
// the complete video first and this will make
// the conversion 50x slower!
args.append("-y");
args.append("-ss");
args.append(ffmpegTimeStamp);
args.append("-i");
args.append(tmpPath);
args.append("-speed");
args.append("ultrafast");
args.append("-vframes");
args.append("1");
args.append("preview.png");
QScopedPointer<QProcess> pro(new QProcess());
pro.data()->setArguments(args);
qDebug() << "Start converting video to thumbnail";
#ifdef Q_OS_WIN
pro.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
emit localWorkshopCreationStatusChanged(State::ConvertingPreviewImage);
pro.data()->start();
pro.data()->waitForFinished(-1);
qDebug() << "Done converting video to thumbnail" << pro.data()->readAllStandardOutput();
emit localWorkshopCreationStatusChanged(State::ConvertingPreviewImageFinished);
pro.data()->close();
});
}
void Create::createVideoPreview(QString path, int frames, int frameRate)
{
if (m_importState != Create::State::Idle) {
return;
}
qDebug() << frames << frameRate;
QtConcurrent::run([=]() {
QStringList args;
args.append("-y");
args.append("-stats");
args.append("-i");
args.append(path);
args.append("-speed");
args.append("ultrafast");
args.append("-vf");
// We allways want to have a 5 second clip via 24fps -> 120 frames
// Divided by the number of frames we can skip (timeInSeconds * Framrate)
// scale & crop parameter: https://unix.stackexchange.com/a/284731
args.append("select='not(mod(n," + QString::number((frames / 5)) + "))',setpts=N/FRAME_RATE/TB,crop=in_h*16/9:in_h,scale=-2:400");
// Disable audio
args.append("-an");
args.append(m_workingDir + "/preview.mp4");
QScopedPointer<QProcess> proConvertPreviewMP4(new QProcess());
proConvertPreviewMP4.data()->setArguments(args);
qDebug() << "Start converting video to preview mp4";
#ifdef Q_OS_WIN
proConvertPreviewMP4.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
proConvertPreviewMP4.data()->start();
proConvertPreviewMP4.data()->waitForFinished(-1);
qDebug() << proConvertPreviewMP4.data()->program() << proConvertPreviewMP4.data()->arguments();
qDebug() << "Done converting video to preview" << proConvertPreviewMP4.data()->readAll() << "\n"
<< proConvertPreviewMP4.data()->readAllStandardError();
proConvertPreviewMP4.data()->close();
/*
*
* Create gif
*
*/
args.clear();
args.append("-y");
args.append("-stats");
args.append("-i");
args.append(m_workingDir + "/preview.mp4");
args.append("-filter_complex");
args.append("[0:v] fps=12,scale=w=480:h=-1,split [a][b];[a] palettegen=stats_mode=single [p];[b][p] paletteuse=new=1");
args.append(m_workingDir + "/preview.gif");
QScopedPointer<QProcess> proConvertGif(new QProcess());
proConvertGif.data()->setArguments(args);
qDebug() << "Start converting video to preview gif";
#ifdef Q_OS_WIN
proConvertGif.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
proConvertGif.data()->start();
proConvertGif.data()->waitForFinished(-1);
qDebug() << proConvertGif.data()->program() << proConvertGif.data()->arguments();
qDebug() << "Done converting video to preview" << proConvertGif.data()->readAll() << "\n"
<< proConvertGif.data()->readAllStandardError();
proConvertGif.data()->close();
});
}
void Create::createWallpaper(QString videoPath)
{
if (m_importState != Create::State::Idle) {
return;
}
videoPath.remove("file:///");
QtConcurrent::run([=]() {
QDir dir;
dir.cd(m_settings->localStoragePath().toString());
m_workingDir = QString("_tmp_" + QTime::currentTime().toString()).replace(":", "");
if (dir.mkdir(m_workingDir)) {
CreateWallpaperData createWallpaperData;
createWallpaperData.videoPath = videoPath;
// Create a temp dir so we can later alter it to the workshop id
createWallpaperData.exportPath = QString(dir.path() + "/" + "_tmp_" + QTime::currentTime().toString()).replace(":", "");
if (dir.mkdir(createWallpaperData.exportPath)) {
// TODO
} else {
return;
}
m_workingDir = dir.path() + "/" + m_workingDir;
// If we return early/false this means the creation
// process did not work
// Todo: cleanup!
QtConcurrent::run([=]() {
if (!this->createWallpaperInfo(createWallpaperData))
return;
if (!this->createWallpaperVideoPreview(createWallpaperData))
return;
if (!this->createWallpaperVideo(createWallpaperData))
return;
if (!this->createWallpaperProjectFile(createWallpaperData))
return;
});
}
bool Create::createWallpaperInfo(CreateWallpaperData& createWallpaperData)
{
// Get video info
QStringList args;
args.append("-print_format");
args.append("json");
args.append("-show_format");
args.append("-show_streams");
args.append(videoPath);
args.append(createWallpaperData.videoPath);
QScopedPointer<QProcess> pro(new QProcess());
pro.data()->setArguments(args);
@ -336,17 +122,18 @@ void Create::createWallpaper(QString videoPath)
if (!okParseDuration) {
qDebug() << "Error parsing video length";
return;
return false;
}
int length = 0;
length = static_cast<int>(tmpLength);
createWallpaperData.length = length;
// Get framerate
QJsonArray arrSteams = obj.value("streams").toArray();
if (arrSteams.size() < 1) {
qDebug() << "Error container does not have any video streams";
return;
return false;
}
QJsonObject tmpObjStreams = arrSteams.at(0).toObject();
@ -359,7 +146,7 @@ void Create::createWallpaper(QString videoPath)
QStringList avgFrameRateList = avgFrameRate.split('/', QString::SkipEmptyParts);
if (avgFrameRateList.length() != 2) {
qDebug() << "Error could not parse streams with length: " << avgFrameRateList.length();
return;
return false;
}
int framerate = 0;
@ -367,7 +154,115 @@ void Create::createWallpaper(QString videoPath)
float value2 = static_cast<float>(avgFrameRateList.at(1).toInt());
framerate = qCeil(value1 / value2);
createWallpaperData.framerate = framerate;
this->createVideoPreview(videoPath, length, framerate);
});
return true;
}
bool Create::createWallpaperVideoPreview(CreateWallpaperData& createWallpaperData)
{
qDebug() << createWallpaperData.length << createWallpaperData.framerate;
QStringList args;
args.append("-y");
args.append("-stats");
args.append("-i");
args.append(createWallpaperData.videoPath);
args.append("-speed");
args.append("ultrafast");
args.append("-vf");
// We allways want to have a 5 second clip via 24fps -> 120 frames
// Divided by the number of frames we can skip (timeInSeconds * Framrate)
// scale & crop parameter: https://unix.stackexchange.com/a/284731
args.append("select='not(mod(n," + QString::number((createWallpaperData.length / 5)) + "))',setpts=N/FRAME_RATE/TB,crop=in_h*16/9:in_h,scale=-2:400");
// Disable audio
args.append("-an");
args.append(createWallpaperData.exportPath + "/preview.mp4");
QScopedPointer<QProcess> proConvertPreviewMP4(new QProcess());
proConvertPreviewMP4.data()->setArguments(args);
qDebug() << "Start converting video to preview mp4";
#ifdef Q_OS_WIN
proConvertPreviewMP4.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
emit createWallpaperStateChanged(Create::State::ConvertingPreviewVideo);
proConvertPreviewMP4.data()->start();
QScopedPointer<QTimer> processOutputTimer(new QTimer());
connect(processOutputTimer.data(), &QTimer::timeout, [&]() {
qDebug() << "### " << proConvertPreviewMP4.data()->readAll();
this->processOutput(proConvertPreviewMP4.data()->readAll());
});
processOutputTimer.data()->start(100);
proConvertPreviewMP4.data()->waitForFinished(-1);
// qDebug() << proConvertPreviewMP4.data()->program() << proConvertPreviewMP4.data()->arguments();
// qDebug() << "Done converting video to preview" << proConvertPreviewMP4.data()->readAll() << "\n"
// << proConvertPreviewMP4.data()->readAllStandardError();
proConvertPreviewMP4.data()->close();
processOutputTimer.data()->stop();
emit createWallpaperStateChanged(Create::State::ConvertingPreviewVideoFinished);
/*
*
* Create gif
*
*/
emit createWallpaperStateChanged(Create::State::ConvertingPreviewGif);
args.clear();
args.append("-y");
args.append("-stats");
args.append("-i");
args.append(createWallpaperData.exportPath + "/preview.mp4");
args.append("-filter_complex");
args.append("[0:v] fps=12,scale=w=480:h=-1,split [a][b];[a] palettegen=stats_mode=single [p];[b][p] paletteuse=new=1");
args.append(createWallpaperData.exportPath + "/preview.gif");
QScopedPointer<QProcess> proConvertGif(new QProcess());
proConvertGif.data()->setArguments(args);
qDebug() << "Start converting video to preview gif";
#ifdef Q_OS_WIN
proConvertGif.data()->setProgram(QApplication::applicationDirPath() + "/ffmpeg.exe");
#endif
proConvertGif.data()->start();
proConvertGif.data()->waitForFinished(-1);
// qDebug() << proConvertGif.data()->program() << proConvertGif.data()->arguments();
// qDebug() << "Done converting video to preview" << proConvertGif.data()->readAll() << "\n"
// << proConvertGif.data()->readAllStandardError();
proConvertGif.data()->close();
emit createWallpaperStateChanged(Create::State::ConvertingPreviewGifFinished);
return true;
}
bool Create::createWallpaperVideo(CreateWallpaperData& createWallpaperData)
{
return true;
}
bool Create::createWallpaperProjectFile(CreateWallpaperData& createWallpaperData)
{
//Copy Project File
QFile configFile(createWallpaperData.exportPath + "/project.json");
if (!configFile.open(QIODevice::ReadWrite | QIODevice::Text)) {
return false;
}
// QTextStream out(&configFile);
// QJsonObject configObj;
// configObj.insert("file", videoPath.fileName());
// configObj.insert("preview", previewPath.fileName());
// //TODO
// configObj.insert("description", "");
// configObj.insert("title", title);
// QJsonDocument configJsonDocument(configObj);
// out << configJsonDocument.toJson();
configFile.close();
return true;
}

View File

@ -3,43 +3,49 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QObject>
#include <QProcess>
#include <QQmlEngine>
#include <QString>
#include <QStringList>
#include <QTime>
#include <QUrl>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonArray>
#include <QtMath>
#include <QObject>
#include <QProcess>
#include <QQmlEngine>
#include <QScopedPointer>
#include <QString>
#include <QStringList>
#include <QTime>
#include <QTimer>
#include <QUrl>
#include <QtMath>
#include "qmlutilities.h"
#include "settings.h"
struct CreateWallpaperData {
CreateWallpaperData() {}
QString videoPath = "";
QString exportPath = "";
int length = 0;
int framerate = 0;
};
class Create : public QObject {
Q_OBJECT
public:
explicit Create(Settings* st, QMLUtilities* util, QObject* parent = nullptr);
Create();
~Create();
Q_PROPERTY(State importState READ importState WRITE setImportState NOTIFY importStateChanged)
Create() {}
~Create() {}
enum class State {
Idle,
Started,
CopyVideoFinished,
CopyImageFinished,
CopyConfigFinished,
ConvertingPreviewImage,
ConvertingPreviewImageFinished,
ConvertingPreviewVideo,
ConvertingPreviewVideoFinished,
ConvertingPreviewGif,
ConvertingPreviewGifFinished,
Finished,
ErrorFolder,
ErrorFolderCreation,
@ -52,37 +58,25 @@ public:
Q_ENUM(State)
State importState() const
{
return m_importState;
}
signals:
void localWorkshopCreationStatusChanged(State status);
void importStateChanged(State importState);
void createWallpaperStateChanged(Create::State state);
void processOutput(QString text);
public slots:
void copyProject(QString relativeProjectPath, QString toPath);
bool copyRecursively(const QString& srcFilePath, const QString& tgtFilePath);
void importVideo(QString title, QUrl videoPath, QUrl previewPath, int videoDuration);
void importVideo(QString title, QUrl videoPath, int timeStamp, int videoDuration, int videoFrameRate);
void createVideoPreview(QString path, int frames, int length);
void createWallpaper(QString videoPath);
void setImportState(State importState)
{
if (m_importState == importState)
return;
m_importState = importState;
emit importStateChanged(m_importState);
}
// Steps to convert any video to a wallpaper broken down into
// several methods in this order:
void createWallpaperStart(QString videoPath);
bool createWallpaperInfo(CreateWallpaperData& createWallpaperData);
bool createWallpaperVideoPreview(CreateWallpaperData& createWallpaperData);
bool createWallpaperVideo(CreateWallpaperData& createWallpaperData);
bool createWallpaperProjectFile(CreateWallpaperData& createWallpaperData);
private:
Settings* m_settings;
QMLUtilities* m_utils;
QString m_workingDir;
State m_importState = State::Idle;
};