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"
24 #include <KGlobalSettings>
25 #include <KMessageBox>
28 ClipTranscode::ClipTranscode(KUrl::List urls, const QString ¶ms, QWidget * parent) :
29 QDialog(parent), m_urls(urls)
31 setFont(KGlobalSettings::toolBarFont());
33 setAttribute(Qt::WA_DeleteOnClose);
34 setWindowTitle(i18n("Transcode Clip"));
36 if (m_urls.count() == 1) {
37 QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
38 QString newFile = params.section(' ', -1).replace("%1", fileName);
40 m_view.source_url->setUrl(m_urls.at(0));
41 m_view.dest_url->setUrl(dest);
42 m_view.urls_list->setHidden(true);
43 connect(m_view.source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
45 m_view.label_source->setHidden(true);
46 m_view.label_dest->setHidden(true);
47 m_view.source_url->setHidden(true);
48 m_view.dest_url->setHidden(true);
49 for (int i = 0; i < m_urls.count(); i++)
50 m_view.urls_list->addItem(m_urls.at(i).path());
52 if (!params.isEmpty()) {
53 m_view.label_profile->setHidden(true);
54 m_view.profile_list->setHidden(true);
55 m_view.params->setPlainText(params.simplified());
58 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
59 KConfigGroup transConfig(config, "Transcoding");
61 QMap< QString, QString > profiles = transConfig.entryMap();
62 QMapIterator<QString, QString> i(profiles);
63 connect(m_view.profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
66 m_view.profile_list->addItem(i.key(), i.value());
70 connect(m_view.button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
72 m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
73 connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
74 connect(&m_transcodeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int, QProcess::ExitStatus)));
79 ClipTranscode::~ClipTranscode()
81 if (m_transcodeProcess.state() != QProcess::NotRunning) {
82 m_transcodeProcess.close();
86 void ClipTranscode::slotStartTransCode()
88 if (m_transcodeProcess.state() != QProcess::NotRunning) {
91 QStringList parameters;
92 if (m_view.urls_list->count() > 0) {
93 m_view.source_url->setUrl(m_urls.takeFirst());
95 QList<QListWidgetItem *> matching = m_view.urls_list->findItems(m_view.source_url->url().path(), Qt::MatchExactly);
96 if (matching.count() > 0) {
97 matching.at(0)->setFlags(Qt::ItemIsSelectable);
98 m_view.urls_list->setCurrentItem(matching.at(0));
101 QString source_url = m_view.source_url->url().path();
103 parameters << "-i" << source_url;
104 if (QFile::exists(m_view.dest_url->url().path())) {
105 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", m_view.dest_url->url().path())) == KMessageBox::No) return;
108 m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
109 QString params = m_view.params->toPlainText().simplified();
110 params = params.section(' ', 0, -2);
111 parameters << params.split(' ') << m_view.dest_url->url().path();
113 //kDebug() << "/// FFMPEG ARGS: " << parameters;
115 m_transcodeProcess.start("ffmpeg", parameters);
116 m_view.button_start->setEnabled(false);
120 void ClipTranscode::slotShowTranscodeInfo()
122 QString log = QString(m_transcodeProcess.readAll());
123 //kDebug() << "//LOG: " << log;
124 m_view.log->setPlainText(log);
127 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
129 m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
130 m_view.button_start->setEnabled(true);
132 if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
133 m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding finished."));
134 if (m_view.auto_add->isChecked()) emit addClip(m_view.dest_url->url());
135 if (m_view.urls_list->count() > 0 && m_urls.count() > 0) {
136 m_transcodeProcess.close();
137 slotStartTransCode();
139 } else if (m_view.auto_close->isChecked()) accept();
141 m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding FAILED!"));
144 m_transcodeProcess.close();
147 void ClipTranscode::slotUpdateParams(int ix)
149 QString fileName = m_view.source_url->url().path();
151 QString params = m_view.profile_list->itemData(ix).toString();
152 m_view.params->setPlainText(params.simplified());
155 QString newFile = m_view.params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
156 m_view.dest_url->setUrl(KUrl(newFile));
160 #include "cliptranscode.moc"