From eff090af47cb28e9d0621957a26efbab4fba14f8 Mon Sep 17 00:00:00 2001 From: Adam Bissen Date: Sat, 13 Jan 2024 16:36:56 +0000 Subject: [PATCH] Add ability to lower cpu affinity (Linux only for the moment). Start work on paths to save file and different args based on format. --- Main.qml | 31 +++++++++++++++++++++++++++++-- QHandbrake.cpp | 32 +++++++++++++++++++++++++++----- QHandbrake.h | 6 ++++-- main.cpp | 9 +-------- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/Main.qml b/Main.qml index c7934f9..4e3fbd0 100644 --- a/Main.qml +++ b/Main.qml @@ -73,7 +73,7 @@ Window { anchors.left: parent.left text: "Start Encode" font.pixelSize: 14 - onClicked: handbrake.startEncode() + onClicked: handbrake.startEncode(txtVideoFile.text, searchResult.text) flat: false height: font.pixelSize + 10 width: 100 @@ -93,6 +93,34 @@ Window { width: 100 } + Button { + id: btnLowerAffinity + anchors.topMargin: 25 + anchors.leftMargin: 25 + anchors.top: btnStartEncode.bottom + anchors.left: parent.left + text: "Lower Affinity" + font.pixelSize: 14 + onClicked: handbrake.setCoreUsage(18) + flat: false + height: font.pixelSize + 10 + width: 100 + } + + Button { + id: btnRaiseAffinity + anchors.topMargin: 25 + anchors.leftMargin: 25 + anchors.top: btnStartEncode.bottom + anchors.left: btnLowerAffinity.right + text: "Raise Affinity" + font.pixelSize: 14 + onClicked: handbrake.setCoreUsage(24) + flat: false + height: font.pixelSize + 10 + width: 100 + } + Text { id: handbrakeStatus anchors.left: btnVideoFile.left @@ -124,7 +152,6 @@ Window { default: hdrType.text = "Error" } - movieDB.searchMovieTitleFile(fileName); } } diff --git a/QHandbrake.cpp b/QHandbrake.cpp index 22bf3da..e7ca1fd 100644 --- a/QHandbrake.cpp +++ b/QHandbrake.cpp @@ -2,13 +2,14 @@ #include #include #include +#include +#include QHandbrake::QHandbrake(QObject *parent) : QObject{parent} { } -uint8_t QHandbrake::setPath(QString pathIn) -{ +uint8_t QHandbrake::setPath(QString pathIn) { if (QFile::exists(pathIn)) { exePath = pathIn; return 1; @@ -16,9 +17,14 @@ uint8_t QHandbrake::setPath(QString pathIn) return -1; } -uint8_t QHandbrake::startEncode() -{ +uint8_t QHandbrake::startEncode(const QString path,const QString title) { + QFileInfo inFile = QFileInfo(path); + qInfo() << inFile.absolutePath(); + qInfo() << inFile.baseName(); + //HandBrakeCLI -i "../%TITLE%/%TITLE% - 4k.mkv" -o "../%TITLE%/temp/handbrake.mkv" -f av_mkv -m -e x265_10bit --encoder-preset slower -q 20 --encoder-profile auto --all-audio -E copy --audio-copy-mask aac,eac3,dts,ac3,truehd,dtshd,mp3 --crop-mode auto --auto-anamorphic --all-subtitles > "../%TITLE%/temp/log.txt" + const QStringList args4kHD = {"-i", path, "-o", "test.mkv", "-f", "av_mkv", "-m", "-e", "x265_10bit", "--encoder-preset", "slower", "-q", "20", "--encoder-profile", "auto", "--all-audio", "-E", "copy", "--audio-copy-mask", "aac,eac3,dts,ac3,truehd,dtshd,mp3", "--crop-mode", "auto", "--auto-anamorphic", "--all-subtitles"} ; + QStringList arguments; arguments << "-i" << "Cosmos.mkv" << "-o" << "test.mkv" << "-f" << "av_mkv" << "-m" << "-e" << "x265_10bit" << "--encoder-preset" << "slower" << "-q" << "20" << "--encoder-profile" << "auto" << "--all-audio" << "-E" << "copy" << "--audio-copy-mask" << "aac,eac3,dts,ac3,truehd,dtshd,mp3" << "--crop-mode" << "auto" << "--auto-anamorphic" << "--all-subtitles"; @@ -28,6 +34,7 @@ uint8_t QHandbrake::startEncode() emit statusChanged("Encode started."); QObject::connect(HBProcess, &QProcess::readyReadStandardOutput, this, &QHandbrake::printStandardStatus); + QObject::connect(HBProcess, &QProcess::finished, HBProcess, &QHandbrake::deleteLater); return true; } @@ -39,7 +46,22 @@ uint8_t QHandbrake::stopEncode() { return true; } +uint8_t QHandbrake::setCoreUsage(uint8_t cores) { + tasksetProcess = new QProcess(parent()); + QString argCores = QString::number(QThread::idealThreadCount() - cores) + "-" + QString::number(QThread::idealThreadCount() - 1); //Set affinity to lowest numbered cores. + QStringList arguments = {"-a", "-pc", argCores, QString::number(HBProcess->processId())}; + + qInfo() << arguments; + tasksetProcess->start("/usr/bin/taskset", arguments); + QObject::connect(tasksetProcess, &QProcess::finished, tasksetProcess, &QHandbrake::deleteLater); + QObject::connect(tasksetProcess, &QProcess::readyReadStandardOutput, this, &QHandbrake::printOutput); + return true; +} + void QHandbrake::printStandardStatus() { emit statusChanged(HBProcess->readAllStandardOutput().trimmed()); - //qInfo() << HBProcess->readAllStandardOutput().trimmed(); +} + +void QHandbrake::printOutput() { + qInfo() << tasksetProcess->readAllStandardOutput().trimmed(); } diff --git a/QHandbrake.h b/QHandbrake.h index 0019e18..a043004 100644 --- a/QHandbrake.h +++ b/QHandbrake.h @@ -11,13 +11,15 @@ class QHandbrake : public QObject public: explicit QHandbrake(QObject *parent = nullptr); uint8_t setPath(QString pathIn); - Q_INVOKABLE uint8_t startEncode(); + Q_INVOKABLE uint8_t startEncode(const QString path,const QString title); Q_INVOKABLE uint8_t stopEncode(); + Q_INVOKABLE uint8_t setCoreUsage(uint8_t cores); private: QString exePath = ""; - QProcess *HBProcess; + QProcess *HBProcess, *tasksetProcess; void printStandardStatus(); + void printOutput(); signals: void statusChanged(QString status); diff --git a/main.cpp b/main.cpp index 141efbf..c982e38 100644 --- a/main.cpp +++ b/main.cpp @@ -25,17 +25,10 @@ int main(int argc, char *argv[]) } QMovieDB movieDB(settings.value("MovieDB API Key").toString()); + //movieDB.searchMovieTitle("TheWitcher"); //No Result Example //movieDB.searchMovieTitle("10 cloverfield lane"); //Single Result Example //movieDB.searchMovieTitle("The Shawshank Redemption"); //Multi-result -// //Check if HDR/DolbyVision -// enum QMediaInfo::HDR_Type hdrType = mInfo.getHDRType(__T("Cosmos.mkv")); -// if (hdrType >= 0) { -// qInfo() << hdrType; -// } else { -// qWarning() << "Could not open file."; -// } - QHandbrake handbrake; #ifdef linux if (handbrake.setPath("/usr/bin/HandBrakeCLI")) {