Qt: Automatically change video file extension as appropriate

This commit is contained in:
Vicki Pfau 2023-02-24 03:51:07 -08:00
parent 11d38a8317
commit 081fe27e3a
3 changed files with 52 additions and 0 deletions

View File

@ -19,6 +19,7 @@ Other fixes:
Misc: Misc:
- Qt: Include wayland QPA in AppImage (fixes mgba.io/i/2796) - Qt: Include wayland QPA in AppImage (fixes mgba.io/i/2796)
- Qt: Stop eating boolean action key events (fixes mgba.io/i/2636) - Qt: Stop eating boolean action key events (fixes mgba.io/i/2636)
- Qt: Automatically change video file extension as appropriate
0.10.1: (2023-01-10) 0.10.1: (2023-01-10)
Emulation fixes: Emulation fixes:

View File

@ -20,6 +20,7 @@ using namespace QGBA;
QMap<QString, QString> VideoView::s_acodecMap; QMap<QString, QString> VideoView::s_acodecMap;
QMap<QString, QString> VideoView::s_vcodecMap; QMap<QString, QString> VideoView::s_vcodecMap;
QMap<QString, QString> VideoView::s_containerMap; QMap<QString, QString> VideoView::s_containerMap;
QMap<QString, QStringList> VideoView::s_extensionMap;
bool VideoView::Preset::compatible(const Preset& other) const { bool VideoView::Preset::compatible(const Preset& other) const {
if (!other.container.isNull() && !container.isNull() && other.container != container) { if (!other.container.isNull() && !container.isNull() && other.container != container) {
@ -71,6 +72,23 @@ VideoView::VideoView(QWidget* parent)
if (s_containerMap.empty()) { if (s_containerMap.empty()) {
s_containerMap["mkv"] = "matroska"; s_containerMap["mkv"] = "matroska";
} }
if (s_extensionMap.empty()) {
s_extensionMap["matroska"] += ".mkv";
s_extensionMap["matroska"] += ".mka";
s_extensionMap["webm"] += ".webm";
s_extensionMap["avi"] += ".avi";
s_extensionMap["mp4"] += ".mp4";
s_extensionMap["mp4"] += ".m4v";
s_extensionMap["mp4"] += ".m4a";
s_extensionMap["flac"] += ".flac";
s_extensionMap["mpeg"] += ".mpg";
s_extensionMap["mpeg"] += ".mpeg";
s_extensionMap["mpegts"] += ".ts";
s_extensionMap["mp3"] += ".mp3";
s_extensionMap["ogg"] += ".ogg";
s_extensionMap["ogv"] += ".ogv";
}
connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &VideoView::close); connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &VideoView::close);
connect(m_ui.start, &QAbstractButton::clicked, this, &VideoView::startRecording); connect(m_ui.start, &QAbstractButton::clicked, this, &VideoView::startRecording);
@ -195,6 +213,9 @@ void VideoView::setController(std::shared_ptr<CoreController> controller) {
} }
void VideoView::startRecording() { void VideoView::startRecording() {
if (QFileInfo(m_filename).suffix().isEmpty()) {
changeExtension();
}
if (!validateSettings()) { if (!validateSettings()) {
return; return;
} }
@ -238,6 +259,7 @@ void VideoView::selectFile() {
QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file")); QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"));
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
m_ui.filename->setText(filename); m_ui.filename->setText(filename);
changeExtension();
} }
} }
@ -289,6 +311,7 @@ void VideoView::setContainer(const QString& container) {
m_containerCstr = nullptr; m_containerCstr = nullptr;
m_container = QString(); m_container = QString();
} }
changeExtension();
validateSettings(); validateSettings();
uncheckIncompatible(); uncheckIncompatible();
} }
@ -458,6 +481,30 @@ void VideoView::uncheckIncompatible() {
} }
} }
void VideoView::changeExtension() {
if (m_filename.isEmpty()) {
return;
}
if (!s_extensionMap.contains(m_container)) {
return;
}
QStringList extensions = s_extensionMap.value(m_container);
QString filename = m_filename;
int index = m_filename.lastIndexOf(".");
if (index >= 0) {
if (extensions.contains(filename.mid(index))) {
// This extension is already valid
return;
}
filename.truncate(index);
}
filename += extensions.front();
m_ui.filename->setText(filename);
}
QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) { QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) {
QString sanitized = codec.toLower(); QString sanitized = codec.toLower();
sanitized = sanitized.remove(QChar('.')); sanitized = sanitized.remove(QChar('.'));

View File

@ -7,6 +7,7 @@
#ifdef USE_FFMPEG #ifdef USE_FFMPEG
#include <QStringList>
#include <QWidget> #include <QWidget>
#include <memory> #include <memory>
@ -62,6 +63,8 @@ private slots:
void uncheckIncompatible(); void uncheckIncompatible();
void updatePresets(); void updatePresets();
void changeExtension();
private: private:
struct Preset { struct Preset {
QString container; QString container;
@ -123,6 +126,7 @@ private:
static QMap<QString, QString> s_acodecMap; static QMap<QString, QString> s_acodecMap;
static QMap<QString, QString> s_vcodecMap; static QMap<QString, QString> s_vcodecMap;
static QMap<QString, QString> s_containerMap; static QMap<QString, QString> s_containerMap;
static QMap<QString, QStringList> s_extensionMap;
}; };
} }