File indexing completed on 2024-07-21 04:27:07

0001 /*
0002 SPDX-FileCopyrightText: 2021 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 
0004 This file is part of Kdenlive. See www.kdenlive.org.
0005 
0006 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "transcodeseek.h"
0010 #include "kdenlivesettings.h"
0011 
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QFontDatabase>
0015 #include <QPushButton>
0016 #include <QStandardPaths>
0017 #include <kxmlgui_version.h>
0018 
0019 TranscodeSeek::TranscodeSeek(bool onUserRequest, bool forceReplace, QWidget *parent)
0020     : QDialog(parent)
0021 {
0022     setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
0023     setupUi(this);
0024     if (onUserRequest) {
0025         label->setVisible(false);
0026     }
0027     setAttribute(Qt::WA_DeleteOnClose, false);
0028     setWindowTitle(i18nc("@title:window", "Transcode Clip"));
0029     KConfig conf(QStringLiteral("kdenlivetranscodingrc"), KConfig::CascadeConfig, QStandardPaths::AppDataLocation);
0030     KConfigGroup group(&conf, "intermediate");
0031     replace_original->setChecked(forceReplace || KdenliveSettings::transcodingReplace());
0032     m_encodeParams = group.entryMap();
0033     encodingprofiles->addItems(group.keyList());
0034     connect(encodingprofiles, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [&](int ix) {
0035         const QString currentParams = m_encodeParams.value(encodingprofiles->itemText(ix));
0036         if (currentParams.endsWith(QLatin1String(";audio"))) {
0037             buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Audio transcode"));
0038         } else if (currentParams.endsWith(QLatin1String(";video"))) {
0039             buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Video transcode"));
0040         } else {
0041             buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Transcode"));
0042         }
0043     });
0044     int ix = encodingprofiles->findText(KdenliveSettings::transcodeFriendly());
0045     if (ix > -1) {
0046         encodingprofiles->setCurrentIndex(ix);
0047     }
0048     const QString currentParams = m_encodeParams.value(encodingprofiles->currentText());
0049     if (currentParams.endsWith(QLatin1String(";audio"))) {
0050         buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Audio transcode"));
0051     } else if (currentParams.endsWith(QLatin1String(";video"))) {
0052         buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Video transcode"));
0053     } else {
0054         buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("Transcode"));
0055     }
0056     messagewidget->setVisible(false);
0057 }
0058 
0059 TranscodeSeek::~TranscodeSeek() {}
0060 
0061 void TranscodeSeek::addUrl(const QString &file, const QString &id, const QString &suffix, ClipType::ProducerType type, const QString &message)
0062 {
0063     QListWidgetItem *it = new QListWidgetItem(file, listWidget);
0064     it->setData(Qt::UserRole, id);
0065     it->setData(Qt::UserRole + 1, suffix);
0066     it->setData(Qt::UserRole + 2, QString::number(type));
0067     if (!message.isEmpty()) {
0068         if (messagewidget->text().isEmpty()) {
0069             messagewidget->setText(message);
0070             messagewidget->animatedShow();
0071         } else {
0072             QString mText = messagewidget->text();
0073             if (mText.length() < 120) {
0074                 mText.append(QStringLiteral("<br/>"));
0075                 mText.append(message);
0076                 messagewidget->setText(mText);
0077                 messagewidget->animatedShow();
0078             }
0079         }
0080     }
0081     const QString currentParams = m_encodeParams.value(encodingprofiles->currentText());
0082     if (listWidget->count() == 1) {
0083         QString currentParams = m_encodeParams.value(encodingprofiles->currentText());
0084         if (type == ClipType::Audio) {
0085             if (!currentParams.endsWith(QLatin1String(";audio"))) {
0086                 // Switch to audio only profile
0087                 QMapIterator<QString, QString> i(m_encodeParams);
0088                 while (i.hasNext()) {
0089                     i.next();
0090                     if (i.value().endsWith(QLatin1String(";audio"))) {
0091                         int ix = encodingprofiles->findText(i.key());
0092                         if (ix > -1) {
0093                             encodingprofiles->setCurrentIndex(ix);
0094                             break;
0095                         }
0096                     }
0097                 }
0098             }
0099         } else if (type == ClipType::Video) {
0100             if (!currentParams.endsWith(QLatin1String(";video"))) {
0101                 // Switch to video only profile
0102                 QMapIterator<QString, QString> i(m_encodeParams);
0103                 while (i.hasNext()) {
0104                     i.next();
0105                     if (i.value().endsWith(QLatin1String(";video"))) {
0106                         int ix = encodingprofiles->findText(i.key());
0107                         if (ix > -1) {
0108                             encodingprofiles->setCurrentIndex(ix);
0109                             break;
0110                         }
0111                     }
0112                 }
0113             }
0114         }
0115     } else {
0116         if ((type != ClipType::Video && currentParams.endsWith(QLatin1String(";video"))) ||
0117             (type != ClipType::Audio && currentParams.endsWith(QLatin1String(";audio")))) {
0118             // Switch back to an AV profile
0119             QMapIterator<QString, QString> i(m_encodeParams);
0120             while (i.hasNext()) {
0121                 i.next();
0122                 if (i.value().endsWith(QLatin1String(";av"))) {
0123                     int ix = encodingprofiles->findText(i.key());
0124                     if (ix > -1) {
0125                         encodingprofiles->setCurrentIndex(ix);
0126                         break;
0127                     }
0128                 }
0129             }
0130         }
0131     }
0132 }
0133 
0134 QMap<QString, QStringList> TranscodeSeek::ids() const
0135 {
0136     QMap<QString, QStringList> urls;
0137     for (int i = 0; i < listWidget->count(); i++) {
0138         QListWidgetItem *item = listWidget->item(i);
0139         urls.insert(item->data(Qt::UserRole).toString(), {item->data(Qt::UserRole + 1).toString(), item->data(Qt::UserRole + 2).toString()});
0140     }
0141     return urls;
0142 }
0143 
0144 QString TranscodeSeek::params(int clipType) const
0145 {
0146     switch (clipType) {
0147     case ClipType::Audio: {
0148         if (!m_encodeParams.value(encodingprofiles->currentText()).endsWith(QLatin1String(";audio"))) {
0149             // Switch to audio only profile
0150             QMapIterator<QString, QString> i(m_encodeParams);
0151             while (i.hasNext()) {
0152                 i.next();
0153                 if (i.value().endsWith(QLatin1String(";audio"))) {
0154                     return i.value().section(QLatin1Char(';'), 0, -2);
0155                 }
0156             }
0157         }
0158         break;
0159     }
0160     case ClipType::Video: {
0161         if (!m_encodeParams.value(encodingprofiles->currentText()).endsWith(QLatin1String(";video"))) {
0162             // Switch to video only profile
0163             QMapIterator<QString, QString> i(m_encodeParams);
0164             while (i.hasNext()) {
0165                 i.next();
0166                 if (i.value().endsWith(QLatin1String(";video"))) {
0167                     return i.value().section(QLatin1Char(';'), 0, -2);
0168                 }
0169             }
0170         }
0171         break;
0172     }
0173     default:
0174         break;
0175     }
0176     if (m_encodeParams.value(encodingprofiles->currentText()).endsWith(QLatin1String(";av"))) {
0177         // Only store selected av preset
0178         KdenliveSettings::setTranscodeFriendly(encodingprofiles->currentText());
0179     }
0180     return m_encodeParams.value(encodingprofiles->currentText()).section(QLatin1Char(';'), 0, -2);
0181 }
0182 
0183 QString TranscodeSeek::preParams() const
0184 {
0185     return QStringLiteral("-noautorotate");
0186 }