]> git.sesse.net Git - kdenlive/blobdiff - src/renderwidget.cpp
render widget: Enable entering of custom bitrate when "bitrate" is found in the param...
[kdenlive] / src / renderwidget.cpp
index 40d2324b78de3d013666d094e7e69fbc362add11..4877f36afeb2724b2b985daf5c9a9bf5e7b2cf09 100644 (file)
@@ -22,6 +22,7 @@
 #include "kdenlivesettings.h"
 #include "ui_saveprofile_ui.h"
 #include "timecode.h"
+#include "profilesdialog.h"
 
 #include <KStandardDirs>
 #include <KDebug>
@@ -44,6 +45,7 @@
 #include <QDBusConnectionInterface>
 #include <QDBusInterface>
 #include <QThread>
+#include <QScriptEngine>
 
 const int GroupRole = Qt::UserRole;
 const int ExtensionRole = GroupRole + 1;
@@ -53,6 +55,8 @@ const int ParamsRole = GroupRole + 4;
 const int EditableRole = GroupRole + 5;
 const int MetaGroupRole = GroupRole + 6;
 const int ExtraRole = GroupRole + 7;
+const int BitratesRole = GroupRole + 8;
+const int DefaultBitrateRole = GroupRole + 9;
 
 // Running job status
 const int WAITINGJOB = 0;
@@ -63,7 +67,8 @@ const int FINISHEDJOB = 2;
 RenderWidget::RenderWidget(const QString &projectfolder, bool enableProxy, QWidget * parent) :
         QDialog(parent),
         m_projectFolder(projectfolder),
-        m_blockProcessing(false)
+        m_blockProcessing(false),
+        m_isPal(true)
 {
     m_view.setupUi(this);
     setWindowTitle(i18n("Rendering"));
@@ -92,10 +97,10 @@ RenderWidget::RenderWidget(const QString &projectfolder, bool enableProxy, QWidg
     
     m_view.proxy_render->setHidden(!enableProxy);
 
-       m_view.encoder_threads->setMaximum(QThread::idealThreadCount());
-       m_view.encoder_threads->setValue(KdenliveSettings::encodethreads());
-       connect(m_view.encoder_threads, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateEncodeThreads(int)));
-       
+    m_view.encoder_threads->setMaximum(QThread::idealThreadCount());
+    m_view.encoder_threads->setValue(KdenliveSettings::encodethreads());
+    connect(m_view.encoder_threads, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateEncodeThreads(int)));
+
     m_view.rescale_keep->setChecked(KdenliveSettings::rescalekeepratio());
     connect(m_view.rescale_width, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateRescaleWidth(int)));
     connect(m_view.rescale_height, SIGNAL(valueChanged(int)), this, SLOT(slotUpdateRescaleHeight(int)));
@@ -164,7 +169,6 @@ RenderWidget::RenderWidget(const QString &projectfolder, bool enableProxy, QWidg
     connect(m_view.guide_end, SIGNAL(activated(int)), this, SLOT(slotCheckStartGuidePosition()));
     connect(m_view.guide_start, SIGNAL(activated(int)), this, SLOT(slotCheckEndGuidePosition()));
 
-    connect(m_view.format_selection, SIGNAL(activated(int)), this, SLOT(refreshView()));
     connect(m_view.tc_overlay, SIGNAL(toggled(bool)), m_view.tc_type, SLOT(setEnabled(bool)));
 
     m_view.buttonRender->setEnabled(false);
@@ -176,6 +180,7 @@ RenderWidget::RenderWidget(const QString &projectfolder, bool enableProxy, QWidg
     m_view.open_browser->setVisible(false);
     m_view.error_box->setVisible(false);
     m_view.tc_type->setEnabled(false);
+    m_view.checkTwoPass->setEnabled(false);
 
     m_view.splitter->setStretchFactor(1, 5);
     m_view.splitter->setStretchFactor(0, 2);
@@ -734,7 +739,7 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
         double fps = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
         guideStart = m_view.guide_start->itemData(m_view.guide_start->currentIndex()).toDouble();
         guideEnd = m_view.guide_end->itemData(m_view.guide_end->currentIndex()).toDouble();
-        render_process_args << "in=" + QString::number(GenTime(guideStart).frames(fps)) << "out=" + QString::number(GenTime(guideEnd).frames(fps));
+        render_process_args << "in=" + QString::number((int) GenTime(guideStart).frames(fps)) << "out=" + QString::number((int) GenTime(guideEnd).frames(fps));
     }
 
     if (!overlayargs.isEmpty()) render_process_args << "preargs=" + overlayargs.join(" ");
@@ -755,7 +760,7 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
         width = m_profile.width;
         height = m_profile.height;
     }
