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 << "-consumer" << "avformat:" + m_dest;
56 QStringList params = m_proxyParams.split('-', QString::SkipEmptyParts);
58 foreach(const QString &s, params) {
59 QString t = s.simplified();
60 if (t.count(' ') == 0) {
63 else t.replace(' ', '=');
67 mltParameters.append(QString("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("consumer:")) display_ratio = KdenliveDoc::getDisplayRatio(m_src.section(":", 1));
72 else display_ratio = KdenliveDoc::getDisplayRatio(m_src);
73 mltParameters << "aspect=" + QLocale().toString(display_ratio);
75 // Ask for progress reporting
76 mltParameters << "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 << "-i" << m_src;
139 QString params = m_proxyParams;
140 foreach(const QString &s, params.split(' '))
143 // Make sure we don't block when proxy file already exists
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 = m_jobProcess->readAll();
189 m_logDetails.append(log + '\n');
195 // Parse FFmpeg output
196 if (m_jobDuration == 0) {
197 if (log.contains("Duration:")) {
198 QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
199 QStringList numbers = data.split(':');
200 m_jobDuration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
203 else if (log.contains("time=")) {
204 QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
205 if (time.contains(':')) {
206 QStringList numbers = time.split(':');
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("percentage:")) {
216 progress = log.section("percentage:", 1).simplified().section(' ', 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("proxy", "-");
238 const QString ProxyJob::statusMessage()
241 switch (m_jobStatus) {
243 statusInfo = i18n("Creating proxy");
246 statusInfo = i18n("Waiting - proxy");
255 #include "proxyclipjob.moc"