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>
26 #include <KFileDialog>
29 ClipTranscode::ClipTranscode(KUrl::List urls, const QString ¶ms, MltVideoProfile profile, QWidget * parent) :
30 QDialog(parent), m_urls(urls), m_profile(profile)
32 setFont(KGlobalSettings::toolBarFont());
34 setAttribute(Qt::WA_DeleteOnClose);
35 setWindowTitle(i18n("Transcode Clip"));
37 if (m_urls.count() == 1) {
38 QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
39 QString newFile = params.section(' ', -1).replace("%1", fileName);
41 m_view.source_url->setUrl(m_urls.at(0));
42 m_view.dest_url->setUrl(dest);
43 m_view.dest_url->setMode(KFile::File);
44 m_view.dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
45 m_view.urls_list->setHidden(true);
46 connect(m_view.source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
48 m_view.label_source->setHidden(true);
49 m_view.label_dest->setHidden(true);
50 m_view.source_url->setHidden(true);
51 m_view.dest_url->setHidden(true);
52 for (int i = 0; i < m_urls.count(); i++)
53 m_view.urls_list->addItem(m_urls.at(i).path());
55 if (!params.isEmpty()) {
56 m_view.label_profile->setHidden(true);
57 m_view.profile_list->setHidden(true);
58 m_view.params->setPlainText(prepareParams(params));
61 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
62 KConfigGroup transConfig(config, "Transcoding");
64 QMap< QString, QString > profiles = transConfig.entryMap();
65 QMapIterator<QString, QString> i(profiles);
66 connect(m_view.profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
69 m_view.profile_list->addItem(i.key(), i.value());
73 connect(m_view.button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
75 m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
76 connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
77 connect(&m_transcodeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int, QProcess::ExitStatus)));
82 ClipTranscode::~ClipTranscode()
84 if (m_transcodeProcess.state() != QProcess::NotRunning) {
85 m_transcodeProcess.close();
89 void ClipTranscode::slotStartTransCode()
91 if (m_transcodeProcess.state() != QProcess::NotRunning) {
94 QStringList parameters;
95 if (m_view.urls_list->count() > 0) {
96 m_view.source_url->setUrl(m_urls.takeFirst());
98 QList<QListWidgetItem *> matching = m_view.urls_list->findItems(m_view.source_url->url().path(), Qt::MatchExactly);
99 if (matching.count() > 0) {
100 matching.at(0)->setFlags(Qt::ItemIsSelectable);
101 m_view.urls_list->setCurrentItem(matching.at(0));
104 QString source_url = m_view.source_url->url().path();
106 parameters << "-i" << source_url;
107 if (QFile::exists(m_view.dest_url->url().path())) {
108 if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", m_view.dest_url->url().path())) == KMessageBox::No) return;
111 m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
112 QString params = m_view.params->toPlainText().simplified();
113 params = params.section(' ', 0, -2);
114 parameters << params.split(' ') << m_view.dest_url->url().path();
116 //kDebug() << "/// FFMPEG ARGS: " << parameters;
118 m_transcodeProcess.start("ffmpeg", parameters);
119 m_view.button_start->setEnabled(false);
123 void ClipTranscode::slotShowTranscodeInfo()
125 QString log = QString(m_transcodeProcess.readAll());
126 //kDebug() << "//LOG: " << log;
127 m_view.log->setPlainText(log);
130 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
132 m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
133 m_view.button_start->setEnabled(true);
135 if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
136 m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding finished."));
137 if (m_view.auto_add->isChecked()) emit addClip(m_view.dest_url->url());
138 if (m_view.urls_list->count() > 0 && m_urls.count() > 0) {
139 m_transcodeProcess.close();
140 slotStartTransCode();
142 } else if (m_view.auto_close->isChecked()) accept();
144 m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding FAILED!"));
147 m_transcodeProcess.close();
150 void ClipTranscode::slotUpdateParams(int ix)
152 QString fileName = m_view.source_url->url().path();
154 QString params = m_view.profile_list->itemData(ix).toString();
155 m_view.params->setPlainText(prepareParams(params));
158 QString newFile = m_view.params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
159 m_view.dest_url->setUrl(KUrl(newFile));
163 QString ClipTranscode::prepareParams(QString params)
165 return "-s " + QString::number(m_profile.width) + 'x' + QString::number(m_profile.height) + " -r " + QString::number((double) m_profile.frame_rate_num / m_profile.frame_rate_den) + ' ' + params.simplified();
168 #include "cliptranscode.moc"