1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
3 * Copyright (C) 2011 by Marco Gittler (marco@gitma.de) *
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 ***************************************************************************/
22 #include "clipstabilize.h"
25 #include <mlt++/Mlt.h>
26 #include "kdenlivesettings.h"
27 #include <KGlobalSettings>
28 #include <KMessageBox>
29 #include <KFileDialog>
32 ClipStabilize::ClipStabilize(KUrl::List urls, const QString ¶ms, QWidget * parent) :
33 QDialog(parent), m_urls(urls), m_duration(0)
35 setFont(KGlobalSettings::toolBarFont());
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());
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);
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()));
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());
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);*/
73 connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
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)));
82 ClipStabilize::~ClipStabilize()
84 if (m_stabilizeProcess.state() != QProcess::NotRunning) {
85 m_stabilizeProcess.close();
89 void ClipStabilize::slotStartStabilize()
91 if (m_stabilizeProcess.state() != QProcess::NotRunning) {
95 QStringList parameters;
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));
107 destination = dest_url->url().path().section('.', 0, -2);
109 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
110 QString s_url = source_url->url().path();
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;
117 foreach(QString s, params.split(' '))
118 parameters << s.replace("%1", destination);
119 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
121 //kDebug() << "/// FFMPEG ARGS: " << parameters;
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);
129 //m_stabilizeProcess.start("ffmpeg", parameters);
130 button_start->setEnabled(false);
134 void ClipStabilize::slotShowStabilizeInfo()
136 QString log = QString(m_stabilizeProcess.readAll());
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);
147 log_text->setHidden(false);
148 job_progress->setHidden(true);
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();
157 else progress = (int) time.toDouble();
158 kDebug()<<"// PROGRESS: "<<progress<<", "<<m_duration;
159 job_progress->setValue((int) (100.0 * progress / m_duration));
161 //kDebug() << "//LOG: " << log;
162 log_text->setPlainText(log);
165 void ClipStabilize::slotStabilizeFinished(int exitCode, QProcess::ExitStatus exitStatus)
167 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
168 button_start->setEnabled(true);
171 if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
172 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
173 if (auto_add->isChecked()) {
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();
182 if (urls_list->count() > 0 && m_urls.count() > 0) {
183 m_stabilizeProcess.close();
184 slotStartStabilize();
186 } else if (auto_close->isChecked()) accept();
188 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding FAILED!"));
191 m_stabilizeProcess.close();
194 void ClipStabilize::slotUpdateParams(int ix)
196 QString fileName = source_url->url().path();
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);
206 if (urls_list->count() == 0) {
207 QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
208 dest_url->setUrl(KUrl(newFile));
213 #include "clipstabilize.moc"