]> git.sesse.net Git - kdenlive/blob - src/projecttree/cutclipjob.cpp
b13f5bae3bbbbcea0f84c3c1420fd8cb97ee85b2
[kdenlive] / src / projecttree / cutclipjob.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 "cutclipjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
24
25 #include <KDebug>
26 #include <KLocale>
27
28 CutClipJob::CutClipJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(CUTJOB, cType, id, parameters)
29 {
30     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");
38     } else {
39         description = i18n("Cut clip");
40     }
41     m_jobDuration = parameters.at(4).toInt();
42     m_addClipToProject = parameters.at(5).toInt();
43     replaceClip = false;
44     if (parameters.count() == 7) m_cutExtraParams = parameters.at(6).simplified();
45 }
46
47 void CutClipJob::startJob()
48 {
49     // Special case: playlist clips (.mlt or .kdenlive project files)
50     if (clipType == AV || clipType == AUDIO || clipType == VIDEO) {
51         QStringList parameters;
52         parameters << "-i" << m_src;
53         if (!m_start.isEmpty())
54             parameters << "-ss" << m_start <<"-t" << m_end;
55         if (!m_cutExtraParams.isEmpty()) {
56             foreach(const QString &s, m_cutExtraParams.split(' '))
57                 parameters << s;
58         }
59
60         // Make sure we don't block when proxy file already exists
61         parameters << "-y";
62         parameters << m_dest;
63         m_jobProcess = new QProcess;
64         m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
65         // kDebug()<<"// STARTING CUT JOB: "<<parameters;
66         m_jobProcess->start("ffmpeg", parameters);
67         m_jobProcess->waitForStarted();
68         while (m_jobProcess->state() != QProcess::NotRunning) {
69             processLogInfo();
70             if (jobStatus == JOBABORTED) {
71                 m_jobProcess->close();
72                 m_jobProcess->waitForFinished();
73                 QFile::remove(m_dest);
74             }
75             m_jobProcess->waitForFinished(400);
76         }
77         
78         if (jobStatus != JOBABORTED) {
79             int result = m_jobProcess->exitStatus();
80             if (result == QProcess::NormalExit) {
81                 if (QFileInfo(m_dest).size() == 0) {
82                     // File was not created
83                     processLogInfo();
84                     m_errorMessage.append(i18n("Failed to create file."));
85                     setStatus(JOBCRASHED);
86                 } else {
87                     setStatus(JOBDONE);
88                 }
89             } else if (result == QProcess::CrashExit) {
90                 // Proxy process crashed
91                 QFile::remove(m_dest);
92                 setStatus(JOBCRASHED);
93             }
94         }
95         delete m_jobProcess;
96         return;
97     } else {
98         m_errorMessage = i18n("Cannot process this clip type.");
99     }
100     setStatus(JOBCRASHED);
101     return;
102 }
103
104 void CutClipJob::processLogInfo()
105 {
106     if (!m_jobProcess || m_jobDuration == 0 || jobStatus == JOBABORTED) return;
107     QString log = m_jobProcess->readAll();
108     if (!log.isEmpty()) m_logDetails.append(log + '\n');
109     int progress;
110     // Parse FFmpeg output
111     if (log.contains("frame=")) {
112         progress = log.section("frame=", 1, 1).simplified().section(' ', 0, 0).toInt();
113         emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
114     }
115     else if (log.contains("time=")) {
116         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
117         if (time.contains(':')) {
118             QStringList numbers = time.split(':');
119             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
120         }
121         else progress = (int) time.toDouble();
122         emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
123     }
124 }
125
126 CutClipJob::~CutClipJob()
127 {
128 }
129
130 const QString CutClipJob::destination() const
131 {
132     return m_dest;
133 }
134
135 stringMap CutClipJob::cancelProperties()
136 {
137     QMap <QString, QString> props;
138     return props;
139 }
140
141 const QString CutClipJob::statusMessage()
142 {
143     QString statusInfo;
144     switch (jobStatus) {
145         case JOBWORKING:
146             if (m_start.isEmpty()) statusInfo = i18n("Transcoding clip");
147             else statusInfo = i18n("Extracting clip cut");
148             break;
149         case JOBWAITING:
150             if (m_start.isEmpty()) statusInfo = i18n("Waiting - transcode clip");
151             else statusInfo = i18n("Waiting - cut clip");
152             break;
153         default:
154             break;
155     }
156     return statusInfo;
157 }
158
159 bool CutClipJob::isExclusive()
160 {
161     return false;
162 }
163