]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
Fix label
[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(const KUrl::List &urls, const QString &params, const QStringList &postParams, const QString &description, bool automaticMode, QWidget * parent) :
31     QDialog(parent), m_urls(urls), m_duration(0), m_automaticMode(automaticMode), m_postParams(postParams)
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     if (m_automaticMode) {
46         auto_add->setHidden(true);
47     }
48     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
49     auto_add->setChecked(KdenliveSettings::add_new_clip());
50
51     if (m_urls.count() == 1) {
52         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
53         QString newFile = params.section(' ', -1).replace("%1", fileName);
54         KUrl dest(newFile);
55         source_url->setUrl(m_urls.at(0));
56         dest_url->setMode(KFile::File);
57         dest_url->setUrl(dest);
58         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
59         urls_list->setHidden(true);
60         connect(source_url, SIGNAL(textChanged(QString)), this, SLOT(slotUpdateParams()));
61     } else {
62         label_source->setHidden(true);
63         source_url->setHidden(true);
64         label_dest->setText(i18n("Destination folder"));
65         dest_url->setMode(KFile::Directory);
66         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
67         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
68         for (int i = 0; i < m_urls.count(); ++i)
69             urls_list->addItem(m_urls.at(i).path());
70     }
71     if (!params.isEmpty()) {
72         label_profile->setHidden(true);
73         profile_list->setHidden(true);
74         ffmpeg_params->setPlainText(params.simplified());
75         if (!description.isEmpty()) {
76             transcode_info->setText(description);
77         } else transcode_info->setHidden(true);
78     } else {
79         // load Profiles
80         KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc", KConfig::CascadeConfig);
81         KConfigGroup transConfig(config, "Transcoding");
82         // read the entries
83         QMap< QString, QString > profiles = transConfig.entryMap();
84         QMapIterator<QString, QString> i(profiles);
85         while (i.hasNext()) {
86             i.next();
87             QStringList data = i.value().split(';');
88             profile_list->addItem(i.key(), data.at(0));
89             if (data.count() > 1) profile_list->setItemData(profile_list->count() - 1, data.at(1), Qt::UserRole + 1);
90         }
91         connect(profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
92         slotUpdateParams(0);
93     }
94
95     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
96
97     m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
98     connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
99     connect(&m_transcodeProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int,QProcess::ExitStatus)));
100     
101     ffmpeg_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
102
103     adjustSize();
104 }
105
106 ClipTranscode::~ClipTranscode()
107 {
108     KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
109     if (m_transcodeProcess.state() != QProcess::NotRunning) {
110         m_transcodeProcess.close();
111     }
112 #if KDE_IS_VERSION(4,7,0)
113     delete m_infoMessage;
114 #endif
115 }
116
117 void ClipTranscode::slotStartTransCode()
118 {
119     if (m_transcodeProcess.state() != QProcess::NotRunning) {
120         return;
121     }
122     m_duration = 0;
123     m_destination.clear();
124 #if KDE_IS_VERSION(4,7,0)
125     m_infoMessage->animatedHide();
126 #endif
127     QStringList parameters;
128     QString destination;
129     QString params = ffmpeg_params->toPlainText().simplified();
130     if (!m_urls.isEmpty() && urls_list->count() > 0) {
131         // We are processing multiple clips
132         source_url->setUrl(m_urls.takeFirst());
133         destination = dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName();
134         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
135         if (matching.count() > 0) {
136             matching.at(0)->setFlags(Qt::ItemIsSelectable);
137             urls_list->setCurrentItem(matching.at(0));
138         }
139     } else {
140         destination = dest_url->url().path().section('.', 0, -2);
141     }
142     QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
143     QString s_url = source_url->url().path();
144     parameters << "-i" << s_url;
145     if (QFile::exists(destination + extension)) {
146         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension)) == KMessageBox::No) {
147             // Abort operation
148             if (m_automaticMode) {
149                 // inform caller that we aborted
150                 emit transcodedClip(source_url->url(), KUrl());
151                 close();
152             }
153             return;
154         }
155         parameters << "-y";
156     }
157
158     bool replaceVfParams = false;
159     foreach(QString s, params.split(' ')) {
160         if (replaceVfParams) {
161             s= m_postParams.at(1);
162             replaceVfParams = false;
163         }
164         parameters << s.replace("%1", destination);
165         if (s == "-vf") {
166             replaceVfParams = true;
167         }
168     }
169     
170     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
171
172     m_destination = destination + extension;
173     m_transcodeProcess.start(KdenliveSettings::ffmpegpath(), parameters);
174     source_url->setEnabled(false);
175     dest_url->setEnabled(false);
176     button_start->setEnabled(false);
177
178 }
179
180 void ClipTranscode::slotShowTranscodeInfo()
181 {
182     QString log = QString(m_transcodeProcess.readAll());
183     if (m_duration == 0) {
184         if (log.contains("Duration:")) {
185             QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
186             QStringList numbers = data.split(':');
187             if (numbers.size() < 3) return;
188             m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
189             log_text->setHidden(true);
190             job_progress->setHidden(false);
191         }
192         else {
193             log_text->setHidden(false);
194             job_progress->setHidden(true);
195         }
196     }
197     else if (log.contains("time=")) {
198         int progress;
199         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
200         if (time.contains(':')) {
201             QStringList numbers = time.split(':');
202             if (numbers.size() < 3) return;
203             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
204         }
205         else progress = (int) time.toDouble();
206         job_progress->setValue((int) (100.0 * progress / m_duration));
207     }
208     log_text->setPlainText(log);
209 }
210
211 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
212 {
213     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
214     button_start->setEnabled(true);
215     source_url->setEnabled(true);
216     dest_url->setEnabled(true);
217     m_duration = 0;
218
219     if (QFileInfo(m_destination).size() <= 0) {
220         // Destination file does not exist, transcoding failed
221         exitCode = 1;
222     }
223     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
224         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
225         if (auto_add->isChecked() || m_automaticMode) {
226             KUrl url;
227             if (urls_list->count() > 0) {
228                 QString params = ffmpeg_params->toPlainText().simplified();
229                 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
230                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName() + extension);
231             } else url = dest_url->url();
232             if (m_automaticMode) emit transcodedClip(source_url->url(), url);
233             else emit addClip(url);
234         }
235         if (urls_list->count() > 0 && m_urls.count() > 0) {
236             m_transcodeProcess.close();
237             slotStartTransCode();
238             return;
239         } else if (auto_close->isChecked()) accept();
240         else {
241 #if KDE_IS_VERSION(4,7,0)
242             m_infoMessage->setMessageType(KMessageWidget::Positive);
243             m_infoMessage->setText(i18n("Transcoding finished."));
244             m_infoMessage->animatedShow();
245 #else
246             log_text->setVisible(true);
247 #endif
248         }
249     } else {
250 #if KDE_IS_VERSION(4,7,0)
251         m_infoMessage->setMessageType(KMessageWidget::Warning);
252         m_infoMessage->setText(i18n("Transcoding failed!"));
253         m_infoMessage->animatedShow();
254 #else
255         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding failed!"));
256 #endif
257         log_text->setVisible(true);
258     }
259     m_transcodeProcess.close();
260     
261     //Refill url list in case user wants to transcode to another format
262     if (urls_list->count() > 0) {
263         m_urls.clear();
264         for (int i = 0; i < urls_list->count(); ++i)
265             m_urls << urls_list->item(i)->text();
266     }
267 }
268
269 void ClipTranscode::slotUpdateParams(int ix)
270 {
271     QString fileName = source_url->url().path();
272     if (ix != -1) {
273         QString params = profile_list->itemData(ix).toString();
274         ffmpeg_params->setPlainText(params.simplified());
275         QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
276         if (!desc.isEmpty()) {
277             transcode_info->setText(desc);
278             transcode_info->setHidden(false);
279         } else transcode_info->setHidden(true);
280     }
281     if (urls_list->count() == 0) {
282         QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
283         dest_url->setUrl(KUrl(newFile));
284     }
285
286 }
287
288 #include "cliptranscode.moc"
289
290