kdenlive_render.cpp 8.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/***************************************************************************
 *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/
19

20
#include "mlt++/Mlt.h"
Nicolas Carion's avatar
Nicolas Carion committed
21
#include "renderjob.h"
22
#include <QtGlobal>
23
#include <QApplication>
24 25
#include <QDir>
#include <QDomDocument>
26
#include <QString>
Nicolas Carion's avatar
Nicolas Carion committed
27
#include <QStringList>
28
#include <QObject>
Nicolas Carion's avatar
Nicolas Carion committed
29
#include <cstdio>
30

31 32
int main(int argc, char **argv)
{
33
    QApplication app(argc, argv);
34
    QStringList args = app.arguments();
35
    QStringList preargs;
36
    QString locale;
37 38 39
    if (args.count() >= 4) {
        // Remove program name
        args.removeFirst();
40
        // renderer path (melt)
41 42
        QString render = args.at(0);
        args.removeFirst();
43
        // Source playlist path
44 45
        QString playlist = args.at(0);
        args.removeFirst();
46
        // target - where to save result
47 48 49
        QString target = args.at(0);
        args.removeFirst();
        int pid = 0;
50
        // pid to send back progress
51 52 53 54
        if (args.count() > 0 && args.at(0).startsWith(QLatin1String("-pid:"))) {
            pid = args.at(0).section(QLatin1Char(':'), 1).toInt();
            args.removeFirst();
        }
55 56 57 58
        // Do we want a split render
        if (args.count() > 0 && args.at(0) == QLatin1String("-split")) {
            args.removeFirst();
            // chunks to render
Laurent Montel's avatar
Laurent Montel committed
59
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
60
            QStringList chunks = args.at(0).split(QLatin1Char(','), QString::SkipEmptyParts);
Laurent Montel's avatar
Laurent Montel committed
61 62 63
#else
            QStringList chunks = args.at(0).split(QLatin1Char(','), Qt::SkipEmptyParts);
#endif
64 65 66 67
            args.removeFirst();
            // chunk size in frames
            int chunkSize = args.at(0).toInt();
            args.removeFirst();
68 69 70
            // chunk size in frames
            QString profilePath = args.at(0);
            args.removeFirst();
71 72 73 74
            // rendered file extension
            QString extension = args.at(0);
            args.removeFirst();
            // avformat consumer params
Laurent Montel's avatar
Laurent Montel committed
75
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
76
            QStringList consumerParams = args.at(0).split(QLatin1Char(' '), QString::SkipEmptyParts);
Laurent Montel's avatar
Laurent Montel committed
77 78 79
#else
            QStringList consumerParams = args.at(0).split(QLatin1Char(' '), Qt::SkipEmptyParts);
#endif
80 81
            args.removeFirst();
            QDir baseFolder(target);
82 83 84

            // After initialising the MLT factory, set the locale back from user default to C
            // to ensure numbers are always serialised with . as decimal point.
85
            Mlt::Factory::init();
86 87 88
            std::setlocale(LC_ALL, "C");
            ::qputenv("LC_ALL", "C");

89 90
            Mlt::Profile profile(profilePath.toUtf8().constData());
            profile.set_explicit(1);
91 92 93
            Mlt::Producer prod(profile, nullptr, playlist.toUtf8().constData());
            if (!prod.is_valid()) {
                fprintf(stderr, "INVALID playlist: %s \n", playlist.toUtf8().constData());
94
                return 1;
95
            }
96 97
            const char *localename = prod.get_lcnumeric();
            QLocale::setDefault(QLocale(localename));
Nicolas Carion's avatar
Nicolas Carion committed
98
            for (const QString &frame : chunks) {
99 100 101 102 103 104 105 106
                fprintf(stderr, "START:%d \n", frame.toInt());
                QString fileName = QStringLiteral("%1.%2").arg(frame).arg(extension);
                if (baseFolder.exists(fileName)) {
                    // Don't overwrite an existing file
                    fprintf(stderr, "DONE:%d \n", frame.toInt());
                    continue;
                }
                QScopedPointer<Mlt::Producer> playlst(prod.cut(frame.toInt(), frame.toInt() + chunkSize));
107 108
                QScopedPointer<Mlt::Consumer> cons(
                    new Mlt::Consumer(profile, QString("avformat:%1").arg(baseFolder.absoluteFilePath(fileName)).toUtf8().constData()));
109 110
                for (const QString &param : consumerParams) {
                    if (param.contains(QLatin1Char('='))) {
111
                        cons->set(param.section(QLatin1Char('='), 0, 0).toUtf8().constData(), param.section(QLatin1Char('='), 1).toUtf8().constData());
112 113
                    }
                }
114 115
                if (!cons->is_valid()) {
                    fprintf(stderr, " = =  = INVALID CONSUMER\n\n");
116
                    return 1;
117
                }
118 119 120 121 122 123 124 125
                cons->set("terminate_on_pause", 1);
                cons->connect(*playlst);
                playlst.reset();
                cons->run();
                cons->stop();
                cons->purge();
                fprintf(stderr, "DONE:%d \n", frame.toInt());
            }
126
            // Mlt::Factory::close();
127 128 129
            fprintf(stderr, "+ + + RENDERING FINSHED + + + \n");
            return 0;
        }
130 131
        int in = -1;
        int out = -1;
132

Yuri Chornoivan's avatar
Yuri Chornoivan committed
133
        // older MLT version, does not support embedded consumer in/out in xml, and current 
134 135 136 137 138 139 140 141
        // MLT (6.16) does not pass it onto the multi / movit consumer, so read it manually and enforce
        QFile f(playlist);
        QDomDocument doc;
        doc.setContent(&f, false);
        f.close();
        QDomElement consumer = doc.documentElement().firstChildElement(QStringLiteral("consumer"));
        if (!consumer.isNull()) {
            if (consumer.hasAttribute(QLatin1String("s")) || consumer.hasAttribute(QLatin1String("r"))) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
142
                // Workaround MLT embedded consumer resize (MLT issue #453)
143 144
                playlist.prepend(QStringLiteral("xml:"));
                playlist.append(QStringLiteral("?multi=1"));
145 146
            }
        }
147

148
        auto *rJob = new RenderJob(render, playlist, target, pid, in, out, qApp);
149
        rJob->start();
150 151 152 153
        QObject::connect(rJob, &RenderJob::renderingFinished, [&, rJob]() {
            rJob->deleteLater();
            app.quit();
        }); 
154
        return app.exec();
155
    } else {
Nicolas Carion's avatar
Nicolas Carion committed
156 157 158 159
        fprintf(stderr,
                "Kdenlive video renderer for MLT.\nUsage: "
                "kdenlive_render [-erase] [-kuiserver] [-locale:LOCALE] [in=pos] [out=pos] [render] [profile] [rendermodule] [player] [src] [dest] [[arg1] "
                "[arg2] ...]\n"
160
                "  -erase: if that parameter is present, src file will be erased at the end\n"
161
                "  -kuiserver: if that parameter is present, use KDE job tracker\n"
162
                "  -locale:LOCALE : set a locale for rendering. For example, -locale:fr_FR.UTF-8 will use a french locale (comma as numeric separator)\n"
163 164
                "  in=pos: start rendering at frame pos\n"
                "  out=pos: end rendering at frame pos\n"
165
                "  render: path to MLT melt renderer\n"
166
                "  profile: the MLT video profile\n"
167
                "  rendermodule: the MLT consumer used for rendering, usually it is avformat\n"
168
                "  player: path to video player to play when rendering is over, use '-' to disable playing\n"
169 170 171
                "  src: source file (usually MLT XML)\n"
                "  dest: destination file\n"
                "  args: space separated libavformat arguments\n");
172
        return 1;
173
    }
174
}