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 "cutclipjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
26 #include <KLocalizedString>
28 CutClipJob::CutClipJob(ClipType cType, const QString &id, const QStringList ¶meters) : AbstractClipJob(CUTJOB, cType, id, parameters)
30 m_jobStatus = JobWaiting;
31 m_dest = parameters.at(0);
32 m_src = parameters.at(1);
33 m_start = parameters.at(2);
34 m_end = parameters.at(3);
35 if (m_start.isEmpty()) {
36 // this is a transcoding job
37 description = i18n("Transcode clip");
39 description = i18n("Cut clip");
41 m_jobDuration = parameters.at(4).toInt();
42 m_addClipToProject = parameters.at(5).toInt();
44 if (parameters.count() == 7) m_cutExtraParams = parameters.at(6).simplified();
47 void CutClipJob::startJob()
49 // Special case: playlist clips (.mlt or .kdenlive project files)
50 if (clipType == AV || clipType == Audio || clipType == Video) {
51 QStringList parameters;
52 parameters << QLatin1String("-i") << m_src;
53 if (!m_start.isEmpty())
54 parameters << QLatin1String("-ss") << m_start <<QLatin1String("-t") << m_end;
55 if (!m_cutExtraParams.isEmpty()) {
56 foreach(const QString &s, m_cutExtraParams.split(QLatin1Char(' ')))
60 // Make sure we don't block when proxy file already exists
61 parameters << QLatin1String("-y");
63 m_jobProcess = new QProcess;
64 m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
65 m_jobProcess->start(KdenliveSettings::ffmpegpath(), parameters);
66 m_jobProcess->waitForStarted();
67 while (m_jobProcess->state() != QProcess::NotRunning) {
69 if (m_jobStatus == JobAborted) {
70 m_jobProcess->close();
71 m_jobProcess->waitForFinished();
72 QFile::remove(m_dest);
74 m_jobProcess->waitForFinished(400);
77 if (m_jobStatus != JobAborted) {
78 int result = m_jobProcess->exitStatus();
79 if (result == QProcess::NormalExit) {
80 if (QFileInfo(m_dest).size() == 0) {
81 // File was not created
83 m_errorMessage.append(i18n("Failed to create file."));
84 setStatus(JobCrashed);
88 } else if (result == QProcess::CrashExit) {
89 // Proxy process crashed
90 QFile::remove(m_dest);
91 setStatus(JobCrashed);
97 m_errorMessage = i18n("Cannot process this clip type.");
99 setStatus(JobCrashed);
103 void CutClipJob::processLogInfo()
105 if (!m_jobProcess || m_jobDuration == 0 || m_jobStatus == JobAborted) return;
106 QString log = QString::fromUtf8(m_jobProcess->readAll());
107 if (!log.isEmpty()) m_logDetails.append(log + QLatin1Char('\n'));
109 // Parse FFmpeg output
110 if (log.contains(QLatin1String("frame="))) {
111 progress = log.section(QLatin1String("frame="), 1, 1).simplified().section(QLatin1Char(' '), 0, 0).toInt();
112 emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
114 else if (log.contains(QLatin1String("time="))) {
115 QString time = log.section(QLatin1String("time="), 1, 1).simplified().section(QLatin1Char(' '), 0, 0);
116 if (time.contains(QLatin1Char(':'))) {
117 QStringList numbers = time.split(QLatin1Char(':'));
118 progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
120 else progress = (int) time.toDouble();
121 emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
125 CutClipJob::~CutClipJob()
129 const QString CutClipJob::destination() const
134 stringMap CutClipJob::cancelProperties()
136 QMap <QString, QString> props;
140 const QString CutClipJob::statusMessage()
143 switch (m_jobStatus) {
145 if (m_start.isEmpty()) statusInfo = i18n("Transcoding clip");
146 else statusInfo = i18n("Extracting clip cut");
149 if (m_start.isEmpty()) statusInfo = i18n("Waiting - transcode clip");
150 else statusInfo = i18n("Waiting - cut clip");
158 bool CutClipJob::isExclusive()
164 #include "cutclipjob.moc"