1 /***************************************************************************
3 * Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "proxyclipjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
28 #include <KLocalizedString>
30 ProxyJob::ProxyJob(ClipType cType, const QString &id, const QStringList& parameters)
31 : AbstractClipJob(PROXYJOB, cType, id, parameters),
35 m_jobStatus = JobWaiting;
36 description = i18n("proxy");
37 m_dest = parameters.at(0);
38 m_src = parameters.at(1);
39 m_exif = parameters.at(2).toInt();
40 m_proxyParams = parameters.at(3);
41 m_renderWidth = parameters.at(4).toInt();
42 m_renderHeight = parameters.at(5).toInt();
46 void ProxyJob::startJob()
48 // Special case: playlist clips (.mlt or .kdenlive project files)
50 if (clipType == Playlist) {
51 // change FFmpeg params to MLT format
52 m_isFfmpegJob = false;
53 QStringList mltParameters;
54 mltParameters << m_src;
55 mltParameters << QLatin1String("-consumer") << QLatin1String("avformat:") + m_dest;
56 QStringList params = m_proxyParams.split(QLatin1Char('-'), QString::SkipEmptyParts);
58 foreach(const QString &s, params) {
59 QString t = s.simplified();
60 if (t.count(QLatin1Char(' ')) == 0) {
61 t.append(QLatin1String("=1"));
63 else t.replace(QLatin1Char(' '), QLatin1String("="));
67 mltParameters.append(QString::fromLatin1("real_time=-%1").arg(KdenliveSettings::mltthreads()));
69 //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
71 if (m_src.startsWith(QLatin1String("consumer:"))) display_ratio = KdenliveDoc::getDisplayRatio(m_src.section(QLatin1String(":"), 1));
72 else display_ratio = KdenliveDoc::getDisplayRatio(m_src);
73 mltParameters << QLatin1String("aspect=") + QLocale().toString(display_ratio);
75 // Ask for progress reporting
76 mltParameters << QLatin1String("progress=1");
78 m_jobProcess = new QProcess;
79 m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
80 m_jobProcess->start(KdenliveSettings::rendererpath(), mltParameters);
81 m_jobProcess->waitForStarted();
83 else if (clipType == Image) {
84 m_isFfmpegJob = false;
88 m_errorMessage.append(i18n("Cannot load image %1.", m_src));
89 setStatus(JobCrashed);
94 // Images are scaled to profile size.
95 //TODO: Make it be configurable?
96 if (i.width() > i.height()) proxy = i.scaledToWidth(m_renderWidth);
97 else proxy = i.scaledToHeight(m_renderHeight);
99 // Rotate image according to exif data
105 matrix.scale( -1, 1 );
108 matrix.rotate( 180 );
111 matrix.scale( 1, -1 );
114 matrix.rotate( 270 );
115 matrix.scale( -1, 1 );
122 matrix.scale( -1, 1 );
125 matrix.rotate( 270 );
128 processed = proxy.transformed( matrix );
129 processed.save(m_dest);
136 m_isFfmpegJob = true;
137 QStringList parameters;
138 parameters << QLatin1String("-i") << m_src;
139 QString params = m_proxyParams;
140 foreach(const QString &s, params.split(QLatin1Char(' ')))
143 // Make sure we don't block when proxy file already exists
144 parameters << QLatin1String("-y");
145 parameters << m_dest;
146 m_jobProcess = new QProcess;
147 m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
148 m_jobProcess->start(KdenliveSettings::ffmpegpath(), parameters, QIODevice::ReadOnly);
149 m_jobProcess->waitForStarted();
151 while (m_jobProcess->state() != QProcess::NotRunning) {
153 if (m_jobStatus == JobAborted) {
154 emit cancelRunningJob(m_clipId, cancelProperties());
155 m_jobProcess->close();
156 m_jobProcess->waitForFinished();
157 QFile::remove(m_dest);
159 m_jobProcess->waitForFinished(400);
162 if (m_jobStatus != JobAborted) {
163 int result = m_jobProcess->exitStatus();
164 if (result == QProcess::NormalExit) {
165 if (QFileInfo(m_dest).size() == 0) {
166 // File was not created
168 m_errorMessage.append(i18n("Failed to create proxy clip."));
169 setStatus(JobCrashed);
171 else setStatus(JobDone);
173 else if (result == QProcess::CrashExit) {
174 // Proxy process crashed
175 QFile::remove(m_dest);
176 setStatus(JobCrashed);
184 void ProxyJob::processLogInfo()
186 if (!m_jobProcess || m_jobStatus == JobAborted) return;
187 QString log = QString::fromUtf8(m_jobProcess->readAll());
189 m_logDetails.append(log + QLatin1Char('\n'));
195 // Parse FFmpeg output
196 if (m_jobDuration == 0) {
197 if (log.contains(QLatin1String("Duration:"))) {
198 QString data = log.section(QLatin1String("Duration:"), 1, 1).section(QLatin1Char(','), 0, 0).simplified();
199 QStringList numbers = data.split(QLatin1Char(':'));
200 m_jobDuration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
203 else if (log.contains(QLatin1String("time="))) {
204 QString time = log.section(QLatin1String("time="), 1, 1).simplified().section(QLatin1Char(' '), 0, 0);
205 if (time.contains(QLatin1Char(':'))) {
206 QStringList numbers = time.split(QLatin1Char(':'));
207 progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
209 else progress = (int) time.toDouble();
210 emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
215 if (log.contains(QLatin1String("percentage:"))) {
216 progress = log.section(QLatin1String("percentage:"), 1).simplified().section(QLatin1Char(' '), 0, 0).toInt();
217 emit jobProgress(m_clipId, progress, jobType);
222 ProxyJob::~ProxyJob()
226 const QString ProxyJob::destination() const
231 stringMap ProxyJob::cancelProperties()
233 QMap <QString, QString> props;
234 props.insert(QLatin1String("proxy"), QLatin1String("-"));
238 const QString ProxyJob::statusMessage()
241 switch (m_jobStatus) {
243 statusInfo = i18n("Creating proxy");
246 statusInfo = i18n("Waiting - proxy");
255 #include "proxyclipjob.moc"