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

0001 /*
0002     SPDX-FileCopyrightText: 2008 Jean-Baptiste Mardelle <jb@kdenlive.org>
0003 
0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "cliptranscode.h"
0008 #include "kdenlivesettings.h"
0009 #include "kxmlgui_version.h"
0010 
0011 #include <QFontDatabase>
0012 #include <QStandardPaths>
0013 
0014 #include "utils/KMessageBox_KdenliveCompat.h"
0015 #include <KLocalizedString>
0016 #include <KMessageBox>
0017 
0018 ClipTranscode::ClipTranscode(QStringList urls, const QString &params, QStringList postParams, const QString &description, QString folderInfo,
0019                              bool automaticMode, QWidget *parent)
0020     : QDialog(parent)
0021     , m_urls(std::move(urls))
0022     , m_folderInfo(std::move(folderInfo))
0023     , m_duration(0)
0024     , m_automaticMode(automaticMode)
0025     , m_postParams(std::move(postParams))
0026 {
0027     setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont));
0028     setupUi(this);
0029     setAttribute(Qt::WA_DeleteOnClose);
0030     m_infoMessage = new KMessageWidget;
0031     auto *s = static_cast<QGridLayout *>(layout());
0032     s->addWidget(m_infoMessage, 10, 0, 1, -1);
0033     m_infoMessage->setCloseButtonVisible(false);
0034     m_infoMessage->hide();
0035     log_text->setHidden(true);
0036     setWindowTitle(i18nc("@title:window", "Transcode Clip"));
0037     if (m_automaticMode) {
0038         auto_add->setHidden(true);
0039     }
0040     auto_add->setText(i18ncp("@action", "Add clip to project", "Add clips to project", m_urls.count()));
0041     auto_add->setChecked(KdenliveSettings::add_new_clip());
0042 
0043     if (m_urls.count() == 1) {
0044         QString fileName = m_urls.constFirst();
0045         source_url->setUrl(QUrl::fromLocalFile(fileName));
0046         dest_url->setMode(KFile::File);
0047         dest_url->setAcceptMode(QFileDialog::AcceptSave);
0048         if (!params.isEmpty()) {
0049             QString newFile = params.section(QLatin1Char(' '), -1).replace(QLatin1String("%1"), fileName);
0050             QUrl dest = QUrl::fromLocalFile(newFile);
0051             dest_url->setUrl(dest);
0052         }
0053         urls_list->setHidden(true);
0054         connect(source_url, SIGNAL(textChanged(QString)), this, SLOT(slotUpdateParams()));
0055         ffmpeg_params->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
0056     } else {
0057         label_source->setHidden(true);
0058         source_url->setHidden(true);
0059         label_dest->setText(i18n("Destination folder"));
0060         dest_url->setMode(KFile::Directory);
0061         dest_url->setUrl(QUrl::fromLocalFile(m_urls.constFirst()).adjusted(QUrl::RemoveFilename));
0062         dest_url->setMode(KFile::Directory | KFile::ExistingOnly);
0063         for (int i = 0; i < m_urls.count(); ++i) {
0064             urls_list->addItem(m_urls.at(i));
0065         }
0066         urls_list->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
0067     }
0068     if (!params.isEmpty()) {
0069         label_profile->setHidden(true);
0070         profile_list->setHidden(true);
0071         ffmpeg_params->setPlainText(params.simplified());
0072         if (!description.isEmpty()) {
0073             transcode_info->setText(description);
0074         } else {
0075             transcode_info->setHidden(true);
0076         }
0077     } else {
0078         // load Profiles
0079         KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("kdenlivetranscodingrc"), KConfig::CascadeConfig, QStandardPaths::AppDataLocation);
0080         KConfigGroup transConfig(config, "Transcoding");
0081         // read the entries
0082         QMap<QString, QString> profiles = transConfig.entryMap();
0083         QMapIterator<QString, QString> i(profiles);
0084         while (i.hasNext()) {
0085             i.next();
0086             QStringList list = i.value().split(QLatin1Char(';'));
0087             profile_list->addItem(i.key(), list.at(0));
0088             if (list.count() > 1) {
0089                 profile_list->setItemData(profile_list->count() - 1, list.at(1), Qt::UserRole + 1);
0090             }
0091         }
0092         connect(profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
0093         slotUpdateParams(0);
0094     }
0095 
0096     connect(button_start, &QAbstractButton::clicked, this, &ClipTranscode::slotStartTransCode);
0097 
0098     m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
0099     connect(&m_transcodeProcess, &QProcess::readyReadStandardOutput, this, &ClipTranscode::slotShowTranscodeInfo);
0100     connect(&m_transcodeProcess, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, &ClipTranscode::slotTranscodeFinished);
0101 
0102     // ffmpeg_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
0103 
0104     adjustSize();
0105 }
0106 
0107 ClipTranscode::~ClipTranscode()
0108 {
0109     KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
0110     if (m_transcodeProcess.state() != QProcess::NotRunning) {
0111         m_transcodeProcess.close();
0112     }
0113     delete m_infoMessage;
0114 }
0115 
0116 void ClipTranscode::slotStartTransCode()
0117 {
0118     if (m_transcodeProcess.state() != QProcess::NotRunning) {
0119         return;
0120     }
0121     if (KdenliveSettings::ffmpegpath().isEmpty()) {
0122         // FFmpeg not detected, cannot process the Job
0123         log_text->setPlainText(i18n("FFmpeg not found, please set path in Kdenlive's settings Environment"));
0124         slotTranscodeFinished(1, QProcess::CrashExit);
0125         return;
0126     }
0127     m_duration = 0;
0128     m_destination.clear();
0129     m_infoMessage->animatedHide();
0130     QStringList parameters;
0131     QString destination;
0132     QString params = ffmpeg_params->toPlainText().simplified();
0133     if (!m_urls.isEmpty() && urls_list->count() > 0) {
0134         // We are processing multiple clips
0135         source_url->setUrl(QUrl::fromLocalFile(m_urls.takeFirst()));
0136         destination = QDir(dest_url->url().toLocalFile()).absoluteFilePath(source_url->url().fileName());
0137         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().toLocalFile(), Qt::MatchExactly);
0138         if (!matching.isEmpty()) {
0139             matching.at(0)->setFlags(Qt::ItemIsSelectable);
0140             urls_list->setCurrentItem(matching.at(0));
0141         }
0142     } else {
0143         destination = dest_url->url().toLocalFile().section(QLatin1Char('.'), 0, -2);
0144     }
0145     QString extension = params.section(QStringLiteral("%1"), 1, 1).section(QLatin1Char(' '), 0, 0);
0146     QString s_url = source_url->url().toLocalFile();
0147     bool mltEncoding = s_url.endsWith(QLatin1String(".mlt")) || s_url.endsWith(QLatin1String(".kdenlive"));
0148 
0149     if (QFile::exists(destination + extension)) {
0150         if (destination + extension == s_url) { // If the source and destination are the same, ffmpeg will fail
0151             KMessageBox::error(this, i18n("Source and destination file can't be the same"));
0152             return;
0153         }
0154         if (KMessageBox::questionTwoActions(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension), {},
0155                                             KStandardGuiItem::overwrite(), KStandardGuiItem::cancel()) != KMessageBox::PrimaryAction) {
0156             // Abort operation
0157             if (m_automaticMode) {
0158                 // inform caller that we aborted
0159                 Q_EMIT transcodedClip(source_url->url(), QUrl());
0160                 close();
0161             }
0162             return;
0163         }
0164         if (!mltEncoding) {
0165             parameters << QStringLiteral("-y");
0166         }
0167     }
0168 
0169     if (mltEncoding) {
0170         params.replace(QStringLiteral("%1"), QString("-consumer %1"));
0171         const QStringList splitted = params.split(QLatin1Char('-'), Qt::SkipEmptyParts);
0172         for (const QString &s : splitted) {
0173             QString t = s.simplified();
0174             if (t.count(QLatin1Char(' ')) == 0) {
0175                 t.append(QLatin1String("=1"));
0176             } else {
0177                 if (t.contains(QLatin1String("%1"))) {
0178                     // file name
0179                     parameters.prepend(t.section(QLatin1Char(' '), 1).replace(QLatin1String("%1"), QString("avformat:%1").arg(destination)));
0180                     parameters.prepend(QStringLiteral("-consumer"));
0181                     continue;
0182                 }
0183                 if (t.startsWith(QLatin1String("aspect "))) {
0184                     // Fix aspect ratio calculation
0185                     t.replace(QLatin1Char(' '), QLatin1String("=@"));
0186                     t.replace(QLatin1Char(':'), QLatin1String("/"));
0187                 } else {
0188                     t.replace(QLatin1Char(' '), QLatin1String("="));
0189                 }
0190             }
0191             parameters << t;
0192         }
0193         parameters.prepend(s_url);
0194         buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
0195         m_destination = destination + extension;
0196         m_transcodeProcess.start(KdenliveSettings::meltpath(), parameters);
0197         source_url->setEnabled(false);
0198         dest_url->setEnabled(false);
0199         button_start->setEnabled(false);
0200         return;
0201     }
0202     if (params.contains(QLatin1String("-i "))) {
0203         // Filename must be inserted later
0204     } else {
0205         parameters << QStringLiteral("-i") << s_url;
0206     }
0207 
0208     bool replaceVfParams = false;
0209     const QStringList splitted = params.split(QLatin1Char(' '));
0210     for (QString s : splitted) {
0211         if (replaceVfParams) {
0212             parameters << m_postParams.at(1);
0213             replaceVfParams = false;
0214         } else if (s.startsWith(QLatin1String("%1"))) {
0215             parameters << s.replace(0, 2, destination);
0216         } else if (!m_postParams.isEmpty() && s == QLatin1String("-vf")) {
0217             replaceVfParams = true;
0218             parameters << s;
0219         } else if (s == QLatin1String("-i")) {
0220             parameters << s;
0221             parameters << s_url;
0222         } else {
0223             parameters << s;
0224         }
0225     }
0226     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
0227     m_destination = destination + extension;
0228     m_transcodeProcess.start(KdenliveSettings::ffmpegpath(), parameters);
0229     source_url->setEnabled(false);
0230     dest_url->setEnabled(false);
0231     button_start->setEnabled(false);
0232 }
0233 
0234 void ClipTranscode::slotShowTranscodeInfo()
0235 {
0236     QString log = QString::fromLatin1(m_transcodeProcess.readAll());
0237     if (m_duration == 0) {
0238         if (log.contains(QStringLiteral("Duration:"))) {
0239             QString duration = log.section(QStringLiteral("Duration:"), 1, 1).section(QLatin1Char(','), 0, 0).simplified();
0240             QStringList numbers = duration.split(QLatin1Char(':'));
0241             if (numbers.size() < 3) {
0242                 return;
0243             }
0244             m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toInt();
0245             log_text->setHidden(true);
0246             job_progress->setHidden(false);
0247         } else {
0248             log_text->setHidden(false);
0249             job_progress->setHidden(true);
0250         }
0251     } else if (log.contains(QStringLiteral("time="))) {
0252         int progress;
0253         QString time = log.section(QStringLiteral("time="), 1, 1).simplified().section(QLatin1Char(' '), 0, 0);
0254         if (time.contains(QLatin1Char(':'))) {
0255             QStringList numbers = time.split(QLatin1Char(':'));
0256             if (numbers.size() < 3) {
0257                 return;
0258             }
0259             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toInt();
0260         } else {
0261             progress = time.toInt();
0262         }
0263         job_progress->setValue(int(100.0 * progress / m_duration));
0264     }
0265     log_text->setPlainText(log);
0266 }
0267 
0268 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
0269 {
0270     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
0271     button_start->setEnabled(true);
0272     source_url->setEnabled(true);
0273     dest_url->setEnabled(true);
0274     m_duration = 0;
0275 
0276     if (QFileInfo(m_destination).size() <= 0) {
0277         // Destination file does not exist, transcoding failed
0278         exitCode = 1;
0279     }
0280     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
0281         log_text->setHtml(log_text->toPlainText() + QStringLiteral("<br /><b>") + i18n("Transcoding finished."));
0282         if (auto_add->isChecked() || m_automaticMode) {
0283             QUrl url;
0284             if (urls_list->count() > 0) {
0285                 QString params = ffmpeg_params->toPlainText().simplified();
0286                 QString extension = params.section(QStringLiteral("%1"), 1, 1).section(QLatin1Char(' '), 0, 0);
0287                 url = QUrl::fromLocalFile(dest_url->url().toLocalFile() + QDir::separator() + source_url->url().fileName() + extension);
0288             } else {
0289                 url = dest_url->url();
0290             }
0291             if (m_automaticMode) {
0292                 Q_EMIT transcodedClip(source_url->url(), url);
0293             } else {
0294                 Q_EMIT addClip(url, m_folderInfo);
0295             }
0296         }
0297         if (urls_list->count() > 0 && m_urls.count() > 0) {
0298             m_transcodeProcess.close();
0299             slotStartTransCode();
0300             return;
0301         }
0302         if (auto_close->isChecked()) {
0303             accept();
0304         } else {
0305             m_infoMessage->setMessageType(KMessageWidget::Positive);
0306             m_infoMessage->setText(i18n("Transcoding finished."));
0307             m_infoMessage->animatedShow();
0308         }
0309     } else {
0310         m_infoMessage->setMessageType(KMessageWidget::Warning);
0311         m_infoMessage->setText(i18n("Transcoding failed"));
0312         m_infoMessage->animatedShow();
0313         log_text->setVisible(true);
0314     }
0315     m_transcodeProcess.close();
0316 
0317     // Refill url list in case user wants to transcode to another format
0318     if (urls_list->count() > 0) {
0319         m_urls.clear();
0320         for (int i = 0; i < urls_list->count(); ++i) {
0321             m_urls << urls_list->item(i)->text();
0322         }
0323     }
0324 }
0325 
0326 void ClipTranscode::slotUpdateParams(int ix)
0327 {
0328     QString fileName = source_url->url().toLocalFile();
0329     if (ix != -1) {
0330         QString params = profile_list->itemData(ix).toString();
0331         ffmpeg_params->setPlainText(params.simplified());
0332         QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
0333         if (!desc.isEmpty()) {
0334             transcode_info->setText(desc);
0335             transcode_info->setHidden(false);
0336         } else {
0337             transcode_info->setHidden(true);
0338         }
0339     }
0340     if (urls_list->count() == 0) {
0341         QString newFile = ffmpeg_params->toPlainText().simplified().section(QLatin1Char(' '), -1).replace(QLatin1String("%1"), fileName);
0342         dest_url->setUrl(QUrl::fromLocalFile(newFile));
0343     }
0344 }