]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
copy from cliptranscode to clipstabilize
[kdenlive] / src / clipstabilize.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *   Copyright (C) 2011 by Marco Gittler (marco@gitma.de)                  *
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
22 #include "clipstabilize.h"
23
24 #include <KDebug>
25 #include <mlt++/Mlt.h>
26 #include "kdenlivesettings.h"
27 #include <KGlobalSettings>
28 #include <KMessageBox>
29 #include <KFileDialog>
30
31
32 ClipStabilize::ClipStabilize(KUrl::List urls, const QString &params, QWidget * parent) :
33         QDialog(parent), m_urls(urls), m_duration(0)
34 {
35     setFont(KGlobalSettings::toolBarFont());
36     setupUi(this);
37     setAttribute(Qt::WA_DeleteOnClose);
38     log_text->setHidden(true);
39     setWindowTitle(i18n("Stabilize Clip"));
40     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
41         profile=Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
42         filtername=params;
43
44     if (m_urls.count() == 1) {
45         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
46         QString newFile = params.section(' ', -1).replace("%1", fileName);
47         KUrl dest(newFile);
48         source_url->setUrl(m_urls.at(0));
49         dest_url->setMode(KFile::File);
50         dest_url->setUrl(dest);
51         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
52         urls_list->setHidden(true);
53         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
54     } else {
55         label_source->setHidden(true);
56         source_url->setHidden(true);
57         label_dest->setText(i18n("Destination folder"));
58         dest_url->setMode(KFile::Directory);
59         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
60         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
61         for (int i = 0; i < m_urls.count(); i++)
62             urls_list->addItem(m_urls.at(i).path());
63     }
64     if (!params.isEmpty()) {
65         label_profile->setHidden(true);
66         profile_list->setHidden(true);
67         ffmpeg_params->setPlainText(params.simplified());
68         /*if (!description.isEmpty()) {
69             transcode_info->setText(description);
70         } else transcode_info->setHidden(true);*/
71     } 
72
73     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
74
75     m_stabilizeProcess.setProcessChannelMode(QProcess::MergedChannels);
76     connect(&m_stabilizeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowStabilizeInfo()));
77     connect(&m_stabilizeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotStabilizeFinished(int, QProcess::ExitStatus)));
78
79     adjustSize();
80 }
81
82 ClipStabilize::~ClipStabilize()
83 {
84     if (m_stabilizeProcess.state() != QProcess::NotRunning) {
85         m_stabilizeProcess.close();
86     }
87 }
88
89 void ClipStabilize::slotStartStabilize()
90 {
91     if (m_stabilizeProcess.state() != QProcess::NotRunning) {
92         return;
93     }
94     m_duration = 0;
95     QStringList parameters;
96     QString destination;
97     QString params = ffmpeg_params->toPlainText().simplified();
98     if (urls_list->count() > 0) {
99         source_url->setUrl(m_urls.takeFirst());
100         destination = dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName();
101         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
102         if (matching.count() > 0) {
103             matching.at(0)->setFlags(Qt::ItemIsSelectable);
104             urls_list->setCurrentItem(matching.at(0));
105         }
106     } else {
107         destination = dest_url->url().path().section('.', 0, -2);
108     }
109     QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
110     QString s_url = source_url->url().path();
111
112     parameters << "-i" << s_url;
113     if (QFile::exists(destination + extension)) {
114         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension)) == KMessageBox::No) return;
115         parameters << "-y";
116     }
117     foreach(QString s, params.split(' '))
118     parameters << s.replace("%1", destination);
119     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
120
121     //kDebug() << "/// FFMPEG ARGS: " << parameters;
122
123         Mlt::Producer producer(profile,s_url.toUtf8().data());
124         Mlt::Filter filter(profile,filtername.toUtf8().data());
125         producer.attach(filter);
126         Mlt::Consumer xmlout(profile,"xml");
127         xmlout.connect(producer);
128         xmlout.run();
129     //m_stabilizeProcess.start("ffmpeg", parameters);
130     button_start->setEnabled(false);
131
132 }
133
134 void ClipStabilize::slotShowStabilizeInfo()
135 {
136     QString log = QString(m_stabilizeProcess.readAll());
137     int progress;
138     if (m_duration == 0) {
139         if (log.contains("Duration:")) {
140             QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
141             QStringList numbers = data.split(':');
142             m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
143             log_text->setHidden(true);
144             job_progress->setHidden(false);
145         }
146         else {
147             log_text->setHidden(false);
148             job_progress->setHidden(true);
149         }
150     }
151     else if (log.contains("time=")) {
152         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
153         if (time.contains(':')) {
154             QStringList numbers = time.split(':');
155             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
156         }
157         else progress = (int) time.toDouble();
158         kDebug()<<"// PROGRESS: "<<progress<<", "<<m_duration;
159         job_progress->setValue((int) (100.0 * progress / m_duration));
160     }
161     //kDebug() << "//LOG: " << log;
162     log_text->setPlainText(log);
163 }
164
165 void ClipStabilize::slotStabilizeFinished(int exitCode, QProcess::ExitStatus exitStatus)
166 {
167     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
168     button_start->setEnabled(true);
169     m_duration = 0;
170
171     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
172         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
173         if (auto_add->isChecked()) {
174             KUrl url;
175             if (urls_list->count() > 0) {
176                 QString params = ffmpeg_params->toPlainText().simplified();
177                 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
178                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName() + extension);
179             } else url = dest_url->url();
180             emit addClip(url);
181         }
182         if (urls_list->count() > 0 && m_urls.count() > 0) {
183             m_stabilizeProcess.close();
184             slotStartStabilize();
185             return;
186         } else if (auto_close->isChecked()) accept();
187     } else {
188         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding FAILED!"));
189     }
190
191     m_stabilizeProcess.close();
192 }
193
194 void ClipStabilize::slotUpdateParams(int ix)
195 {
196     QString fileName = source_url->url().path();
197     if (ix != -1) {
198         QString params = profile_list->itemData(ix).toString();
199         ffmpeg_params->setPlainText(params.simplified());
200         QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
201         if (!desc.isEmpty()) {
202             transcode_info->setText(desc);
203             transcode_info->setHidden(false);
204         } else transcode_info->setHidden(true);
205     }
206     if (urls_list->count() == 0) {
207         QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
208         dest_url->setUrl(KUrl(newFile));
209     }
210
211 }
212
213 #include "clipstabilize.moc"
214
215