]> git.sesse.net Git - kdenlive/blob - src/cliptranscode.cpp
Add new "Transcode Clip" option in File 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
27
28 ClipTranscode::ClipTranscode(const KUrl &src, const QString &params, QWidget * parent) :
29         QDialog(parent)
30 {
31     setFont(KGlobalSettings::toolBarFont());
32     m_view.setupUi(this);
33     setAttribute(Qt::WA_DeleteOnClose);
34     setWindowTitle(i18n("Transcode Clip"));
35
36     QString fileName = src.path(); //.section('.', 0, -1);
37     QString newFile = params.section(' ', -1).replace("%1", fileName);
38     KUrl dest(newFile);
39     m_view.source_url->setUrl(src);
40     m_view.dest_url->setUrl(dest);
41     connect(m_view.source_url, SIGNAL(textChanged(const QString &)), this, SLOT(slotUpdateParams()));
42     if (!params.isEmpty()) {
43         m_view.label_profile->setHidden(true);
44         m_view.profile_list->setHidden(true);
45         m_view.params->setPlainText(params.simplified());
46     } else {
47         // load Profiles
48         KSharedConfigPtr config = KGlobal::config();
49         KConfigGroup transConfig(config, "Transcoding");
50         // read the entries
51         QMap< QString, QString > profiles = transConfig.entryMap();
52         QMapIterator<QString, QString> i(profiles);
53         connect(m_view.profile_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateParams(int)));
54         while (i.hasNext()) {
55             i.next();
56             m_view.profile_list->addItem(i.key(), i.value());
57         }
58     }
59
60     kDebug() << "//PARAMS: " << params << "\n\nNAME: " << newFile;
61
62     connect(m_view.button_start, SIGNAL(clicked()), this, SLOT(slotStartTransCode()));
63
64     m_transcodeProcess.setProcessChannelMode(QProcess::MergedChannels);
65     connect(&m_transcodeProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotShowTranscodeInfo()));
66     connect(&m_transcodeProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotTranscodeFinished(int, QProcess::ExitStatus)));
67
68     //adjustSize();
69 }
70
71 ClipTranscode::~ClipTranscode()
72 {
73     if (m_transcodeProcess.state() != QProcess::NotRunning) {
74         m_transcodeProcess.close();
75     }
76 }
77
78 void ClipTranscode::slotStartTransCode()
79 {
80     if (m_transcodeProcess.state() != QProcess::NotRunning) {
81         return;
82     }
83     QStringList parameters;
84     parameters << "-i" << m_view.source_url->url().path();
85
86     if (QFile::exists(m_view.dest_url->url().path())) {
87         if (KMessageBox::questionYesNo(this, i18n("File %1 already exists.\nDo you want to overwrite it?", m_view.dest_url->url().path())) == KMessageBox::No) return;
88         parameters << "-y";
89     }
90     m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Abort"));
91     QString params = m_view.params->toPlainText().simplified();
92     params = params.section(' ', 0, -2);
93     parameters << params.split(' ') << m_view.dest_url->url().path();
94
95     kDebug() << "/// FFMPEG ARGS: " << parameters;
96
97     m_transcodeProcess.start("ffmpeg", parameters);
98     m_view.button_start->setEnabled(false);
99
100 }
101
102 void ClipTranscode::slotShowTranscodeInfo()
103 {
104     QString log = QString(m_transcodeProcess.readAll());
105     //kDebug() << "//LOG: " << log;
106     m_view.log->setPlainText(log);
107 }
108
109 void ClipTranscode::slotTranscodeFinished(int exitCode, QProcess::ExitStatus exitStatus)
110 {
111     m_view.buttonBox->button(QDialogButtonBox::Abort)->setText(i18n("Close"));
112     m_view.button_start->setEnabled(true);
113
114     if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
115         m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding finished."));
116         if (m_view.auto_add->isChecked()) emit addClip(m_view.dest_url->url());
117         if (m_view.auto_close->isChecked()) accept();
118     } else {
119         m_view.log->setHtml(m_view.log->toPlainText() + "<br><b>" + i18n("Transcoding FAILED!"));
120     }
121
122     m_transcodeProcess.close();
123 }
124
125 void ClipTranscode::slotUpdateParams(int ix)
126 {
127     if (ix == -1) ix = m_view.profile_list->currentIndex();
128     QString fileName = m_view.source_url->url().path();
129     QString params = m_view.profile_list->itemData(ix).toString();
130     QString newFile = params.section(' ', -1).replace("%1", fileName);
131     KUrl dest(newFile);
132     m_view.dest_url->setUrl(dest);
133     m_view.params->setPlainText(params.simplified());
134 }
135
136 #include "cliptranscode.moc"
137
138