diff --git a/src/mkvtoolnix-gui/merge_widget/input.cpp b/src/mkvtoolnix-gui/merge_widget/input.cpp index 4a2bb77ac..2fe261c76 100644 --- a/src/mkvtoolnix-gui/merge_widget/input.cpp +++ b/src/mkvtoolnix-gui/merge_widget/input.cpp @@ -12,7 +12,9 @@ #include "mkvtoolnix-gui/util/settings.h" #include "mkvtoolnix-gui/util/util.h" +#include #include +#include #include #include #include @@ -507,6 +509,7 @@ MergeWidget::addOrAppendFiles(bool append) { reinitFilesTracksControls(); setTitleMaybe(identifiedFiles); + setOutputFileNameMaybe(identifiedFiles[0]->m_fileName); } QStringList @@ -641,3 +644,76 @@ MergeWidget::setTitleMaybe(QString const &title) { ui->title->setText(title); m_config.m_title = title; } + +QString +MergeWidget::suggestOutputFileNameExtension() + const { + auto hasTracks = false, hasVideo = false, hasAudio = false, hasStereoscopy = false; + + for (auto const &t : m_config.m_tracks) { + if (!t->m_muxThis) + continue; + + hasTracks = true; + + if (t->isVideo()) { + hasVideo = true; + if (t->m_stereoscopy >= 2) + hasStereoscopy = true; + + } else if (t->isAudio()) + hasAudio = true; + } + + return m_config.m_webmMode ? "webm" + : hasStereoscopy ? "mk3d" + : hasVideo ? "mkv" + : hasAudio ? "mka" + : hasTracks ? "mks" + : "mkv"; +} + +void +MergeWidget::setOutputFileNameMaybe(QString const &fileName) { + auto &settings = Settings::get(); + auto policy = settings.m_outputFileNamePolicy; + + if (fileName.isEmpty() || (Settings::DontSetOutputFileName == policy)) + return; + + auto currentOutput = ui->output->text(); + auto srcFileName = QFileInfo{ currentOutput.isEmpty() ? fileName : currentOutput }; + QDir outputDir; + + if (Settings::ToPreviousDirectory == policy) + outputDir = settings.m_lastOutputDir; + + else if (Settings::ToFixedDirectory == policy) + outputDir = settings.m_fixedOutputDir; + + else if (Settings::ToParentOfFirstInputFile == policy) + outputDir = srcFileName.absoluteDir(); + + else + Q_ASSERT_X(false, "setOutputFileNameMaybe", "Untested output file name policy"); + + if (!outputDir.exists()) + outputDir = srcFileName.absoluteDir(); + + auto baseName = srcFileName.baseName(); + auto idx = 0; + + while (true) { + auto suffix = suggestOutputFileNameExtension(); + auto currentBaseName = QString{"%1%2.%3"}.arg(baseName).arg(idx ? QString{" (%1)"}.arg(idx) : "").arg(suffix); + auto outputFileName = QFileInfo{outputDir, currentBaseName}; + + if (!settings.m_uniqueOutputFileNames || !outputFileName.exists()) { + ui->output->setText(outputFileName.absoluteFilePath()); + m_config.m_destination = outputFileName.absoluteFilePath(); + break; + } + + ++idx; + } +} diff --git a/src/mkvtoolnix-gui/merge_widget/merge_widget.h b/src/mkvtoolnix-gui/merge_widget/merge_widget.h index f7aac9342..6775af2de 100644 --- a/src/mkvtoolnix-gui/merge_widget/merge_widget.h +++ b/src/mkvtoolnix-gui/merge_widget/merge_widget.h @@ -169,6 +169,9 @@ protected: virtual void setTitleMaybe(QList const &files); virtual void setTitleMaybe(QString const &title); + + virtual void setOutputFileNameMaybe(QString const &fileName); + virtual QString suggestOutputFileNameExtension() const; }; #endif // MTX_MKVTOOLNIX_GUI_MERGE_WIDGET_MERGE_WIDGET_H diff --git a/src/mkvtoolnix-gui/merge_widget/output.cpp b/src/mkvtoolnix-gui/merge_widget/output.cpp index a8ad6926e..322ceab68 100644 --- a/src/mkvtoolnix-gui/merge_widget/output.cpp +++ b/src/mkvtoolnix-gui/merge_widget/output.cpp @@ -33,8 +33,11 @@ void MergeWidget::onBrowseOutput() { auto filter = m_config.m_webmMode ? QY("WebM files") + Q(" (*.webm)") : QY("Matroska files") + Q(" (*.mkv *.mka *.mks *.mk3d)"); auto fileName = getSaveFileName(QY("Select output file name"), filter, ui->output); - if (!fileName.isEmpty()) - m_config.m_destination = fileName; + if (!fileName.isEmpty()) { + m_config.m_destination = fileName; + Settings::get().m_lastOutputDir = QFileInfo{ fileName }.absoluteDir(); + Settings::get().save(); + } } void diff --git a/src/mkvtoolnix-gui/util/settings.cpp b/src/mkvtoolnix-gui/util/settings.cpp index 23ee45b8a..1ccf9b4fb 100644 --- a/src/mkvtoolnix-gui/util/settings.cpp +++ b/src/mkvtoolnix-gui/util/settings.cpp @@ -23,20 +23,22 @@ Settings::load() { QSettings reg; reg.beginGroup("settings"); - m_mkvmergeExe = reg.value("mkvmergeExe", "mkvmerge").toString(); - m_priority = static_cast(reg.value("priority", static_cast(NormalPriority)).toInt()); - m_lastOpenDir = QDir{reg.value("lastOpenDir").toString()}; - m_lastOutputDir = QDir{reg.value("lastOutputDir").toString()}; - m_lastConfigDir = QDir{reg.value("lastConfigDir").toString()}; - m_scanForPlaylistsPolicy = static_cast(reg.value("scanForPlaylistsPolicy", static_cast(AskBeforeScanning)).toInt()); - m_minimumPlaylistDuration = reg.value("minimumPlaylistDuration", 120).toUInt(); - reg.endGroup(); + m_mkvmergeExe = reg.value("mkvmergeExe", "mkvmerge").toString(); + m_priority = static_cast(reg.value("priority", static_cast(NormalPriority)).toInt()); + m_lastOpenDir = QDir{reg.value("lastOpenDir").toString()}; + m_lastOutputDir = QDir{reg.value("lastOutputDir").toString()}; + m_lastConfigDir = QDir{reg.value("lastConfigDir").toString()}; + + m_scanForPlaylistsPolicy = static_cast(reg.value("scanForPlaylistsPolicy", static_cast(AskBeforeScanning)).toInt()); + m_minimumPlaylistDuration = reg.value("minimumPlaylistDuration", 120).toUInt(); - reg.beginGroup("features"); m_setAudioDelayFromFileName = reg.value("setAudioDelayFromFileName", true).toBool(); m_disableAVCompression = reg.value("disableAVCompression", false).toBool(); m_autoSetFileTitle = reg.value("autoSetFileTitle", true).toBool(); - m_autoSetOutputFileName = reg.value("autoSetOutputFileName", true).toBool(); + + m_uniqueOutputFileNames = reg.value("uniqueOutputFileNames", true).toBool(); + m_outputFileNamePolicy = static_cast(reg.value("outputFileNamePolicy", static_cast(ToPreviousDirectory)).toInt()); + m_fixedOutputDir = QDir{reg.value("fixedOutputDir").toString()}; reg.endGroup(); reg.beginGroup("defaults"); @@ -56,20 +58,22 @@ Settings::save() QSettings reg; reg.beginGroup("settings"); - reg.setValue("mkvmergeExe", m_mkvmergeExe); - reg.setValue("priority", static_cast(m_priority)); - reg.setValue("lastOpenDir", m_lastOpenDir.path()); - reg.setValue("lastOutputDir", m_lastOutputDir.path()); - reg.setValue("lastConfigDir", m_lastConfigDir.path()); - reg.setValue("scanForPlaylistsPolicy", static_cast(m_scanForPlaylistsPolicy)); - reg.setValue("minimumPlaylistDuration", m_minimumPlaylistDuration); - reg.endGroup(); + reg.setValue("mkvmergeExe", m_mkvmergeExe); + reg.setValue("priority", static_cast(m_priority)); + reg.setValue("lastOpenDir", m_lastOpenDir.path()); + reg.setValue("lastOutputDir", m_lastOutputDir.path()); + reg.setValue("lastConfigDir", m_lastConfigDir.path()); + + reg.setValue("scanForPlaylistsPolicy", static_cast(m_scanForPlaylistsPolicy)); + reg.setValue("minimumPlaylistDuration", m_minimumPlaylistDuration); - reg.beginGroup("features"); reg.setValue("setAudioDelayFromFileName", m_setAudioDelayFromFileName); reg.setValue("autoSetFileTitle", m_autoSetFileTitle); - reg.setValue("autoSetOutputFileName", m_autoSetOutputFileName); reg.setValue("disableAVCompression", m_disableAVCompression); + + reg.setValue("outputFileNamePolicy", static_cast(m_outputFileNamePolicy)); + reg.setValue("fixedOutputDir", m_fixedOutputDir.path()); + reg.setValue("uniqueOutputFileNames", m_uniqueOutputFileNames); reg.endGroup(); reg.beginGroup("defaults"); diff --git a/src/mkvtoolnix-gui/util/settings.h b/src/mkvtoolnix-gui/util/settings.h index 8edbf292e..a0394d513 100644 --- a/src/mkvtoolnix-gui/util/settings.h +++ b/src/mkvtoolnix-gui/util/settings.h @@ -23,10 +23,21 @@ public: NeverScan, }; + enum OutputFileNamePolicy { + DontSetOutputFileName = 0, + ToPreviousDirectory, + ToFixedDirectory, + ToParentOfFirstInputFile, + }; + QString m_mkvmergeExe, m_defaultTrackLanguage; ProcessPriority m_priority; QDir m_lastOpenDir, m_lastOutputDir, m_lastConfigDir; - bool m_setAudioDelayFromFileName, m_disableAVCompression, m_autoSetFileTitle, m_autoSetOutputFileName; + bool m_setAudioDelayFromFileName, m_disableAVCompression, m_autoSetFileTitle; + + OutputFileNamePolicy m_outputFileNamePolicy; + QDir m_fixedOutputDir; + bool m_uniqueOutputFileNames; ScanForPlaylistsPolicy m_scanForPlaylistsPolicy; unsigned int m_minimumPlaylistDuration;