]> git.sesse.net Git - kdenlive/blob - src/projecttree/proxyclipjob.cpp
Fix display of proxy clip progress when using MLT with debug enabled
[kdenlive] / src / projecttree / proxyclipjob.cpp
1 /***************************************************************************
2  *                                                                         *
3  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "proxyclipjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
24
25 #include <KDebug>
26 #include <KLocale>
27
28 ProxyJob::ProxyJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(PROXYJOB, cType, id, parameters),
29     m_jobDuration(0),
30     m_isFfmpegJob(true)
31 {
32     m_jobStatus = JOBWAITING;
33     description = i18n("proxy");
34     m_dest = parameters.at(0);
35     m_src = parameters.at(1);
36     m_exif = parameters.at(2).toInt();
37     m_proxyParams = parameters.at(3);
38     m_renderWidth = parameters.at(4).toInt();
39     m_renderHeight = parameters.at(5).toInt();
40     replaceClip = true;
41 }
42
43 void ProxyJob::startJob()
44 {
45     // Special case: playlist clips (.mlt or .kdenlive project files)
46     m_jobDuration = 0;
47     if (clipType == PLAYLIST) {
48         // change FFmpeg params to MLT format
49         m_isFfmpegJob = false;
50         QStringList mltParameters;
51         mltParameters << m_src;
52         mltParameters << "-consumer" << "avformat:" + m_dest;
53         QStringList params = m_proxyParams.split('-', QString::SkipEmptyParts);
54                 
55         foreach(const QString &s, params) {
56             QString t = s.simplified();
57             if (t.count(' ') == 0) {
58                 t.append("=1");
59             }
60             else t.replace(' ', '=');
61             mltParameters << t;
62         }
63         
64         mltParameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
65
66         //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
67         double display_ratio;
68         if (m_src.startsWith("consumer:")) display_ratio = KdenliveDoc::getDisplayRatio(m_src.section(":", 1));
69         else display_ratio = KdenliveDoc::getDisplayRatio(m_src);
70         mltParameters << "aspect=" + QString::number(display_ratio);
71             
72         // Ask for progress reporting
73         mltParameters << "progress=1";
74
75         m_jobProcess = new QProcess;
76         m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
77         m_jobProcess->start(KdenliveSettings::rendererpath(), mltParameters);
78         m_jobProcess->waitForStarted();
79     }
80     else if (clipType == IMAGE) {
81         m_isFfmpegJob = false;
82         // Image proxy
83         QImage i(m_src);
84         if (i.isNull()) {
85             m_errorMessage.append(i18n("Cannot load image %1.", m_src));
86             setStatus(JOBCRASHED);
87             return;
88         }
89         
90         QImage proxy;
91         // Images are scaled to profile size. 
92         //TODO: Make it be configurable?
93         if (i.width() > i.height()) proxy = i.scaledToWidth(m_renderWidth);
94         else proxy = i.scaledToHeight(m_renderHeight);
95         if (m_exif > 1) {
96             // Rotate image according to exif data
97             QImage processed;
98             QMatrix matrix;
99
100             switch ( m_exif ) {
101                 case 2:
102                     matrix.scale( -1, 1 );
103                     break;
104                 case 3:
105                     matrix.rotate( 180 );
106                     break;
107                 case 4:
108                     matrix.scale( 1, -1 );
109                     break;
110                 case 5:
111                     matrix.rotate( 270 );
112                     matrix.scale( -1, 1 );
113                     break;
114                 case 6:
115                     matrix.rotate( 90 );
116                     break;
117                 case 7:
118                     matrix.rotate( 90 );
119                     matrix.scale( -1, 1 );
120                     break;
121                 case 8:
122                     matrix.rotate( 270 );
123                     break;
124             }
125             processed = proxy.transformed( matrix );
126             processed.save(m_dest);
127         }
128         else proxy.save(m_dest);
129         setStatus(JOBDONE);
130         return;
131     }
132     else {
133         m_isFfmpegJob = true;
134         QStringList parameters;
135         parameters << "-i" << m_src;
136         QString params = m_proxyParams;
137         foreach(const QString &s, params.split(' '))
138             parameters << s;
139
140         // Make sure we don't block when proxy file already exists
141         parameters << "-y";
142         parameters << m_dest;
143         m_jobProcess = new QProcess;
144         m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
145         m_jobProcess->start(KdenliveSettings::ffmpegpath(), parameters, QIODevice::ReadOnly);
146         m_jobProcess->waitForStarted();
147     }
148     while (m_jobProcess->state() != QProcess::NotRunning) {
149         processLogInfo();
150         if (m_jobStatus == JOBABORTED) {
151             emit cancelRunningJob(m_clipId, cancelProperties());
152             m_jobProcess->close();
153             m_jobProcess->waitForFinished();
154             QFile::remove(m_dest);
155         }
156         m_jobProcess->waitForFinished(400);
157     }
158     
159     if (m_jobStatus != JOBABORTED) {
160         int result = m_jobProcess->exitStatus();
161         if (result == QProcess::NormalExit) {
162             if (QFileInfo(m_dest).size() == 0) {
163                 // File was not created
164                 processLogInfo();
165                 m_errorMessage.append(i18n("Failed to create proxy clip."));
166                 setStatus(JOBCRASHED);
167             }
168             else setStatus(JOBDONE);
169         }
170         else if (result == QProcess::CrashExit) {
171             // Proxy process crashed
172             QFile::remove(m_dest);
173             setStatus(JOBCRASHED);
174         }
175     }
176     
177     delete m_jobProcess;
178     return;
179 }
180
181 void ProxyJob::processLogInfo()
182 {
183     if (!m_jobProcess || m_jobStatus == JOBABORTED) return;
184     QString log = m_jobProcess->readAll();
185     if (!log.isEmpty()) m_logDetails.append(log + '\n');
186     else return;
187     int progress;
188     if (m_isFfmpegJob) {
189         // Parse FFmpeg output
190         if (m_jobDuration == 0) {
191             if (log.contains("Duration:")) {
192                 QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
193                 QStringList numbers = data.split(':');
194                 m_jobDuration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
195             }
196         }
197         else if (log.contains("time=")) {
198             QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
199             if (time.contains(':')) {
200                 QStringList numbers = time.split(':');
201                 progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
202             }
203             else progress = (int) time.toDouble();
204             emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
205         }
206     }
207     else {
208         // Parse MLT output
209         if (log.contains("percentage:")) {
210             progress = log.section("percentage:", 1).simplified().section(' ', 0, 0).toInt();
211             emit jobProgress(m_clipId, progress, jobType);
212         }
213     }
214 }
215
216 ProxyJob::~ProxyJob()
217 {
218 }
219
220 const QString ProxyJob::destination() const
221 {
222     return m_dest;
223 }
224
225 stringMap ProxyJob::cancelProperties()
226 {
227     QMap <QString, QString> props;
228     props.insert("proxy", "-");
229     return props;
230 }
231
232 const QString ProxyJob::statusMessage()
233 {
234     QString statusInfo;
235     switch (m_jobStatus) {
236         case JOBWORKING:
237             statusInfo = i18n("Creating proxy");
238             break;
239         case JOBWAITING:
240             statusInfo = i18n("Waiting - proxy");
241             break;
242         default:
243             break;
244     }
245     return statusInfo;
246 }
247