]> git.sesse.net Git - kdenlive/blob - src/projecttree/cutclipjob.cpp
Some improvements in clip jobs
[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     description = i18n("clip cut");
32     m_dest = parameters.at(0);
33     m_src = parameters.at(1);
34     m_start = parameters.at(2);
35     m_end = parameters.at(3);
36     m_jobDuration = parameters.at(4).toInt();
37     addClipToProject = parameters.at(5).toInt();
38     if (parameters.count() == 7) m_cutExtraParams = parameters.at(6).simplified();
39 }
40
41 QProcess *CutClipJob::startJob(bool *ok)
42 {
43     // Special case: playlist clips (.mlt or .kdenlive project files)
44     if (clipType == AV || clipType == AUDIO || clipType == VIDEO) {
45         QStringList parameters;
46         parameters << "-i" << m_src;
47         parameters << "-ss" << m_start <<"-t" << m_end;
48         parameters << "-acodec" << "copy" << "-vcodec" << "copy";
49         if (!m_cutExtraParams.isEmpty()) {
50             foreach(const QString &s, m_cutExtraParams.split(' '))
51                 parameters << s;
52         }
53
54         // Make sure we don't block when proxy file already exists
55         parameters << "-y";
56         parameters << m_dest;
57         m_jobProcess = new QProcess;
58         m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
59         kDebug()<<"// STARTING CIUT JOS: "<<parameters;
60         m_jobProcess->start("ffmpeg", parameters);
61         m_jobProcess->waitForStarted();
62         QString log = m_jobProcess->readAll();
63         if (!log.isEmpty()) m_errorMessage.append(log + '\n');
64         return m_jobProcess;
65     }
66     else m_errorMessage = i18n("Cannot process this clip type.");
67     *ok = false;
68     return NULL;
69 }
70
71 int CutClipJob::processLogInfo()
72 {
73     if (!m_jobProcess || m_jobDuration == 0 || jobStatus == JOBABORTED) return JOBABORTED;
74     QString log = m_jobProcess->readAll();
75     if (!log.isEmpty()) m_errorMessage.append(log + '\n');
76     int progress;
77     // Parse FFmpeg output
78     if (log.contains("frame=")) {
79         int progress = log.section("frame=", 1, 1).simplified().section(' ', 0, 0).toInt();
80         kDebug()<<"// PROgress: "<<progress<<", DUR: "<<m_jobDuration;
81         return (int) (100.0 * progress / m_jobDuration);
82     }
83     else if (log.contains("time=")) {
84         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
85         if (time.contains(':')) {
86             QStringList numbers = time.split(':');
87             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
88         }
89         else progress = (int) time.toDouble();
90         kDebug()<<"// PROgress: "<<progress<<", DUR: "<<m_jobDuration;
91         return (int) (100.0 * progress / m_jobDuration);
92     }
93     return -1;
94 }
95
96 CutClipJob::~CutClipJob()
97 {
98 }
99
100 const QString CutClipJob::destination() const
101 {
102     return m_dest;
103 }
104
105 stringMap CutClipJob::cancelProperties()
106 {
107     QMap <QString, QString> props;
108     return props;
109 }
110
111 const QString CutClipJob::statusMessage()
112 {
113     QString statusInfo;
114     switch (jobStatus) {
115         case JOBWORKING:
116             statusInfo = i18n("Extracting clip cut");
117             break;
118         case JOBWAITING:
119             statusInfo = i18n("Waiting - clip cut");
120             break;
121         default:
122             break;
123     }
124     return statusInfo;
125 }
126