]> git.sesse.net Git - kdenlive/blob - src/clipstabilize.cpp
stabilize with default params first
[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 <QtConcurrentRun>
30 #include <QTimer>
31 #include <KFileDialog>
32
33
34 ClipStabilize::ClipStabilize(KUrl::List urls, const QString &params, QWidget * parent) :
35         QDialog(parent), m_urls(urls), m_duration(0),m_profile(NULL),m_consumer(NULL),m_playlist(NULL)
36 {
37     setFont(KGlobalSettings::toolBarFont());
38     setupUi(this);
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());
44         filtername=params;
45
46     if (m_urls.count() == 1) {
47         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
48         QString newFile = fileName.append(".mlt");
49         KUrl dest(newFile);
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()));
56     } else {
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());
65     }
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);*/
73     } 
74
75     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartStabilize()));
76         connect(buttonBox,SIGNAL(rejected()), this, SLOT(slotAbortStabilize()));
77
78         m_timer=new QTimer(this);
79         connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShowStabilizeInfo()));
80
81     adjustSize();
82 }
83
84 ClipStabilize::~ClipStabilize()
85 {
86     /*if (m_stabilizeProcess.state() != QProcess::NotRunning) {
87         m_stabilizeProcess.close();
88     }*/
89         if (m_profile) free (m_profile);
90         if (m_consumer) free (m_consumer);
91         if (m_playlist) free (m_playlist);
92 }
93
94 void ClipStabilize::slotStartStabilize()
95 {
96     if (m_consumer && !m_consumer->is_stopped()) {
97         return;
98     }
99     m_duration = 0;
100     QStringList parameters;
101     QString destination;
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));
110         }
111     } else {
112         destination = dest_url->url().path();
113     }
114     QString s_url = source_url->url().path();
115
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;
118     }
119     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
120
121         if (m_profile){
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);
135         }
136
137 }
138
139 void ClipStabilize::slotRunStabilize()
140 {
141         if (m_consumer)
142         {
143                 m_timer->start(500);
144                 m_consumer->run();
145         }
146 }
147
148 void ClipStabilize::slotAbortStabilize()
149 {
150         if (m_consumer)
151         {
152                 m_timer->stop();
153                 m_consumer->stop();
154                 slotStabilizeFinished(false);
155         }
156 }
157
158 void ClipStabilize::slotShowStabilizeInfo()
159 {
160         if (m_playlist){
161         job_progress->setValue((int) (100.0 * m_consumer->position()/m_playlist->get_out() ));
162                 if (m_consumer->position()==m_playlist->get_out()){
163                         m_timer->stop();
164                         slotStabilizeFinished(true);
165                 }
166         }
167 }
168
169 void ClipStabilize::slotStabilizeFinished(bool success)
170 {
171     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
172     button_start->setEnabled(true);
173     m_duration = 0;
174
175     if (success) {
176         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilize finished."));
177         if (auto_add->isChecked()) {
178             KUrl url;
179             if (urls_list->count() > 0) {
180                 url = KUrl(dest_url->url());
181             } else url = dest_url->url();
182             emit addClip(url);
183         }
184         if (urls_list->count() > 0 && m_urls.count() > 0) {
185             slotStartStabilize();
186             return;
187         } else if (auto_close->isChecked()) accept();
188     } else {
189         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Stabilizing FAILED!"));
190     }
191
192 }
193
194
195 #include "clipstabilize.moc"
196
197