window-remux.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #define RECORDING_PATTERN "(*.flv *.mp4 *.mov *.mkv *.ts *.m3u8)"
  79. void OBSRemux::BrowseInput()
  80. {
  81. QString path = ui->sourceFile->text();
  82. if (path.isEmpty())
  83. path = recPath;
  84. path = QFileDialog::getOpenFileName(this,
  85. QTStr("Remux.SelectRecording"), path,
  86. QTStr("Remux.OBSRecording") + QString(" ") +
  87. RECORDING_PATTERN);
  88. inputChanged(path);
  89. }
  90. void OBSRemux::inputChanged(const QString &path)
  91. {
  92. if (!QFileInfo::exists(path)) {
  93. ui->remux->setEnabled(false);
  94. return;
  95. }
  96. ui->sourceFile->setText(path);
  97. ui->remux->setEnabled(true);
  98. QFileInfo fi(path);
  99. QString mp4 = fi.path() + "/" + fi.baseName() + ".mp4";
  100. ui->targetFile->setText(mp4);
  101. ui->targetFile->setEnabled(true);
  102. ui->browseTarget->setEnabled(true);
  103. }
  104. void OBSRemux::BrowseOutput()
  105. {
  106. QString path(ui->targetFile->text());
  107. path = QFileDialog::getSaveFileName(this, QTStr("Remux.SelectTarget"),
  108. path, RECORDING_PATTERN);
  109. if (path.isEmpty())
  110. return;
  111. ui->targetFile->setText(path);
  112. }
  113. void OBSRemux::Remux()
  114. {
  115. if (QFileInfo::exists(ui->targetFile->text()))
  116. if (QMessageBox::question(this, QTStr("Remux.FileExistsTitle"),
  117. QTStr("Remux.FileExists"),
  118. QMessageBox::Yes | QMessageBox::No) !=
  119. QMessageBox::Yes)
  120. return;
  121. media_remux_job_t mr_job = nullptr;
  122. if (!media_remux_job_create(&mr_job, QT_TO_UTF8(ui->sourceFile->text()),
  123. QT_TO_UTF8(ui->targetFile->text())))
  124. return;
  125. worker->job = job_t(mr_job, media_remux_job_destroy);
  126. worker->lastProgress = 0.f;
  127. ui->progressBar->setVisible(true);
  128. ui->remux->setEnabled(false);
  129. emit remux();
  130. }
  131. void OBSRemux::closeEvent(QCloseEvent *event)
  132. {
  133. if (!Stop())
  134. event->ignore();
  135. else
  136. QDialog::closeEvent(event);
  137. }
  138. void OBSRemux::reject()
  139. {
  140. if (!Stop())
  141. return;
  142. QDialog::reject();
  143. }
  144. void OBSRemux::updateProgress(float percent)
  145. {
  146. ui->progressBar->setValue(percent * 10);
  147. }
  148. void OBSRemux::remuxFinished(bool success)
  149. {
  150. QMessageBox::information(this, QTStr("Remux.FinishedTitle"),
  151. success ?
  152. QTStr("Remux.Finished") : QTStr("Remux.FinishedError"));
  153. worker->job.reset();
  154. ui->progressBar->setVisible(false);
  155. ui->remux->setEnabled(true);
  156. }
  157. RemuxWorker::RemuxWorker()
  158. {
  159. os_event_init(&stop, OS_EVENT_TYPE_MANUAL);
  160. }
  161. RemuxWorker::~RemuxWorker()
  162. {
  163. os_event_destroy(stop);
  164. }
  165. void RemuxWorker::UpdateProgress(float percent)
  166. {
  167. if (abs(lastProgress - percent) < 0.1f)
  168. return;
  169. emit updateProgress(percent);
  170. lastProgress = percent;
  171. }
  172. void RemuxWorker::remux()
  173. {
  174. auto callback = [](void *data, float percent)
  175. {
  176. auto rw = static_cast<RemuxWorker*>(data);
  177. rw->UpdateProgress(percent);
  178. return !!os_event_try(rw->stop);
  179. };
  180. bool success = media_remux_job_process(job.get(), callback, this);
  181. emit remuxFinished(os_event_try(stop) && success);
  182. }