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