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