1 /***************************************************************************
2 * Copyright (C) 2009 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include "dvdwizardvob.h"
23 #include "cliptranscode.h"
25 #include <mlt++/Mlt.h>
27 #include <KUrlRequester>
29 #include <KStandardDirs>
31 #include <KFileDialog>
33 #include <QHBoxLayout>
34 #include <QDomDocument>
35 #include <QTreeWidgetItem>
36 #include <QHeaderView>
38 DvdTreeWidget::DvdTreeWidget(QWidget *parent) :
44 void DvdTreeWidget::dragEnterEvent(QDragEnterEvent * event ) {
45 if (event->mimeData()->hasUrls()) {
46 event->setDropAction(Qt::CopyAction);
47 event->setAccepted(true);
49 else QTreeWidget::dragEnterEvent(event);
52 void DvdTreeWidget::dragMoveEvent(QDragMoveEvent * event) {
53 event->acceptProposedAction();
56 void DvdTreeWidget::mouseDoubleClickEvent( QMouseEvent * )
61 void DvdTreeWidget::dropEvent(QDropEvent * event ) {
62 QList<QUrl> clips = event->mimeData()->urls();
67 DvdWizardVob::DvdWizardVob(QWidget *parent) :
72 m_view.intro_vob->setEnabled(false);
73 m_view.intro_vob->setFilter("video/mpeg");
74 m_view.button_add->setIcon(KIcon("list-add"));
75 m_view.button_delete->setIcon(KIcon("list-remove"));
76 m_view.button_up->setIcon(KIcon("go-up"));
77 m_view.button_down->setIcon(KIcon("go-down"));
78 m_vobList = new DvdTreeWidget(this);
79 QVBoxLayout *lay1 = new QVBoxLayout;
80 lay1->addWidget(m_vobList);
81 m_view.list_frame->setLayout(lay1);
82 m_vobList->setColumnCount(3);
83 m_vobList->setHeaderHidden(true);
85 connect(m_vobList, SIGNAL(addClips(QList<QUrl>)), this, SLOT(slotAddVobList(QList<QUrl>)));
86 connect(m_vobList, SIGNAL(addNewClip()), this, SLOT(slotAddVobFile()));
88 connect(m_view.use_intro, SIGNAL(toggled(bool)), m_view.intro_vob, SLOT(setEnabled(bool)));
89 connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddVobFile()));
90 connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteVobFile()));
91 connect(m_view.button_up, SIGNAL(clicked()), this, SLOT(slotItemUp()));
92 connect(m_view.button_down, SIGNAL(clicked()), this, SLOT(slotItemDown()));
93 connect(m_vobList, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckVobList()));
95 m_vobList->setIconSize(QSize(60, 45));
97 if (KStandardDirs::findExe("dvdauthor").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("dvdauthor")));
98 if (KStandardDirs::findExe("mkisofs").isEmpty() && KStandardDirs::findExe("genisoimage").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 or %2 is required for the DVD wizard.</strong>", i18n("mkisofs"), i18n("genisoimage")));
99 if (m_errorMessage.isEmpty()) m_view.error_message->setVisible(false);
101 m_view.error_message->setText(m_errorMessage);
102 m_installCheck = false;
105 m_view.dvd_profile->addItems(QStringList() << i18n("PAL 4:3") << i18n("PAL 16:9") << i18n("NTSC 4:3") << i18n("NTSC 16:9"));
107 connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(slotCheckProfiles()));
108 m_vobList->header()->setStretchLastSection(false);
109 m_vobList->header()->setResizeMode(0, QHeaderView::Stretch);
110 m_vobList->header()->setResizeMode(1, QHeaderView::Custom);
111 m_vobList->header()->setResizeMode(2, QHeaderView::Custom);
113 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
114 QHBoxLayout *lay = new QHBoxLayout;
115 lay->addWidget(m_capacityBar);
116 m_view.size_box->setLayout(lay);
118 m_vobList->setItemDelegate(new DvdViewDelegate(m_vobList));
119 m_transcodeAction = new QAction(i18n("Transcode"), this);
120 connect(m_transcodeAction, SIGNAL(triggered()), this, SLOT(slotTranscodeFiles()));
122 #if KDE_IS_VERSION(4,7,0)
123 m_warnMessage = new KMessageWidget;
124 m_warnMessage->setMessageType(KMessageWidget::Warning);
125 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
126 m_warnMessage->setCloseButtonVisible(false);
127 m_warnMessage->addAction(m_transcodeAction);
128 QGridLayout *s = static_cast <QGridLayout*> (layout());
129 s->addWidget(m_warnMessage, 3, 0, 1, -1);
130 m_warnMessage->hide();
131 m_view.button_transcode->setHidden(true);
133 m_view.button_transcode->setDefaultAction(m_transcodeAction);
134 m_view.button_transcode->setEnabled(false);
140 DvdWizardVob::~DvdWizardVob()
142 delete m_capacityBar;
145 void DvdWizardVob::slotCheckProfiles()
147 bool conflict = false;
148 int comboProfile = m_view.dvd_profile->currentIndex();
149 for (int i = 0; i < m_vobList->topLevelItemCount(); i++) {
150 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
151 if (item->data(0, Qt::UserRole + 1).toInt() != comboProfile) {
156 m_transcodeAction->setEnabled(conflict);
161 #if KDE_IS_VERSION(4,7,0)
162 m_warnMessage->animatedHide();
164 if (m_installCheck) m_view.error_message->setVisible(false);
169 void DvdWizardVob::slotAddVobList(QList <QUrl>list)
171 foreach (const QUrl url, list) {
172 slotAddVobFile(KUrl(url), QString(), false);
178 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters, bool checkFormats)
180 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
181 if (url.isEmpty()) return;
183 qint64 fileSize = f.size();
185 Mlt::Profile profile;
186 profile.set_explicit(false);
187 QTreeWidgetItem *item = new QTreeWidgetItem(m_vobList, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
188 item->setData(2, Qt::UserRole, fileSize);
189 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
190 item->setToolTip(0, url.path());
192 QString resource = url.path();
193 resource.prepend("avformat:");
194 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
195 if (producer && producer->is_valid() && !producer->is_blank()) {
196 //Mlt::Frame *frame = producer->get_frame();
198 profile.from_producer(*producer);
199 int width = 45.0 * profile.dar();
200 int swidth = 45.0 * profile.width() / profile.height();
201 if (width % 2 == 1) width++;
202 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
203 int playTime = producer->get_playtime();
204 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
205 item->setData(1, Qt::UserRole, playTime);
207 int aspect = profile.dar() * 100;
208 if (profile.height() == 576) {
209 if (aspect > 150) standard = 1;
212 else if (profile.height() == 480) {
213 if (aspect > 150) standard = 3;
216 QString standardName;
219 standardName = i18n("NTSC 16:9");
222 standardName = i18n("NTSC 4:3");
225 standardName = i18n("PAL 16:9");
228 standardName = i18n("PAL 4:3");
231 standardName = i18n("Unknown");
233 item->setData(0, Qt::UserRole, standardName);
234 item->setData(0, Qt::UserRole + 1, standard);
235 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
236 if (m_vobList->topLevelItemCount() == 1) {
237 // This is the first added movie, auto select DVD format
239 m_view.dvd_profile->blockSignals(true);
240 m_view.dvd_profile->setCurrentIndex(standard);
241 m_view.dvd_profile->blockSignals(false);
247 // Cannot load movie, reject
248 showError(i18n("The clip %1 is invalid.", url.fileName()));
250 if (producer) delete producer;
252 if (chapters.isEmpty() == false) {
253 item->setData(1, Qt::UserRole + 1, chapters);
255 else if (QFile::exists(url.path() + ".dvdchapter")) {
256 // insert chapters as children
257 QFile file(url.path() + ".dvdchapter");
258 if (file.open(QIODevice::ReadOnly)) {
260 if (doc.setContent(&file) == false) {
265 QDomNodeList chapters = doc.elementsByTagName("chapter");
266 QStringList chaptersList;
267 for (int j = 0; j < chapters.count(); j++) {
268 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
270 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
272 } else // Explicitly add a chapter at 00:00:00:00
273 item->setData(1, Qt::UserRole + 1, "0");
281 void DvdWizardVob::slotDeleteVobFile()
283 QTreeWidgetItem *item = m_vobList->currentItem();
284 if (item == NULL) return;
292 bool DvdWizardVob::isComplete() const
294 if (!m_installCheck) return false;
295 if (m_vobList->topLevelItemCount() == 0) return false;
299 void DvdWizardVob::setUrl(const QString &url)
301 slotAddVobFile(KUrl(url));
304 QStringList DvdWizardVob::selectedUrls() const
308 int max = m_vobList->topLevelItemCount();
309 for (int i = 0; i < max; i++) {
310 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
311 if (item) result.append(item->text(0));
317 QStringList DvdWizardVob::durations() const
321 int max = m_vobList->topLevelItemCount();
322 for (int i = 0; i < max; i++) {
323 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
324 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
329 QStringList DvdWizardVob::chapters() const
333 int max = m_vobList->topLevelItemCount();
334 for (int i = 0; i < max; i++) {
335 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
337 result.append(item->data(1, Qt::UserRole + 1).toString());
343 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
345 int max = m_vobList->topLevelItemCount();
346 for (int i = 0; i < max; i++) {
347 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
348 if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
352 int DvdWizardVob::duration(int ix) const
355 QTreeWidgetItem *item = m_vobList->topLevelItem(ix);
357 result = item->data(1, Qt::UserRole).toInt();
363 QString DvdWizardVob::introMovie() const
365 if (!m_view.use_intro->isChecked()) return QString();
366 return m_view.intro_vob->url().path();
369 void DvdWizardVob::setIntroMovie(const QString& path)
371 m_view.intro_vob->setUrl(KUrl(path));
372 m_view.use_intro->setChecked(path.isEmpty() == false);
376 void DvdWizardVob::slotCheckVobList()
378 emit completeChanged();
379 int max = m_vobList->topLevelItemCount();
380 QTreeWidgetItem *item = m_vobList->currentItem();
382 if (item == NULL) hasItem = false;
383 m_view.button_delete->setEnabled(hasItem);
384 if (hasItem && m_vobList->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
385 else m_view.button_up->setEnabled(hasItem);
386 if (hasItem && m_vobList->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
387 else m_view.button_down->setEnabled(hasItem);
389 qint64 totalSize = 0;
390 for (int i = 0; i < max; i++) {
391 item = m_vobList->topLevelItem(i);
392 if (item) totalSize += (qint64) item->data(2, Qt::UserRole).toInt();
395 qint64 maxSize = (qint64) 47000 * 100000;
396 m_capacityBar->setValue(100 * totalSize / maxSize);
397 m_capacityBar->setText(KIO::convertSize(totalSize));
400 void DvdWizardVob::slotItemUp()
402 QTreeWidgetItem *item = m_vobList->currentItem();
403 if (item == NULL) return;
404 int index = m_vobList->indexOfTopLevelItem(item);
405 if (index == 0) return;
406 m_vobList->insertTopLevelItem(index - 1, m_vobList->takeTopLevelItem(index));
409 void DvdWizardVob::slotItemDown()
411 int max = m_vobList->topLevelItemCount();
412 QTreeWidgetItem *item = m_vobList->currentItem();
413 if (item == NULL) return;
414 int index = m_vobList->indexOfTopLevelItem(item);
415 if (index == max - 1) return;
416 m_vobList->insertTopLevelItem(index + 1, m_vobList->takeTopLevelItem(index));
419 DVDFORMAT DvdWizardVob::dvdFormat() const
421 return (DVDFORMAT) m_view.dvd_profile->currentIndex();
424 const QString DvdWizardVob::dvdProfile() const
427 switch (m_view.dvd_profile->currentIndex()) {
429 profile = "dv_pal_wide";
435 profile = "dv_ntsc_wide";
444 QString DvdWizardVob::getDvdProfile(DVDFORMAT format)
449 profile = "dv_pal_wide";
455 profile = "dv_ntsc_wide";
463 void DvdWizardVob::setProfile(const QString& profile)
465 if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(PAL_WIDE);
466 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(NTSC);
467 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(NTSC_WIDE);
468 else m_view.dvd_profile->setCurrentIndex(PAL);
471 void DvdWizardVob::clear()
476 void DvdWizardVob::slotTranscodeFiles()
478 // Find transcoding infos related to selected DVD profile
479 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc");
480 KConfigGroup transConfig(config, "Transcoding");
482 QString profileEasyName;
485 switch (m_view.dvd_profile->currentIndex()) {
487 profileEasyName = "DVD PAL 16:9";
488 destSize = QSize(1024, 576);
489 finalSize = QSize(720, 576);
492 profileEasyName = "DVD NTSC 4:3";
493 destSize = QSize(640, 480);
494 finalSize = QSize(720, 480);
497 profileEasyName = "DVD NTSC 16:9";
498 destSize = QSize(853, 480);
499 finalSize = QSize(720, 480);
502 profileEasyName = "DVD PAL 4:3";
503 destSize = QSize(768, 576);
504 finalSize = QSize(720, 576);
506 QString params = transConfig.readEntry(profileEasyName);
508 // Transcode files that do not match selected profile
509 int max = m_vobList->topLevelItemCount();
510 int format = m_view.dvd_profile->currentIndex();
511 for (int i = 0; i < max; i++) {
512 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
513 if (item->data(0, Qt::UserRole + 1).toInt() != format) {
514 // File needs to be transcoded
515 m_transcodeAction->setEnabled(false);
516 QSize original = item->data(0, Qt::UserRole + 2).toSize();
517 double input_aspect= (double) original.width() / original.height();
518 QStringList postParams;
519 if (input_aspect > (double) destSize.width() / destSize.height()) {
521 int conv_height = (int) (destSize.width() / input_aspect);
522 int conv_pad = (int) (((double) (destSize.height() - conv_height)) / 2.0);
523 if (conv_pad %2 == 1) conv_pad --;
524 postParams << "-vf" << QString("scale=%1:%2,pad=%3:%4:0:%5,setdar=%6").arg(finalSize.width()).arg(destSize.height() - 2 * conv_pad).arg(finalSize.width()).arg(finalSize.height()).arg(conv_pad).arg(input_aspect);
527 int conv_width = (int) (destSize.height() * input_aspect);
528 int conv_pad = (int) (((double) (destSize.width() - conv_width)) / destSize.width() * finalSize.width() / 2.0);
529 if (conv_pad %2 == 1) conv_pad --;
530 postParams << "-vf" << QString("scale=%1:%2,pad=%3:%4:%5:0,setdar=%6").arg(finalSize.width() - 2 * conv_pad).arg(destSize.height()).arg(finalSize.width()).arg(finalSize.height()).arg(conv_pad).arg(input_aspect);
532 ClipTranscode *d = new ClipTranscode(KUrl::List () << KUrl(item->text(0)), params.section(';', 0, 0), postParams, i18n("Transcoding to DVD format"), true, this);
533 connect(d, SIGNAL(transcodedClip(KUrl,KUrl)), this, SLOT(slotTranscodedClip(KUrl, KUrl)));
539 void DvdWizardVob::slotTranscodedClip(KUrl src, KUrl transcoded)
541 int max = m_vobList->topLevelItemCount();
542 for (int i = 0; i < max; i++) {
543 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
544 if (KUrl(item->text(0)).path() == src.path()) {
545 // Replace movie with transcoded version
546 item->setText(0, transcoded.path());
548 QFile f(transcoded.path());
549 qint64 fileSize = f.size();
551 Mlt::Profile profile;
552 profile.set_explicit(false);
553 item->setText(2, KIO::convertSize(fileSize));
554 item->setData(2, Qt::UserRole, fileSize);
555 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
556 item->setToolTip(0, transcoded.path());
558 QString resource = transcoded.path();
559 resource.prepend("avformat:");
560 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
561 if (producer && producer->is_valid() && !producer->is_blank()) {
562 profile.from_producer(*producer);
563 int width = 45.0 * profile.dar();
564 int swidth = 45.0 * profile.width() / profile.height();
565 if (width % 2 == 1) width++;
566 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
567 int playTime = producer->get_playtime();
568 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
569 item->setData(1, Qt::UserRole, playTime);
571 int aspect = profile.dar() * 100;
572 if (profile.height() == 576) {
573 if (aspect > 150) standard = 1;
576 else if (profile.height() == 480) {
577 if (aspect > 150) standard = 3;
580 QString standardName;
583 standardName = i18n("NTSC 16:9");
586 standardName = i18n("NTSC 4:3");
589 standardName = i18n("PAL 16:9");
592 standardName = i18n("PAL 4:3");
595 standardName = i18n("Unknown");
597 item->setData(0, Qt::UserRole, standardName);
598 item->setData(0, Qt::UserRole + 1, standard);
599 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
602 // Cannot load movie, reject
603 showError(i18n("The clip %1 is invalid.", transcoded.fileName()));
605 if (producer) delete producer;
613 void DvdWizardVob::showProfileError()
615 #if KDE_IS_VERSION(4,7,0)
616 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
617 m_warnMessage->setCloseButtonVisible(false);
618 m_warnMessage->addAction(m_transcodeAction);
619 m_warnMessage->animatedShow();
621 m_view.error_message->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
622 m_view.error_message->setVisible(true);
626 void DvdWizardVob::showError(const QString error)
628 #if KDE_IS_VERSION(4,7,0)
629 m_warnMessage->setText(error);
630 m_warnMessage->setCloseButtonVisible(true);
631 m_warnMessage->removeAction(m_transcodeAction);
632 m_warnMessage->animatedShow();
634 m_view.error_message->setText(error);
635 m_view.error_message->setVisible(true);