-    renderArgs.replace("%dar", '@' + QString::number(m_profile.display_aspect_num) + '/' + QString::number(m_profile.display_aspect_den));
+
     //renderArgs.replace("%width", QString::number((int)(m_profile.height * m_profile.display_aspect_num / (double) m_profile.display_aspect_den + 0.5)));
     //renderArgs.replace("%height", QString::number((int)m_profile.height));
 
@@ -766,9 +771,9 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
     // disable audio if requested
     if (!exportAudio) renderArgs.append(" an=1 ");
 
-       // Set the thread counts
-       renderArgs.append(QString(" threads=%1").arg(KdenliveSettings::encodethreads()));
-       renderArgs.append(QString(" real_time=-%1").arg(KdenliveSettings::mltthreads()));
+    // Set the thread counts
+    renderArgs.append(QString(" threads=%1").arg(KdenliveSettings::encodethreads()));
+    renderArgs.append(QString(" real_time=-%1").arg(KdenliveSettings::mltthreads()));
 
     // Check if the rendering profile is different from project profile,
     // in which case we need to use the producer_comsumer from MLT
@@ -789,27 +794,36 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
     }
     bool resizeProfile = (subsize != currentSize);
     QStringList paramsList = renderArgs.split(" ", QString::SkipEmptyParts);
