]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
Audio transcoding actions are now in "Extract audio" menu
[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 #if KDE_IS_VERSION(4,7,0)
36     m_infoMessage = new KMessageWidget;
37     QGridLayout *s =  static_cast <QGridLayout*> (layout());
38     s->addWidget(m_infoMessage, 10, 0, 1, -1);
39     m_infoMessage->setCloseButtonVisible(false);
40     m_infoMessage->hide();
41 #endif
42     log_text->setHidden(true);
43     setWindowTitle(i18n("Transcode Clip"));
44     auto_add->setText(i18np("Add clip to project", "Add clips to project", m_urls.count()));
45
46     if (m_urls.count() == 1) {
47         QString fileName = m_urls.at(0).path(); //.section('.', 0, -1);
48         QString newFile = params.section(' ', -1).replace("%1", fileName);
49         KUrl dest(newFile);
50         source_url->setUrl(m_urls.at(0));
51         dest_url->setMode(KFile::File);
52         dest_url->setUrl(dest);
53         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
54         urls_list->setHidden(true);
55         connect(source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
56     } else {
57         label_source->setHidden(true);
58         source_url->setHidden(true);
59         label_dest->setText(i18n("Destination folder"));
60         dest_url->setMode(KFile::Directory);
61         dest_url->setUrl(KUrl(m_urls.at(0).directory()));
62         dest_url->fileDialog()->setOperationMode(KFileDialog::Saving);
63         for (int i = 0; i < m_urls.count(); i++)
64             urls_list->addItem(m_urls.at(i).path());
65     }
66     if (!params.isEmpty()) {
67         label_profile->setHidden(true);
68         profile_list->setHidden(true);
69         ffmpeg_params->setPlainText(params.simplified());
70         if (!description.isEmpty()) {
71             transcode_info->setText(description);
72         } else transcode_info->setHidden(true);
73     } else {
74         // load Profiles
75         KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
76         KConfigGroup transConfig(config, "Transcoding");
77         // read the entries
78         QMap< QString, QString > profiles = transConfig.entryMap();
79         QMapIterator<QString, QString> i(profiles);
80         while (i.hasNext()) {
81             i.next();
82             QStringList data = i.value().split(";");
83             profile_list->addItem(i.key(), data.at(0));
84             if (data.count() > 1) profile_list->setItemData(profile_list->count() - 1, data.at(1), Qt::UserRole + 1);
85         }
86         connect(profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
87         slotUpdateParams(0);
88     }
89
90     connect(button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
91
92     m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
93     connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
94     connect(&m_transcodeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int, QProcess::ExitStatus)));
95     
96     ffmpeg_params->setMaximumHeight(QFontMetrics(font()).lineSpacing() * 5);
97
98     adjustSize();
99 }
100
101 ClipTranscode::~ClipTranscode()
102 {
103     if (m_transcodeProcess.state() != QProcess::NotRunning) {
104         m_transcodeProcess.close();
105     }
106 #if KDE_IS_VERSION(4,7,0)
107     delete m_infoMessage;
108 #endif
109 }
110
111 void ClipTranscode::slotStartTransCode()
112 {
113     if (m_transcodeProcess.state() != QProcess::NotRunning) {
114         return;
115     }
116     m_duration = 0;
117     m_destination.clear();
118 #if KDE_IS_VERSION(4,7,0)
119     m_infoMessage->animatedHide();
120 #endif
121     QStringList parameters;
122     QString destination;
123     QString params = ffmpeg_params->toPlainText().simplified();
124     if (m_urls.count() > 0 && urls_list->count() > 0) {
125         // We are processing multiple clips
126         source_url->setUrl(m_urls.takeFirst());
127         destination = dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName();
128         QList<QListWidgetItem *> matching = urls_list->findItems(source_url->url().path(), Qt::MatchExactly);
129         if (matching.count() > 0) {
130             matching.at(0)->setFlags(Qt::ItemIsSelectable);
131             urls_list->setCurrentItem(matching.at(0));
132         }
133     } else {
134         destination = dest_url->url().path().section('.', 0, -2);
135     }
136     QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
137     QString s_url = source_url->url().path();
138     parameters << "-i" << s_url;
139     if (QFile::exists(destination + extension)) {
140         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", destination + extension)) == KMessageBox::No) return;
141         parameters << "-y";
142     }
143     foreach(QString s, params.split(' '))
144         parameters << s.replace("%1", destination);
145     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
146
147     m_destination = destination + extension;
148     m_transcodeProcess.start("ffmpeg", parameters);
149     source_url->setEnabled(false);
150     dest_url->setEnabled(false);
151     button_start->setEnabled(false);
152
153 }
154
155 void ClipTranscode::slotShowTranscodeInfo()
156 {
157     QString log = QString(m_transcodeProcess.readAll());
158     if (m_duration == 0) {
159         if (log.contains("Duration:")) {
160             QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
161             QStringList numbers = data.split(':');
162             if (numbers.size() < 3) return;
163             m_duration = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
164             log_text->setHidden(true);
165             job_progress->setHidden(false);
166         }
167         else {
168             log_text->setHidden(false);
169             job_progress->setHidden(true);
170         }
171     }
172     else if (log.contains("time=")) {
173         int progress;
174         QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
175         if (time.contains(':')) {
176             QStringList numbers = time.split(':');
177             if (numbers.size() < 3) return;
178             progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
179         }
180         else progress = (int) time.toDouble();
181         job_progress->setValue((int) (100.0 * progress / m_duration));
182     }
183     log_text->setPlainText(log);
184 }
185
186 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
187 {
188     buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
189     button_start->setEnabled(true);
190     source_url->setEnabled(true);
191     dest_url->setEnabled(true);
192     m_duration = 0;
193
194     if (QFileInfo(m_destination).size() <= 0) {
195         // Destination file does not exist, transcoding failed
196         exitCode = 1;
197     }
198     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
199         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding finished."));
200         if (auto_add->isChecked()) {
201             KUrl url;
202             if (urls_list->count() > 0) {
203                 QString params = ffmpeg_params->toPlainText().simplified();
204                 QString extension = params.section("%1", 1, 1).section(' ', 0, 0);
205                 url = KUrl(dest_url->url().path(KUrl::AddTrailingSlash) + source_url->url().fileName() + extension);
206             } else url = dest_url->url();
207             emit addClip(url);
208         }
209         if (urls_list->count() > 0 && m_urls.count() > 0) {
210             m_transcodeProcess.close();
211             slotStartTransCode();
212             return;
213         } else if (auto_close->isChecked()) accept();
214         else {
215 #if KDE_IS_VERSION(4,7,0)
216             m_infoMessage->setMessageType(KMessageWidget::Positive);
217             m_infoMessage->setText(i18n("Transcoding finished."));
218             m_infoMessage->animatedShow();
219 #else
220             log_text->setVisible(true);
221 #endif
222         }
223     } else {
224 #if KDE_IS_VERSION(4,7,0)
225         m_infoMessage->setMessageType(KMessageWidget::Warning);
226         m_infoMessage->setText(i18n("Transcoding failed!"));
227         m_infoMessage->animatedShow();
228 #else
229         log_text->setHtml(log_text->toPlainText() + "<br /><b>" + i18n("Transcoding failed!"));
230 #endif
231         log_text->setVisible(true);
232     }
233     m_transcodeProcess.close();
234     
235     //Refill url list in case user wants to transcode to another format
236     if (urls_list->count() > 0) {
237         m_urls.clear();
238         for (int i = 0; i < urls_list->count(); i++)
239             m_urls << urls_list->item(i)->text();
240     }
241 }
242
243 void ClipTranscode::slotUpdateParams(int ix)
244 {
245     QString fileName = source_url->url().path();
246     if (ix != -1) {
247         QString params = profile_list->itemData(ix).toString();
248         ffmpeg_params->setPlainText(params.simplified());
249         QString desc = profile_list->itemData(ix, Qt::UserRole + 1).toString();
250         if (!desc.isEmpty()) {
251             transcode_info->setText(desc);
252             transcode_info->setHidden(false);
253         } else transcode_info->setHidden(true);
254     }
255     if (urls_list->count() == 0) {
256         QString newFile = ffmpeg_params->toPlainText().simplified().section(' ', -1).replace("%1", fileName);
257         dest_url->setUrl(KUrl(newFile));
258     }
259
260 }
261
262 #include "cliptranscode.moc"
263
264