]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
8dbf6df4430c436500464390dbf4ca5c24ed450c
[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, const QString &description, QWidget * parent) :
30         QDialog(parent), m_urls(urls), m_duration(0)
31 {
32     setFont(KGlobalSettings::toolBarFont());
33     setupUi(this);
34     setAttribute(Qt::WA_DeleteOnClose);
35     log_text->setHidden(true);
36     setWindowTitle(i18n("Transcode Clip"));
37     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
38
39     if (m_urls.count() == 1) {
40         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
41         QString newFile = params.section(' ', -1).replace("%1", fileName);
42         KUrl dest(newFile);
43         source_url->setUrl(m_urls.at(0));
44         dest_url->setMode(KFile::File);
45         dest_url->setUrl(dest);
46         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
47         urls_list->setHidden(true);
48         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
49     } else {
50         label_source->setHidden(true);
51         source_url->setHidden(true);
52         label_dest->setText(i18n("Destination folder"));
53         dest_url->setMode(KFile::Directory);
54         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
55         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
56         for (int i = 0; i < m_urls.count(); i++)
57             urls_list->addItem(m_urls.at(i).path());
58     }
59     if (!params.isEmpty()) {
60         label_profile->setHidden(true);
61         profile_list->setHidden(true);
62         ffmpeg_params->setPlainText(params.simplified());
63         if (!description.isEmpty()) {
64             transcode_info->setText(description);
65         } else transcode_info->setHidden(true);
66     } else {
67         // load Profiles
68         KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
69         KConfigGroup transConfig(config, "Transcoding");
70         // read the entries
71         QMap< QString, QString > profiles = transConfig.entryMap();
72         QMapIterator<QString, QString> i(profiles);
73         while (i.hasNext()) {
74             i.next();
75             QStringList data = i.value().split(";", QString::SkipEmptyParts);
76             profile_list->addItem(i.key(), data.at(0));
77             if (data.count() > 1) profile_list->setItemData(profile_list->count() - 1, data.at(1), Qt::UserRole + 1);
78         }
79         connect(profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
80         slotUpdateParams(0);
81     }
82
83     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
84
85     m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
86     connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
87     connect(&m_transcodeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int, QProcess::ExitStatus)));
88
89     adjustSize();
90 }
91
92 ClipTranscode::~ClipTranscode()
93 {
94     if (m_transcodeProcess.state() != QProcess::NotRunning) {
95         m_transcodeProcess.close();
96     }
97 }
98
99 void ClipTranscode::slotStartTransCode()
100 {
101     if (m_transcodeProcess.state() != QProcess::NotRunning) {
102         return;
103     }
104     m_duration = 0;
105     QStringList parameters;
106     QString destination;
107     QString params = ffmpeg_params->toPlainText().simplified();
108     if (urls_list->count() > 0) {
109         source_url->setUrl(m_urls.takeFirst());
110         destination = dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName();
111         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
112         if (matching.count() > 0) {
113             matching.at(0)->setFlags(Qt::ItemIsSelectable);
114             urls_list->setCurrentItem(matching.at(0));
115         }
116     } else {
117         destination = dest_url->url().path().section('.', 0, -2);
118     }
119     QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
120     QString s_url = source_url->url().path();
121
122     parameters << "-i" << s_url;
123     if (QFile::exists(destination + extension)) {
124         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension)) == KMessageBox::No) return;
125         parameters << "-y";
126     }
127     foreach(QString s, params.split(' '))
128     parameters << s.replace("%1", destination);
129     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
130
131     //kDebug() << "/// FFMPEG ARGS: " << parameters;
132
133     m_transcodeProcess.start("ffmpeg", parameters);
134     button_start->setEnabled(false);
135
136 }
137
138 void ClipTranscode::slotShowTranscodeInfo()
139 {
140     QString log = QString(m_transcodeProcess.readAll());
141     int progress;
142     if (m_duration == 0) {
143         if (log.contains("Duration:")) {
144             QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
145             QStringList numbers = data.split(':');
146             m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
147             log_text->setHidden(true);
148             job_progress->setHidden(false);
149         }
150         else {
151             log_text->setHidden(false);
152             job_progress->setHidden(true);
153         }
154     }
155     else if (log.contains("time=")) {
156         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
157         if (time.contains(':')) {
158             QStringList numbers = time.split(':');
159             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
160         }
161         else progress = (int) time.toDouble();
162         kDebug()<<"// PROGRESS: "<<progress<<", "<<m_duration;
163         job_progress->setValue((int) (100.0 * progress / m_duration));
164     }
165     //kDebug() << "//LOG: " << log;
166     log_text->setPlainText(log);
167 }
168
169 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
170 {
171     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
172     button_start->setEnabled(true);
173     m_duration = 0;
174
175     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
176         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
177         if (auto_add->isChecked()) {
178             KUrl url;
179             if (urls_list->count() > 0) {
180                 QString params = ffmpeg_params->toPlainText().simplified();
181                 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
182                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName() + extension);
183             } else url = dest_url->url();
184             emit addClip(url);
185         }
186         if (urls_list->count() > 0 && m_urls.count() > 0) {
187             m_transcodeProcess.close();
188             slotStartTransCode();
189             return;
190         } else if (auto_close->isChecked()) accept();
191     } else {
192         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding FAILED!"));
193     }
194
195     m_transcodeProcess.close();
196 }
197
198 void ClipTranscode::slotUpdateParams(int ix)
199 {
200     QString fileName = source_url->url().path();
201     if (ix != -1) {
202         QString params = profile_list->itemData(ix).toString();
203         ffmpeg_params->setPlainText(params.simplified());
204         QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
205         if (!desc.isEmpty()) {
206             transcode_info->setText(desc);
207             transcode_info->setHidden(false);
208         } else transcode_info->setHidden(true);
209     }
210     if (urls_list->count() == 0) {
211         QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
212         dest_url->setUrl(KUrl(newFile));
213     }
214
215 }
216
217 #include "cliptranscode.moc"
218
219