1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "cliptranscode.h"
22 #include "kdenlivesettings.h"
25 #include <KGlobalSettings>
26 #include <KMessageBox>
27 #include <KFileDialog>
30 ClipTranscode::ClipTranscode(KUrl::List urls, const QString ¶ms, 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)
33 setFont(KGlobalSettings::toolBarFont());
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();
43 log_text->setHidden(true);
44 setWindowTitle(i18n("Transcode Clip"));
45 if (m_automaticMode) {
46 auto_add->setHidden(true);
48 auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
49 auto_add->setChecked(KdenliveSettings::add_new_clip());
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);
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(const QString &)), this, SLOT(slotUpdateParams()));
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());
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);
80 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc", KConfig::CascadeConfig);
81 KConfigGroup transConfig(config, "Transcoding");
83 QMap< QString, QString > profiles = transConfig.entryMap();
84 QMapIterator<QString, QString> i(profiles);
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);
91 connect(profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
95 connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
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)));
101 ffmpeg_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
104 if (m_automaticMode) slotStartTransCode();
107 ClipTranscode::~ClipTranscode()
109 KdenliveSettings::setAdd_new_clip(auto_add->isChecked());
110 if (m_transcodeProcess.state() != QProcess::NotRunning) {
111 m_transcodeProcess.close();
113 #if KDE_IS_VERSION(4,7,0)
114 delete m_infoMessage;
118 void ClipTranscode::slotStartTransCode()
120 if (m_transcodeProcess.state() != QProcess::NotRunning) {
124 m_destination.clear();
125 #if KDE_IS_VERSION(4,7,0)
126 m_infoMessage->animatedHide();
128 QStringList parameters;
130 QString params = ffmpeg_params->toPlainText().simplified();
131 if (m_urls.count() > 0 && urls_list->count() > 0) {
132 // We are processing multiple clips
133 source_url->setUrl(m_urls.takeFirst());
134 destination = dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName();
135 QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
136 if (matching.count() > 0) {
137 matching.at(0)->setFlags(Qt::ItemIsSelectable);
138 urls_list->setCurrentItem(matching.at(0));
141 destination = dest_url->url().path().section('.', 0, -2);
143 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
144 QString s_url = source_url->url().path();
145 parameters << "-i" << s_url;
146 if (QFile::exists(destination + extension)) {
147 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension)) == KMessageBox::No) return;
151 bool replaceVfParams = false;
152 foreach(QString s, params.split(' ')) {
153 if (replaceVfParams) {
154 s= m_postParams.at(1);
155 replaceVfParams = false;
157 parameters << s.replace("%1", destination);
159 replaceVfParams = true;
163 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
165 m_destination = destination + extension;
166 m_transcodeProcess.start(KdenliveSettings::ffmpegpath(), parameters);
167 source_url->setEnabled(false);
168 dest_url->setEnabled(false);
169 button_start->setEnabled(false);
173 void ClipTranscode::slotShowTranscodeInfo()
175 QString log = QString(m_transcodeProcess.readAll());
176 if (m_duration == 0) {
177 if (log.contains("Duration:")) {
178 QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
179 QStringList numbers = data.split(':');
180 if (numbers.size() < 3) return;
181 m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
182 log_text->setHidden(true);
183 job_progress->setHidden(false);
186 log_text->setHidden(false);
187 job_progress->setHidden(true);
190 else if (log.contains("time=")) {
192 QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
193 if (time.contains(':')) {
194 QStringList numbers = time.split(':');
195 if (numbers.size() < 3) return;
196 progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
198 else progress = (int) time.toDouble();
199 job_progress->setValue((int) (100.0 * progress / m_duration));
201 log_text->setPlainText(log);
204 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
206 buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
207 button_start->setEnabled(true);
208 source_url->setEnabled(true);
209 dest_url->setEnabled(true);
212 if (QFileInfo(m_destination).size() <= 0) {
213 // Destination file does not exist, transcoding failed
216 if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
217 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
218 if (auto_add->isChecked() || m_automaticMode) {
220 if (urls_list->count() > 0) {
221 QString params = ffmpeg_params->toPlainText().simplified();
222 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
223 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName() + extension);
224 } else url = dest_url->url();
225 if (m_automaticMode) emit transcodedClip(source_url->url(), url);
226 else emit addClip(url);
228 if (urls_list->count() > 0 && m_urls.count() > 0) {
229 m_transcodeProcess.close();
230 slotStartTransCode();
232 } else if (auto_close->isChecked()) accept();
234 #if KDE_IS_VERSION(4,7,0)
235 m_infoMessage->setMessageType(KMessageWidget::Positive);
236 m_infoMessage->setText(i18n("Transcoding finished."));
237 m_infoMessage->animatedShow();
239 log_text->setVisible(true);
243 #if KDE_IS_VERSION(4,7,0)
244 m_infoMessage->setMessageType(KMessageWidget::Warning);
245 m_infoMessage->setText(i18n("Transcoding failed!"));
246 m_infoMessage->animatedShow();
248 log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding failed!"));
250 log_text->setVisible(true);
252 m_transcodeProcess.close();
254 //Refill url list in case user wants to transcode to another format
255 if (urls_list->count() > 0) {
257 for (int i = 0; i < urls_list->count(); i++)
258 m_urls << urls_list->item(i)->text();
262 void ClipTranscode::slotUpdateParams(int ix)
264 QString fileName = source_url->url().path();
266 QString params = profile_list->itemData(ix).toString();
267 ffmpeg_params->setPlainText(params.simplified());
268 QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
269 if (!desc.isEmpty()) {
270 transcode_info->setText(desc);
271 transcode_info->setHidden(false);
272 } else transcode_info->setHidden(true);
274 if (urls_list->count() == 0) {
275 QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
276 dest_url->setUrl(KUrl(newFile));
281 #include "cliptranscode.moc"