kdenlive_render.cpp 8.2 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 "../src/lib/localeHandling.h"
21
#include "mlt++/Mlt.h"
Nicolas Carion's avatar
Nicolas Carion committed
22
#include "renderjob.h"
23
#include <QApplication>
24 25
#include <QDir>
#include <QDomDocument>
26

27 28
int main(int argc, char **argv)
{
29
    QApplication app(argc, argv);
30
    QStringList args = app.arguments();
31
    QStringList preargs;
32 33 34
    if (args.count() >= 4) {
        // Remove program name
        args.removeFirst();
35
        // renderer path (melt)
36 37
        QString render = args.at(0);
        args.removeFirst();
38
        // Source playlist path
39 40
        QString playlist = args.at(0);
        args.removeFirst();
41
        // target - where to save result
42 43 44
        QString target = args.at(0);
        args.removeFirst();
        int pid = 0;
45
        // pid to send back progress
46 47 48 49
        if (args.count() > 0 && args.at(0).startsWith(QLatin1String("-pid:"))) {
            pid = args.at(0).section(QLatin1Char(':'), 1).toInt();
            args.removeFirst();
        }
50 51 52 53 54 55 56 57 58
        int in = -1;
        int out = -1;
        
        if (args.count() > 1 && args.at(0) == QLatin1String("-out")) {
            args.removeFirst();
            out = args.at(0).toInt();
            args.removeFirst();
        }

59 60 61 62
        // 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
63
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
64
            QStringList chunks = args.at(0).split(QLatin1Char(','), QString::SkipEmptyParts);
Laurent Montel's avatar
Laurent Montel committed
65 66 67
#else
            QStringList chunks = args.at(0).split(QLatin1Char(','), Qt::SkipEmptyParts);
#endif
68 69 70 71
            args.removeFirst();
            // chunk size in frames
            int chunkSize = args.at(0).toInt();
            args.removeFirst();
72 73 74
            // chunk size in frames
            QString profilePath = args.at(0);
            args.removeFirst();
75 76 77 78
            // rendered file extension
            QString extension = args.at(0);
            args.removeFirst();
            // avformat consumer params
Laurent Montel's avatar
Laurent Montel committed
79
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
80
            QStringList consumerParams = args.at(0).split(QLatin1Char(' '), QString::SkipEmptyParts);
Laurent Montel's avatar
Laurent Montel committed
81 82 83
#else
            QStringList consumerParams = args.at(0).split(QLatin1Char(' '), Qt::SkipEmptyParts);
#endif
84 85
            args.removeFirst();
            QDir baseFolder(target);
86 87 88

            // After initialising the MLT factory, set the locale back from user default to C
            // to ensure numbers are always serialised with . as decimal point.
89
            Mlt::Factory::init();
90
            LocaleHandling::resetAllLocale();
91

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

Yuri Chornoivan's avatar
Yuri Chornoivan committed
135
        // older MLT version, does not support embedded consumer in/out in xml, and current 
136 137 138 139 140 141 142 143
        // 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
144
                // Workaround MLT embedded consumer resize (MLT issue #453)
145 146
                playlist.prepend(QStringLiteral("xml:"));
                playlist.append(QStringLiteral("?multi=1"));
147 148
            }
        }
149
        LocaleHandling::resetAllLocale();
150
        auto *rJob = new RenderJob(render, playlist, target, pid, in, out, qApp);
151
        rJob->start();
152
        QObject::connect(rJob, &RenderJob::renderingFinished, rJob, [&, rJob]() {
153 154
            rJob->deleteLater();
            app.quit();
155
        });
156
        return app.exec();
157
    } else {
Nicolas Carion's avatar
Nicolas Carion committed
158 159 160 161
        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"
162
                "  -erase: if that parameter is present, src file will be erased at the end\n"
163
                "  -kuiserver: if that parameter is present, use KDE job tracker\n"
164
                "  -locale:LOCALE : set a locale for rendering. For example, -locale:fr_FR.UTF-8 will use a french locale (comma as numeric separator)\n"
165 166
                "  in=pos: start rendering at frame pos\n"
                "  out=pos: end rendering at frame pos\n"
167
                "  render: path to MLT melt renderer\n"
168
                "  profile: the MLT video profile\n"
169
                "  rendermodule: the MLT consumer used for rendering, usually it is avformat\n"
170
                "  player: path to video player to play when rendering is over, use '-' to disable playing\n"
171 172 173
                "  src: source file (usually MLT XML)\n"
                "  dest: destination file\n"
                "  args: space separated libavformat arguments\n");
174
        return 1;
175
    }
176
}