]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
Cleanup & fix melt job (like video stab) not keeping original clip profile
[kdenlive] / src / cliptranscode.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "cliptranscode.h"
22 #include "kdenlivesettings.h"
23
24 #include <KDebug>
25 #include <KGlobalSettings>
26 #include <KMessageBox>
27 #include <KFileDialog>
28
29
30 ClipTranscode::ClipTranscode(KUrl::List urls, const QString &params, const QString &description, QWidget * parent) :
31         QDialog(parent), m_urls(urls), m_duration(0)
32 {
33     setFont(KGlobalSettings::toolBarFont());
34     setupUi(this);
35     setAttribute(Qt::WA_DeleteOnClose);
36 #if KDE_IS_VERSION(4,7,0)
37     m_infoMessage = new KMessageWidget;
38     QGridLayout *s =  static_cast <QGridLayout*> (layout());
39     s->addWidget(m_infoMessage, 10, 0, 1, -1);
40     m_infoMessage->setCloseButtonVisible(false);
41     m_infoMessage->hide();
42 #endif
43     log_text->setHidden(true);
44     setWindowTitle(i18n("Transcode Clip"));
45     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
46     auto_add->setChecked(KdenliveSettings::add_new_clip());
47
48     if (m_urls.count() == 1) {
49         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
50         QString newFile = params.section(' ', -1).replace("%1", fileName);
51         KUrl dest(newFile);
52         source_url->setUrl(m_urls.at(0));
53         dest_url->setMode(KFile::File);
54         dest_url->setUrl(dest);
55         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
56         urls_list->setHidden(true);
57         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
58     } else {
59         label_source->setHidden(true);
60         source_url->setHidden(true);
61         label_dest->setText(i18n("Destination folder"));
62         dest_url->setMode(KFile::Directory);
63         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
64         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
65         for (int i = 0; i < m_urls.count(); i++)
66             urls_list->addItem(m_urls.at(i).path());
67     }
68     if (!params.isEmpty()) {
69         label_profile->setHidden(true);
70         profile_list->setHidden(true);
71         ffmpeg_params->setPlainText(params.simplified());
72         if (!description.isEmpty()) {
73             transcode_info->setText(description);
74         } else transcode_info->setHidden(true);
75     } else {
76         // load Profiles
77         KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
78         KConfigGroup transConfig(config, "Transcoding");
79         // read the entries
80         QMap< QString, QString > profiles = transConfig.entryMap();
81         QMapIterator<QString, QString> i(profiles);
82         while (i.hasNext()) {
83             i.next();
84             QStringList data = i.value().split(';');
85             profile_list->addItem(i.key(), data.at(0));
86             if (data.count() > 1) profile_list->setItemData(profile_list->count() - 1, data.at(1), Qt::UserRole + 1);
87         }
88         connect(profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
89         slotUpdateParams(0);
90     }
91
92     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
93
94     m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
95     connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
96     connect(&m_transcodeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int, QProcess::ExitStatus)));
97     
98     ffmpeg_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
99
100     adjustSize();
101 }
102
103 ClipTranscode::~ClipTranscode()
104 {
105     KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
106     if (m_transcodeProcess.state() != QProcess::NotRunning) {
107         m_transcodeProcess.close();
108     }
109 #if KDE_IS_VERSION(4,7,0)
110     delete m_infoMessage;
111 #endif
112 }
113
114 void ClipTranscode::slotStartTransCode()
115 {
116     if (m_transcodeProcess.state() != QProcess::NotRunning) {
117         return;
118     }
119     m_duration = 0;
120     m_destination.clear();
121 #if KDE_IS_VERSION(4,7,0)
122     m_infoMessage->animatedHide();
123 #endif
124     QStringList parameters;
125     QString destination;
126     QString params = ffmpeg_params->toPlainText().simplified();
127     if (m_urls.count() > 0 && urls_list->count() > 0) {
128         // We are processing multiple clips
129         source_url->setUrl(m_urls.takeFirst());
130         destination = dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName();
131         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
132         if (matching.count() > 0) {
133             matching.at(0)->setFlags(Qt::ItemIsSelectable);
134             urls_list->setCurrentItem(matching.at(0));
135         }
136     } else {
137         destination = dest_url->url().path().section('.', 0, -2);
138     }
139     QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
140     QString s_url = source_url->url().path();
141     parameters << "-i" << s_url;
142     if (QFile::exists(destination + extension)) {
143         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension)) == KMessageBox::No) return;
144         parameters << "-y";
145     }
146     foreach(QString s, params.split(' '))
147         parameters << s.replace("%1", destination);
148     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
149
150     m_destination = destination + extension;
151     m_transcodeProcess.start(KdenliveSettings::ffmpegpath(), parameters);
152     source_url->setEnabled(false);
153     dest_url->setEnabled(false);
154     button_start->setEnabled(false);
155
156 }
157
158 void ClipTranscode::slotShowTranscodeInfo()
159 {
160     QString log = QString(m_transcodeProcess.readAll());
161     if (m_duration == 0) {
162         if (log.contains("Duration:")) {
163             QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
164             QStringList numbers = data.split(':');
165             if (numbers.size() < 3) return;
166             m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
167             log_text->setHidden(true);
168             job_progress->setHidden(false);
169         }
170         else {
171             log_text->setHidden(false);
172             job_progress->setHidden(true);
173         }
174     }
175     else if (log.contains("time=")) {
176         int progress;
177         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
178         if (time.contains(':')) {
179             QStringList numbers = time.split(':');
180             if (numbers.size() < 3) return;
181             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
182         }
183         else progress = (int) time.toDouble();
184         job_progress->setValue((int) (100.0 * progress / m_duration));
185     }
186     log_text->setPlainText(log);
187 }
188
189 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
190 {
191     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
192     button_start->setEnabled(true);
193     source_url->setEnabled(true);
194     dest_url->setEnabled(true);
195     m_duration = 0;
196
197     if (QFileInfo(m_destination).size() <= 0) {
198         // Destination file does not exist, transcoding failed
199         exitCode = 1;
200     }
201     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
202         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
203         if (auto_add->isChecked()) {
204             KUrl url;
205             if (urls_list->count() > 0) {
206                 QString params = ffmpeg_params->toPlainText().simplified();
207                 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
208                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName() + extension);
209             } else url = dest_url->url();
210             emit addClip(url);
211         }
212         if (urls_list->count() > 0 && m_urls.count() > 0) {
213             m_transcodeProcess.close();
214             slotStartTransCode();
215             return;
216         } else if (auto_close->isChecked()) accept();
217         else {
218 #if KDE_IS_VERSION(4,7,0)
219             m_infoMessage->setMessageType(KMessageWidget::Positive);
220             m_infoMessage->setText(i18n("Transcoding finished."));
221             m_infoMessage->animatedShow();
222 #else
223             log_text->setVisible(true);
224 #endif
225         }
226     } else {
227 #if KDE_IS_VERSION(4,7,0)
228         m_infoMessage->setMessageType(KMessageWidget::Warning);
229         m_infoMessage->setText(i18n("Transcoding failed!"));
230         m_infoMessage->animatedShow();
231 #else
232         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding failed!"));
233 #endif
234         log_text->setVisible(true);
235     }
236     m_transcodeProcess.close();
237     
238     //Refill url list in case user wants to transcode to another format
239     if (urls_list->count() > 0) {
240         m_urls.clear();
241         for (int i = 0; i < urls_list->count(); i++)
242             m_urls << urls_list->item(i)->text();
243     }
244 }
245
246 void ClipTranscode::slotUpdateParams(int ix)
247 {
248     QString fileName = source_url->url().path();
249     if (ix != -1) {
250         QString params = profile_list->itemData(ix).toString();
251         ffmpeg_params->setPlainText(params.simplified());
252         QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
253         if (!desc.isEmpty()) {
254             transcode_info->setText(desc);
255             transcode_info->setHidden(false);
256         } else transcode_info->setHidden(true);
257     }
258     if (urls_list->count() == 0) {
259         QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
260         dest_url->setUrl(KUrl(newFile));
261     }
262
263 }
264
265 #include "cliptranscode.moc"
266
267