window-remux.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /******************************************************************************
  2. Copyright (C) 2014 by Ruwen Hahn <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "window-remux.hpp"
  15. #include "obs-app.hpp"
  16. #include <QCloseEvent>
  17. #include <QFileDialog>
  18. #include <QFileInfo>
  19. #include <QMessageBox>
  20. #include "qt-wrappers.hpp"
  21. #include <memory>
  22. #include <cmath>
  23. using namespace std;
  24. OBSRemux::OBSRemux(const char *path, QWidget *parent)
  25. : QDialog (parent),
  26. worker (new RemuxWorker),
  27. ui (new Ui::OBSRemux),
  28. recPath (path)
  29. {
  30. ui->setupUi(this);
  31. ui->progressBar->setVisible(false);
  32. ui->remux->setEnabled(false);
  33. ui->targetFile->setEnabled(false);
  34. ui->browseTarget->setEnabled(false);
  35. ui->progressBar->setMinimum(0);
  36. ui->progressBar->setMaximum(1000);
  37. ui->progressBar->setValue(0);
  38. installEventFilter(CreateShortcutFilter());
  39. connect(ui->browseSource, &QPushButton::clicked,
  40. [&]() { BrowseInput(); });
  41. connect(ui->browseTarget, &QPushButton::clicked,
  42. [&]() { BrowseOutput(); });
  43. connect(ui->remux, &QPushButton::clicked, [&]() { Remux(); });
  44. connect(ui->sourceFile, &QLineEdit::textChanged,
  45. this, &OBSRemux::inputChanged);
  46. worker->moveToThread(&remuxer);
  47. remuxer.start();
  48. //gcc-4.8 can't use QPointer<RemuxWorker> below
  49. RemuxWorker *worker_ = worker;
  50. connect(worker_, &RemuxWorker::updateProgress,
  51. this, &OBSRemux::updateProgress);
  52. connect(&remuxer, &QThread::finished, worker_, &QObject::deleteLater);
  53. connect(worker_, &RemuxWorker::remuxFinished,
  54. this, &OBSRemux::remuxFinished);
  55. connect(this, &OBSRemux::remux, worker_, &RemuxWorker::remux);
  56. }
  57. bool OBSRemux::Stop()
  58. {
  59. if (!worker->job)
  60. return true;
  61. if (QMessageBox::critical(nullptr,
  62. QTStr("Remux.ExitUnfinishedTitle"),
  63. QTStr("Remux.ExitUnfinished"),
  64. QMessageBox::Yes | QMessageBox::No,
  65. QMessageBox::No) ==
  66. QMessageBox::Yes) {
  67. os_event_signal(worker->stop);
  68. return true;
  69. }
  70. return false;
  71. }
  72. OBSRemux::~OBSRemux()
  73. {
  74. Stop();
  75. remuxer.quit();
  76. remuxer.wait();
  77. }
  78. void OBSRemux::BrowseInput()
  79. {
  80. QString path = ui->sourceFile->text();
  81. if (path.isEmpty())
  82. path = recPath;
  83. path = QFileDialog::getOpenFileName(this,
  84. QTStr("Remux.SelectRecording"), path,
  85. QTStr("Remux.RecordingPattern"));
  86. inputChanged(path);
  87. }
  88. void OBSRemux::inputChanged(const QString &path)
  89. {
  90. if (!QFileInfo::exists(path)) {
  91. ui->remux->setEnabled(false);
  92. return;
  93. }
  94. ui->sourceFile->setText(path);
  95. ui->remux->setEnabled(true);
  96. QFileInfo fi(path);
  97. QString mp4 = fi.path() + "/" + fi.baseName() + ".mp4";
  98. ui->targetFile->setText(mp4);
  99. ui->targetFile->setEnabled(true);
  100. ui->browseTarget->setEnabled(true);
  101. }
  102. void OBSRemux::BrowseOutput()
  103. {
  104. QString path(ui->targetFile->text());
  105. path = QFileDialog::getSaveFileName(this, QTStr("Remux.SelectTarget"),
  106. path, "(*.mp4)");
  107. if (path.isEmpty())
  108. return;
  109. ui->targetFile->setText(path);
  110. }
  111. void OBSRemux::Remux()
  112. {
  113. if (QFileInfo::exists(ui->targetFile->text()))
  114. if (QMessageBox::question(this, QTStr("Remux.FileExistsTitle"),
  115. QTStr("Remux.FileExists"),
  116. QMessageBox::Yes | QMessageBox::No) !=
  117. QMessageBox::Yes)
  118. return;
  119. media_remux_job_t mr_job = nullptr;
  120. if (!media_remux_job_create(&mr_job, QT_TO_UTF8(ui->sourceFile->text()),
  121. QT_TO_UTF8(ui->targetFile->text())))
  122. return;
  123. worker->job = job_t(mr_job, media_remux_job_destroy);
  124. worker->lastProgress = 0.f;
  125. ui->progressBar->setVisible(true);
  126. ui->remux->setEnabled(false);
  127. emit remux();
  128. }
  129. void OBSRemux::closeEvent(QCloseEvent *event)
  130. {
  131. if (!Stop())
  132. event->ignore();
  133. else
  134. QDialog::closeEvent(event);
  135. }
  136. void OBSRemux::reject()
  137. {
  138. if (!Stop())
  139. return;
  140. QDialog::reject();
  141. }
  142. void OBSRemux::updateProgress(float percent)
  143. {
  144. ui->progressBar->setValue(percent * 10);
  145. }
  146. void OBSRemux::remuxFinished(bool success)
  147. {
  148. QMessageBox::information(this, QTStr("Remux.FinishedTitle"),
  149. success ?
  150. QTStr("Remux.Finished") : QTStr("Remux.FinishedError"));
  151. worker->job.reset();
  152. ui->progressBar->setVisible(false);
  153. ui->remux->setEnabled(true);
  154. }
  155. RemuxWorker::RemuxWorker()
  156. {
  157. os_event_init(&stop, OS_EVENT_TYPE_MANUAL);
  158. }
  159. RemuxWorker::~RemuxWorker()
  160. {
  161. os_event_destroy(stop);
  162. }
  163. void RemuxWorker::UpdateProgress(float percent)
  164. {
  165. if (abs(lastProgress - percent) < 0.1f)
  166. return;
  167. emit updateProgress(percent);
  168. lastProgress = percent;
  169. }
  170. void RemuxWorker::remux()
  171. {
  172. auto callback = [](void *data, float percent)
  173. {
  174. auto rw = static_cast<RemuxWorker*>(data);
  175. rw->UpdateProgress(percent);
  176. return !!os_event_try(rw->stop);
  177. };
  178. bool success = media_remux_job_process(job.get(), callback, this);
  179. emit remuxFinished(os_event_try(stop) && success);
  180. }