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 <QtConcurrentRun>
31 #include <KFileDialog>
34 ClipStabilize::ClipStabilize(KUrl::List urls, const QString ¶ms, QWidget * parent) :
35 QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL)
37 setFont(KGlobalSettings::toolBarFont());
39 setAttribute(Qt::WA_DeleteOnClose);
40 log_text->setHidden(true);
41 setWindowTitle(i18n("Stabilize Clip"));
42 auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
43 m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
46 if (m_urls.count() == 1) {
47 QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
48 QString newFile = fileName.append(".mlt");
50 source_url->setUrl(m_urls.at(0));
51 dest_url->setMode(KFile::File);
52 dest_url->setUrl(dest);
53 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
54 urls_list->setHidden(true);
55 connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
57 label_source->setHidden(true);
58 source_url->setHidden(true);
59 label_dest->setText(i18n("Destination folder"));
60 dest_url->setMode(KFile::Directory);
61 dest_url->setUrl(KUrl(m_urls.at(0).directory()));
62 dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
63 for (int i = 0; i < m_urls.count(); i++)
64 urls_list->addItem(m_urls.at(i).path());
66 if (!params.isEmpty()) {
67 label_profile->setHidden(true);
68 profile_list->setHidden(true);
69 //ffmpeg_params->setPlainText(params.simplified());
70 /*if (!description.isEmpty()) {
71 transcode_info->setText(description);
72 } else transcode_info->setHidden(true);*/
75 connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
76 connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
78 m_timer=new QTimer(this);
79 connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
84 ClipStabilize::~ClipStabilize()
86 /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
87 m_stabilizeProcess.close();
89 if (m_profile) free (m_profile);
90 if (m_consumer) free (m_consumer);
91 if (m_playlist) free (m_playlist);
94 void ClipStabilize::slotStartStabilize()
96 if (m_consumer && !m_consumer->is_stopped()) {
100 QStringList parameters;
102 QString params = ffmpeg_params->toPlainText().simplified();
103 if (urls_list->count() > 0) {
104 source_url->setUrl(m_urls.takeFirst());
105 destination = dest_url->url().path();
106 QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
107 if (matching.count() > 0) {
108 matching.at(0)->setFlags(Qt::ItemIsSelectable);
109 urls_list->setCurrentItem(matching.at(0));
112 destination = dest_url->url().path();
114 QString s_url = source_url->url().path();
116 if (QFile::exists(destination)) {
117 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination )) == KMessageBox::No) return;
119 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
122 qDebug() << m_profile->description();
123 m_playlist= new Mlt::Playlist;
124 Mlt::Filter filter(*m_profile,filtername.toUtf8().data());
125 Mlt::Producer p(*m_profile,s_url.toUtf8().data());
126 m_playlist->append(p);
127 m_playlist->attach(filter);
128 //m_producer->set("out",20);
129 m_consumer= new Mlt::Consumer(*m_profile,"xml",destination.toUtf8().data());
130 m_consumer->set("all","1");
131 m_consumer->set("real_time","-2");
132 m_consumer->connect(*m_playlist);
133 QtConcurrent::run(this, &ClipStabilize::slotRunStabilize);
134 button_start->setEnabled(false);
139 void ClipStabilize::slotRunStabilize()
148 void ClipStabilize::slotAbortStabilize()
154 slotStabilizeFinished(false);
158 void ClipStabilize::slotShowStabilizeInfo()
161 job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
162 if (m_consumer->position()==m_playlist->get_out()){
164 slotStabilizeFinished(true);
169 void ClipStabilize::slotStabilizeFinished(bool success)
171 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
172 button_start->setEnabled(true);
176 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
177 if (auto_add->isChecked()) {
179 if (urls_list->count() > 0) {
180 url = KUrl(dest_url->url());
181 } else url = dest_url->url();
184 if (urls_list->count() > 0 && m_urls.count() > 0) {
185 slotStartStabilize();
187 } else if (auto_close->isChecked()) accept();
189 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
195 #include "clipstabilize.moc"