]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
Undo files that should not have been part of last commit
[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
27
28 ClipTranscode::ClipTranscode(KUrl::List urls, const QString &params, QWidget * parent) :
29         QDialog(parent), m_urls(urls)
30 {
31     setFont(KGlobalSettings::toolBarFont());
32     m_view.setupUi(this);
33     setAttribute(Qt::WA_DeleteOnClose);
34     setWindowTitle(i18n("Transcode Clip"));
35
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);
39         KUrl dest(newFile);
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()));
44     } else {
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());
51     }
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());
56     } else {
57         // load Profiles
58         KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
59         KConfigGroup transConfig(config, "Transcoding");
60         // read the entries
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)));
64         while (i.hasNext()) {
65             i.next();
66             m_view.profile_list->addItem(i.key(), i.value());
67         }
68     }
69
70     connect(m_view.button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
71
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)));
75
76     //adjustSize();
77 }
78
79 ClipTranscode::~ClipTranscode()
80 {
81     if (m_transcodeProcess.state() != QProcess::NotRunning) {
82         m_transcodeProcess.close();
83     }
84 }
85
86 void ClipTranscode::slotStartTransCode()
87 {
88     if (m_transcodeProcess.state() != QProcess::NotRunning) {
89         return;
90     }
91     QStringList parameters;
92     if (m_view.urls_list->count() > 0) {
93         m_view.source_url->setUrl(m_urls.takeFirst());
94         slotUpdateParams(-1);
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));
99         }
100     }
101     QString source_url = m_view.source_url->url().path();
102
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;
106         parameters << "-y";
107     }
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();
112
113     //kDebug() << "/// FFMPEG ARGS: " << parameters;
114
115     m_transcodeProcess.start("ffmpeg", parameters);
116     m_view.button_start->setEnabled(false);
117
118 }
119
120 void ClipTranscode::slotShowTranscodeInfo()
121 {
122     QString log = QString(m_transcodeProcess.readAll());
123     //kDebug() << "//LOG: " << log;
124     m_view.log->setPlainText(log);
125 }
126
127 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
128 {
129     m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
130     m_view.button_start->setEnabled(true);
131
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();
138             return;
139         } else if (m_view.auto_close->isChecked()) accept();
140     } else {
141         m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding FAILED!"));
142     }
143
144     m_transcodeProcess.close();
145 }
146
147 void ClipTranscode::slotUpdateParams(int ix)
148 {
149     QString fileName = m_view.source_url->url().path();
150     if (ix != -1) {
151         QString params = m_view.profile_list->itemData(ix).toString();
152         m_view.params->setPlainText(params.simplified());
153     }
154
155     QString newFile = m_view.params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
156     m_view.dest_url->setUrl(KUrl(newFile));
157
158 }
159
160 #include "cliptranscode.moc"
161
162