]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
Add XML files for frei0r contrast0r and brightness effects so that we have correct...
[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
23 #include <KDebug>
24 #include <KGlobalSettings>
25 #include <KMessageBox>
26 #include <KFileDialog>
27
28
29 ClipTranscode::ClipTranscode(KUrl::List urls, const QString &params, MltVideoProfile profile, QWidget * parent) :
30         QDialog(parent), m_urls(urls), m_profile(profile)
31 {
32     setFont(KGlobalSettings::toolBarFont());
33     m_view.setupUi(this);
34     setAttribute(Qt::WA_DeleteOnClose);
35     setWindowTitle(i18n("Transcode Clip"));
36
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);
40         KUrl dest(newFile);
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()));
47     } else {
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());
54     }
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));
59     } else {
60         // load Profiles
61         KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
62         KConfigGroup transConfig(config, "Transcoding");
63         // read the entries
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)));
67         while (i.hasNext()) {
68             i.next();
69             m_view.profile_list->addItem(i.key(), i.value());
70         }
71     }
72
73     connect(m_view.button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
74
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)));
78
79     //adjustSize();
80 }
81
82 ClipTranscode::~ClipTranscode()
83 {
84     if (m_transcodeProcess.state() != QProcess::NotRunning) {
85         m_transcodeProcess.close();
86     }
87 }
88
89 void ClipTranscode::slotStartTransCode()
90 {
91     if (m_transcodeProcess.state() != QProcess::NotRunning) {
92         return;
93     }
94     QStringList parameters;
95     if (m_view.urls_list->count() > 0) {
96         m_view.source_url->setUrl(m_urls.takeFirst());
97         slotUpdateParams(-1);
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));
102         }
103     }
104     QString source_url = m_view.source_url->url().path();
105
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;
109         parameters << "-y";
110     }
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();
115
116     //kDebug() << "/// FFMPEG ARGS: " << parameters;
117
118     m_transcodeProcess.start("ffmpeg", parameters);
119     m_view.button_start->setEnabled(false);
120
121 }
122
123 void ClipTranscode::slotShowTranscodeInfo()
124 {
125     QString log = QString(m_transcodeProcess.readAll());
126     //kDebug() << "//LOG: " << log;
127     m_view.log->setPlainText(log);
128 }
129
130 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
131 {
132     m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
133     m_view.button_start->setEnabled(true);
134
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();
141             return;
142         } else if (m_view.auto_close->isChecked()) accept();
143     } else {
144         m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding FAILED!"));
145     }
146
147     m_transcodeProcess.close();
148 }
149
150 void ClipTranscode::slotUpdateParams(int ix)
151 {
152     QString fileName = m_view.source_url->url().path();
153     if (ix != -1) {
154         QString params = m_view.profile_list->itemData(ix).toString();
155         m_view.params->setPlainText(prepareParams(params));
156     }
157
158     QString newFile = m_view.params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
159     m_view.dest_url->setUrl(KUrl(newFile));
160
161 }
162
163 QString ClipTranscode::prepareParams(QString params)
164 {
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();
166 }
167
168 #include "cliptranscode.moc"
169
170