-    for (int i = 0; i < paramsList.count(); i++) {
-        if (paramsList.at(i).startsWith("profile=")) {
-            if (paramsList.at(i).section('=', 1) != m_profile.path) resizeProfile = true;
-            break;
+
+    QScriptEngine sEngine;
+    sEngine.globalObject().setProperty("bitrate", m_view.comboBitrates->currentText());
+    sEngine.globalObject().setProperty("dar", '@' + QString::number(m_profile.display_aspect_num) + '/' + QString::number(m_profile.display_aspect_den));
+    sEngine.globalObject().setProperty("passes", static_cast<int>(m_view.checkTwoPass->isChecked()) + 1);
+
+    for (int i = 0; i < paramsList.count(); ++i) {
+        QString paramName = paramsList.at(i).section('=', 0, 0);
+        QString paramValue = paramsList.at(i).section('=', 1, 1);
+        // If the profiles do not match we need to use the consumer tag
+        if (paramName == "mlt_profile=" && paramValue != m_profile.path) {
+            resizeProfile = true;
+        }
+        // evaluate expression
+        if (paramValue.startsWith('%')) {
+            paramValue = sEngine.evaluate(paramValue.remove(0, 1)).toString();
+            paramsList[i] = paramName + '=' + paramValue;
         }
+        sEngine.globalObject().setProperty(paramName.toUtf8().constData(), paramValue);
     }
-       
+
     if (resizeProfile)
         render_process_args << "consumer:" + (scriptExport ? "$SOURCE" : playlistPath);
     else
         render_process_args <<  (scriptExport ? "$SOURCE" : playlistPath);
-    render_process_args << (scriptExport ? "$TARGET" : dest);
+    render_process_args << (scriptExport ? "$TARGET" : KUrl(dest).url());
     render_process_args << paramsList;
 
     QString group = m_view.size_list->currentItem()->data(MetaGroupRole).toString();
 
-    QStringList renderParameters;
-    renderParameters << dest << item->data(RenderRole).toString() << renderArgs.simplified();
-    renderParameters << QString::number(zoneIn) << QString::number(zoneOut) << QString::number(m_view.play_after->isChecked());
-    renderParameters << QString::number(guideStart) << QString::number(guideEnd) << QString::number(resizeProfile);
-
     QString scriptName;
     if (scriptExport) {
         // Generate script file
@@ -821,7 +835,7 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
         QTextStream outStream(&file);
         outStream << "#! /bin/sh" << "\n" << "\n";
         outStream << "SOURCE=" << "\"" + playlistPath + "\"" << "\n";
-        outStream << "TARGET=" << "\"" + dest + "\"" << "\n";
+        outStream << "TARGET=" << "\"" + KUrl(dest).url() + "\"" << "\n";
         outStream << "RENDERER=" << "\"" + m_renderer + "\"" << "\n";
         outStream << "MELT=" << "\"" + render_process_args.takeFirst() + "\"" << "\n";
         outStream << "PARAMETERS=" << "\"" + render_process_args.join(" ") + "\"" << "\n";
@@ -838,7 +852,6 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
         m_view.tabWidget->setCurrentIndex(2);
         return;
     }
-    renderParameters << scriptName;
 
     // Save rendering profile to document
     QMap <QString, QString> renderProps;
@@ -863,6 +876,7 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
     renderProps.insert("rendertctype", QString::number(m_view.tc_type->currentIndex()));
     renderProps.insert("renderratio", QString::number(m_view.rescale_keep->isChecked()));
     renderProps.insert("renderplay", QString::number(m_view.play_after->isChecked()));
+    renderProps.insert("rendertwopass", QString::number(m_view.checkTwoPass->isChecked()));
 
     emit selectedRenderProfile(renderProps);
 
@@ -889,9 +903,9 @@ void RenderWidget::slotExport(bool scriptExport, int zoneIn, int zoneOut, const
     if (group == "dvd") {
         if (m_view.open_dvd->isChecked()) {
             renderItem->setData(0, Qt::UserRole, group);
-            if (renderArgs.contains("profile=")) {
+            if (renderArgs.contains("mlt_profile=")) {
                 // rendering profile contains an MLT profile, so pass it to the running jog item, useful for dvd
-                QString prof = renderArgs.section("profile=", 1, 1);
+                QString prof = renderArgs.section("mlt_profile=", 1, 1);
                 prof = prof.section(' ', 0, 0);
                 kDebug() << "// render profile: " << prof;
                 renderItem->setData(0, Qt::UserRole + 1, prof);
@@ -972,8 +986,10 @@ void RenderWidget::setProfile(MltVideoProfile profile)
 {
     m_profile = profile;
     //WARNING: this way to tell the video standard is a bit hackish...
-    if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive)) m_view.format_selection->setCurrentIndex(0);
-    else m_view.format_selection->setCurrentIndex(1);
+    if (m_profile.description.contains("pal", Qt::CaseInsensitive) || m_profile.description.contains("25", Qt::CaseInsensitive) || m_profile.description.contains("50", Qt::CaseInsensitive))
+        m_isPal = true;
+    else
+        m_isPal = false;
     m_view.scanning_list->setCurrentIndex(0);
     m_view.rescale_width->setValue(KdenliveSettings::defaultrescalewidth());
     if (!m_view.rescale_keep->isChecked()) {
@@ -1053,6 +1069,7 @@ void RenderWidget::refreshView()
     if (m_view.destination_list->currentIndex() > 0)
         destination = m_view.destination_list->itemData(m_view.destination_list->currentIndex()).toString();
     KIcon brokenIcon("dialog-close");
+    KIcon warningIcon("dialog-warning");
 
     const QStringList formatsList = KdenliveSettings::supportedformats();
     const QStringList vcodecsList = KdenliveSettings::videocodecs();
@@ -1062,23 +1079,37 @@ void RenderWidget::refreshView()
     const QColor disabled = scheme.foreground(KColorScheme::InactiveText).color();
     const QColor disabledbg = scheme.background(KColorScheme::NegativeBackground).color();
 
-
+    double project_framerate = (double) m_profile.frame_rate_num / m_profile.frame_rate_den;
     for (int i = 0; i < m_renderItems.count(); i++) {
         sizeItem = m_renderItems.at(i);
         QListWidgetItem *dupItem = NULL;
         if ((sizeItem->data(GroupRole).toString() == group || sizeItem->data(GroupRole).toString().isEmpty()) && sizeItem->data(MetaGroupRole).toString() == destination) {
             std = sizeItem->data(StandardRole).toString();
             if (!std.isEmpty()) {
-                if (std.contains("PAL", Qt::CaseInsensitive) && m_view.format_selection->currentIndex() == 0) dupItem = sizeItem->clone();
-                else if (std.contains("NTSC", Qt::CaseInsensitive) && m_view.format_selection->currentIndex() == 1)  dupItem = sizeItem->clone();
+                if (std.contains("PAL", Qt::CaseInsensitive) && m_isPal) dupItem = sizeItem->clone();
+                else if (std.contains("NTSC", Qt::CaseInsensitive) && !m_isPal)  dupItem = sizeItem->clone();
             } else {
                 dupItem = sizeItem->clone();
             }
 
             if (dupItem) {
                 m_view.size_list->addItem(dupItem);
-                // Make sure the selected profile uses an installed avformat codec / format
                 std = dupItem->data(ParamsRole).toString();
+                // Make sure the selected profile uses the same frame rate as project profile
+                if (std.contains("mlt_profile=")) {
+                    QString profile = std.section("mlt_profile=", 1, 1).section(' ', 0, 0);
+                    MltVideoProfile p = ProfilesDialog::getVideoProfile(profile);
+                    if (p.frame_rate_den > 0) {
+                        double profile_rate = (double) p.frame_rate_num / p.frame_rate_den;
+                        if ((int) (1000.0 * profile_rate) != (int) (1000.0 * project_framerate)) {
+                            dupItem->setToolTip(i18n("Frame rate (%1) not compatible with project profile (%2)", profile_rate, project_framerate));
+                            dupItem->setIcon(brokenIcon);
+                            dupItem->setForeground(disabled);
+                        }
+                    }
+                }
+                
+                // Make sure the selected profile uses an installed avformat codec / format
                 if (!formatsList.isEmpty()) {
                     QString format;
                     if (std.startsWith("f=")) format = std.section("f=", 1, 1);
@@ -1088,7 +1119,7 @@ void RenderWidget::refreshView()
                         if (!formatsList.contains(format)) {
                             kDebug() << "***** UNSUPPORTED F: " << format;
                             //sizeItem->setHidden(true);
-                            //sizeItem->setFlags(Qt::ItemIsSelectable);
+                            //sizeItem-item>setFlags(Qt::ItemIsSelectable);
                             dupItem->setToolTip(i18n("Unsupported video format: %1", format));
                             dupItem->setIcon(brokenIcon);
                             dupItem->setForeground(disabled);
@@ -1128,6 +1159,12 @@ void RenderWidget::refreshView()
                         }
                     }
                 }
+                if (std.contains(" profile=") || std.startsWith("profile=")) {
+                    // changed in MLT commit d8a3a5c9190646aae72048f71a39ee7446a3bd45
+                    // (http://www.mltframework.org/gitweb/mlt.git?p=mltframework.org/mlt.git;a=commit;h=d8a3a5c9190646aae72048f71a39ee7446a3bd45)
+                    dupItem->setToolTip(i18n("This render profile uses a 'profile' parameter.<br />Unless you know what you are doing you will probably have to change it to 'mlt_profile'."));
+                    dupItem->setIcon(warningIcon);
+                }
             }
         }
     }
@@ -1165,6 +1202,7 @@ void RenderWidget::refreshParams()
 {
     // Format not available (e.g. codec not installed); Disable start button
     QListWidgetItem *item = m_view.size_list->currentItem();
+    errorMessage(item->toolTip());
     if (!item || item->isHidden()) {
         m_view.advanced_params->clear();
         m_view.buttonRender->setEnabled(false);
@@ -1204,6 +1242,23 @@ void RenderWidget::refreshParams()
         m_view.buttonEdit->setEnabled(true);
     }
 
+    // setup comboBox with bitrates
+    m_view.comboBitrates->clear();
+    if (params.contains("bitrate")) {
+        m_view.comboBitrates->setEnabled(true);
+        if ( item->data(BitratesRole).canConvert(QVariant::StringList) && item->data(BitratesRole).toStringList().count()) {
+            QStringList bitrates = item->data(BitratesRole).toStringList();
+            foreach (QString bitrate, bitrates)
+                m_view.comboBitrates->addItem(bitrate);
+            if (item->data(DefaultBitrateRole).canConvert(QVariant::String))
+                m_view.comboBitrates->setCurrentIndex(bitrates.indexOf(item->data(DefaultBitrateRole).toString()));
+        }
+    } else {
+        m_view.comboBitrates->setEnabled(false);
+    }
+
+    m_view.checkTwoPass->setEnabled(params.contains("passes"));
+
     m_view.buttonRender->setEnabled(m_view.size_list->currentItem()->toolTip().isEmpty());
     m_view.buttonGenerateScript->setEnabled(m_view.size_list->currentItem()->toolTip().isEmpty());
 }
@@ -1407,6 +1462,7 @@ void RenderWidget::parseFile(QString exportFile, bool editable)
     QString renderer;
     QString params;
     QString standard;
+    QString bitrates, defaultBitrate;
     KIcon icon;
 
     while (!groups.item(i).isNull()) {
@@ -1453,6 +1509,8 @@ void RenderWidget::parseFile(QString exportFile, bool editable)
             profileElement = n.toElement();
             profileName = profileElement.attribute("name");
             standard = profileElement.attribute("standard");
+            bitrates = profileElement.attribute("bitrates");
+            defaultBitrate = profileElement.attribute("defaultbitrate");
             params = profileElement.attribute("args");
 
             if (replaceVorbisCodec && params.contains("acodec=vorbis")) {
@@ -1473,6 +1531,8 @@ void RenderWidget::parseFile(QString exportFile, bool editable)
             item->setData(RenderRole, renderer);
             item->setData(StandardRole, standard);
             item->setData(ParamsRole, params);
+            item->setData(BitratesRole, bitrates.split(',', QString::SkipEmptyParts));
+            item->setData(DefaultBitrateRole, defaultBitrate);
             if (profileElement.hasAttribute("url")) item->setData(ExtraRole, profileElement.attribute("url"));
             if (editable) item->setData(EditableRole, exportFile);
             m_renderItems.append(item);
@@ -1635,14 +1695,14 @@ void RenderWidget::parseScriptFiles()
             while (!stream.atEnd()) {
                 QString line = stream.readLine();
                 if (line.startsWith("TARGET=")) {
-                    target = line.section("TARGET=", 1).simplified();
-                    target.remove(QChar('"'));
-                } else if (line.startsWith("RENDERER=")) {
-                    renderer = line.section("RENDERER=", 1).simplified();
-                    renderer.remove(QChar('"'));
-                } else if (line.startsWith("MELT=")) {
-                    melt = line.section("MELT=", 1).simplified();
-                    melt.remove(QChar('"'));
+                    target = line.section("TARGET=\"", 1);
+                    target = target.section('"', 0, 0);
+                } else if (line.startsWith("RENDERER=\"")) {
+                    renderer = line.section("RENDERER=", 1);
+                    renderer = renderer.section('"', 0, 0);
+                } else if (line.startsWith("MELT=\"")) {
+                    melt = line.section("MELT=", 1);
+                    melt = melt.section('"', 0, 0);
                 }
             }
             file.close();
@@ -1659,15 +1719,15 @@ void RenderWidget::parseScriptFiles()
             item->setData(0, Qt::UserRole, '1');
         } else item->setIcon(0, KIcon("application-x-executable-script"));
         item->setSizeHint(0, QSize(m_view.scripts_list->columnWidth(0), fontMetrics().height() * 2));
-        item->setData(1, Qt::UserRole, target.simplified());
+        item->setData(1, Qt::UserRole, KUrl(target).path());
         item->setData(1, Qt::UserRole + 1, scriptpath.path());
     }
-    bool activate = false;
+//     bool activate = false;
     QTreeWidgetItem *script = m_view.scripts_list->topLevelItem(0);
     if (script) {
         m_view.scripts_list->setCurrentItem(script);
         script->setSelected(true);
-        activate = true;
+//         activate = true;
     }
 //    m_view.start_script->setEnabled(activate);
 //    m_view.delete_script->setEnabled(activate);
@@ -1762,6 +1822,7 @@ void RenderWidget::setRenderProfile(QMap <QString, QString> props)
     if (props.contains("rendertctype")) m_view.tc_type->setCurrentIndex(props.value("rendertctype").toInt());
     if (props.contains("renderratio")) m_view.rescale_keep->setChecked(props.value("renderratio").toInt());
     if (props.contains("renderplay")) m_view.play_after->setChecked(props.value("renderplay").toInt());
+    if (props.contains("rendertwopass")) m_view.checkTwoPass->setChecked(props.value("rendertwopass").toInt());
 
     if (props.value("renderzone") == "1") m_view.render_zone->setChecked(true);
     else if (props.value("renderguide") == "1") {
@@ -1876,6 +1937,19 @@ void RenderWidget::missingClips(bool hasMissing)
     } else m_view.errorBox->setHidden(true);
 }
 
+void RenderWidget::errorMessage(const QString &message)
+{
+    if (!message.isEmpty()) {
+        m_view.errorLabel->setText(message);
+        m_view.errorBox->setHidden(false);
+    }
+    else {
+        m_view.errorBox->setHidden(true);
+        m_view.errorLabel->setText(QString());
+    }
+}
+
+
 void RenderWidget::slotUpdateEncodeThreads(int val)
 {
        KdenliveSettings::setEncodethreads(val);