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