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