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>
39 DvdTreeWidget::DvdTreeWidget(QWidget *parent) :
45 void DvdTreeWidget::dragEnterEvent(QDragEnterEvent * event ) {
46 if (event->mimeData()->hasUrls()) {
47 event->setDropAction(Qt::CopyAction);
48 event->setAccepted(true);
50 else QTreeWidget::dragEnterEvent(event);
53 void DvdTreeWidget::dragMoveEvent(QDragMoveEvent * event) {
54 event->acceptProposedAction();
57 void DvdTreeWidget::mouseDoubleClickEvent( QMouseEvent * )
62 void DvdTreeWidget::dropEvent(QDropEvent * event ) {
63 QList<QUrl> clips = event->mimeData()->urls();
68 DvdWizardVob::DvdWizardVob(QWidget *parent) :
73 m_view.button_add->setIcon(KIcon("list-add"));
74 m_view.button_delete->setIcon(KIcon("list-remove"));
75 m_view.button_up->setIcon(KIcon("go-up"));
76 m_view.button_down->setIcon(KIcon("go-down"));
77 m_vobList = new DvdTreeWidget(this);
78 QVBoxLayout *lay1 = new QVBoxLayout;
79 lay1->addWidget(m_vobList);
80 m_view.list_frame->setLayout(lay1);
81 m_vobList->setColumnCount(3);
82 m_vobList->setHeaderHidden(true);
84 connect(m_vobList, SIGNAL(addClips(QList<QUrl>)), this, SLOT(slotAddVobList(QList<QUrl>)));
85 connect(m_vobList, SIGNAL(addNewClip()), this, SLOT(slotAddVobFile()));
86 connect(m_view.button_add, SIGNAL(clicked()), this, SLOT(slotAddVobFile()));
87 connect(m_view.button_delete, SIGNAL(clicked()), this, SLOT(slotDeleteVobFile()));
88 connect(m_view.button_up, SIGNAL(clicked()), this, SLOT(slotItemUp()));
89 connect(m_view.button_down, SIGNAL(clicked()), this, SLOT(slotItemDown()));
90 connect(m_vobList, SIGNAL(itemSelectionChanged()), this, SLOT(slotCheckVobList()));
92 m_vobList->setIconSize(QSize(60, 45));
95 if (KStandardDirs::findExe("dvdauthor").isEmpty()) errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("dvdauthor")));
96 if (KStandardDirs::findExe("mkisofs").isEmpty() && KStandardDirs::findExe("genisoimage").isEmpty()) errorMessage.append(i18n("<strong>Program %1 or %2 is required for the DVD wizard.</strong>", i18n("mkisofs"), i18n("genisoimage")));
97 if (!errorMessage.isEmpty()) {
98 m_view.button_add->setEnabled(false);
99 m_view.dvd_profile->setEnabled(false);
102 m_view.dvd_profile->addItems(QStringList() << i18n("PAL 4:3") << i18n("PAL 16:9") << i18n("NTSC 4:3") << i18n("NTSC 16:9"));
104 connect(m_view.dvd_profile, SIGNAL(activated(int)), this, SLOT(slotCheckProfiles()));
105 m_vobList->header()->setStretchLastSection(false);
106 m_vobList->header()->setResizeMode(0, QHeaderView::Stretch);
107 m_vobList->header()->setResizeMode(1, QHeaderView::Custom);
108 m_vobList->header()->setResizeMode(2, QHeaderView::Custom);
110 m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline, this);
111 QHBoxLayout *lay = new QHBoxLayout;
112 lay->addWidget(m_capacityBar);
113 m_view.size_box->setLayout(lay);
115 m_vobList->setItemDelegate(new DvdViewDelegate(m_vobList));
116 m_transcodeAction = new QAction(i18n("Transcode"), this);
117 connect(m_transcodeAction, SIGNAL(triggered()), this, SLOT(slotTranscodeFiles()));
119 #if KDE_IS_VERSION(4,7,0)
120 m_warnMessage = new KMessageWidget;
121 m_warnMessage->setCloseButtonVisible(false);
122 QGridLayout *s = static_cast <QGridLayout*> (layout());
123 s->addWidget(m_warnMessage, 2, 0, 1, -1);
124 if (!errorMessage.isEmpty()) {
125 m_warnMessage->setMessageType(KMessageWidget::Error);
126 m_warnMessage->setText(errorMessage);
127 m_installCheck = false;
129 m_warnMessage->setMessageType(KMessageWidget::Warning);
130 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
131 m_warnMessage->addAction(m_transcodeAction);
132 m_warnMessage->hide();
134 m_view.button_transcode->setHidden(true);
136 m_view.button_transcode->setDefaultAction(m_transcodeAction);
137 m_view.button_transcode->setEnabled(false);
138 if (!errorMessage.isEmpty()) {
139 m_view.error_message->setText(errorMessage);
140 m_installCheck = false;
147 DvdWizardVob::~DvdWizardVob()
149 delete m_capacityBar;
152 void DvdWizardVob::slotCheckProfiles()
154 bool conflict = false;
155 int comboProfile = m_view.dvd_profile->currentIndex();
156 for (int i = 0; i < m_vobList->topLevelItemCount(); i++) {
157 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
158 if (item->data(0, Qt::UserRole + 1).toInt() != comboProfile) {
163 m_transcodeAction->setEnabled(conflict);
168 #if KDE_IS_VERSION(4,7,0)
169 m_warnMessage->animatedHide();
171 if (m_installCheck) m_view.error_message->setVisible(false);
176 void DvdWizardVob::slotAddVobList(QList <QUrl>list)
178 foreach (const QUrl url, list) {
179 slotAddVobFile(KUrl(url), QString(), false);
185 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters, bool checkFormats)
187 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
188 if (url.isEmpty()) return;
190 qint64 fileSize = f.size();
192 Mlt::Profile profile;
193 profile.set_explicit(false);
194 QTreeWidgetItem *item = new QTreeWidgetItem(m_vobList, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
195 item->setData(2, Qt::UserRole, fileSize);
196 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
197 item->setToolTip(0, url.path());
199 QString resource = url.path();
200 resource.prepend("avformat:");
201 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
202 if (producer && producer->is_valid() && !producer->is_blank()) {
203 //Mlt::Frame *frame = producer->get_frame();
205 profile.from_producer(*producer);
206 int width = 45.0 * profile.dar();
207 int swidth = 45.0 * profile.width() / profile.height();
208 if (width % 2 == 1) width++;
209 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
210 int playTime = producer->get_playtime();
211 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
212 item->setData(1, Qt::UserRole, playTime);
214 int aspect = profile.dar() * 100;
215 if (profile.height() == 576 && profile.fps() == 25.0) {
216 if (aspect > 150) standard = 1;
219 else if (profile.height() == 480 && qAbs(profile.fps() - 30000.0 / 1001) < 0.2) {
220 if (aspect > 150) standard = 3;
223 QString standardName;
226 standardName = i18n("NTSC 16:9");
229 standardName = i18n("NTSC 4:3");
232 standardName = i18n("PAL 16:9");
235 standardName = i18n("PAL 4:3");
238 standardName = i18n("Unknown");
240 standardName.append(QString(" | %1x%2, %3fps").arg(profile.width()).arg(profile.height()).arg(profile.fps()));
241 item->setData(0, Qt::UserRole, standardName);
242 item->setData(0, Qt::UserRole + 1, standard);
243 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
244 if (m_vobList->topLevelItemCount() == 1) {
245 // This is the first added movie, auto select DVD format
247 m_view.dvd_profile->blockSignals(true);
248 m_view.dvd_profile->setCurrentIndex(standard);
249 m_view.dvd_profile->blockSignals(false);
255 // Cannot load movie, reject
256 showError(i18n("The clip %1 is invalid.", url.fileName()));
258 if (producer) delete producer;
260 if (chapters.isEmpty() == false) {
261 item->setData(1, Qt::UserRole + 1, chapters);
263 else if (QFile::exists(url.path() + ".dvdchapter")) {
264 // insert chapters as children
265 QFile file(url.path() + ".dvdchapter");
266 if (file.open(QIODevice::ReadOnly)) {
268 if (doc.setContent(&file) == false) {
273 QDomNodeList chapters = doc.elementsByTagName("chapter");
274 QStringList chaptersList;
275 for (int j = 0; j < chapters.count(); j++) {
276 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
278 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
280 } else // Explicitly add a chapter at 00:00:00:00
281 item->setData(1, Qt::UserRole + 1, "0");
289 void DvdWizardVob::slotDeleteVobFile()
291 QTreeWidgetItem *item = m_vobList->currentItem();
292 if (item == NULL) return;
300 bool DvdWizardVob::isComplete() const
302 if (!m_installCheck) return false;
303 if (m_vobList->topLevelItemCount() == 0) return false;
307 void DvdWizardVob::setUrl(const QString &url)
309 slotAddVobFile(KUrl(url));
312 QStringList DvdWizardVob::selectedUrls() const
316 int max = m_vobList->topLevelItemCount();
318 if (m_view.use_intro->isChecked()) {
319 // First movie is only for intro
322 for (; i < max; i++) {
323 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
324 if (item) result.append(item->text(0));
330 QStringList DvdWizardVob::durations() const
334 int max = m_vobList->topLevelItemCount();
335 for (int i = 0; i < max; i++) {
336 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
337 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
342 QStringList DvdWizardVob::chapters() const
346 int max = m_vobList->topLevelItemCount();
347 for (int i = 0; i < max; i++) {
348 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
350 result.append(item->data(1, Qt::UserRole + 1).toString());
356 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
358 int max = m_vobList->topLevelItemCount();
359 for (int i = 0; i < max; i++) {
360 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
361 if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
365 int DvdWizardVob::duration(int ix) const
368 QTreeWidgetItem *item = m_vobList->topLevelItem(ix);
370 result = item->data(1, Qt::UserRole).toInt();
375 const QString DvdWizardVob::introMovie() const
378 if (m_view.use_intro->isChecked() && m_vobList->topLevelItemCount() > 0) url = m_vobList->topLevelItem(0)->text(0);
382 void DvdWizardVob::setUseIntroMovie(bool use)
384 m_view.use_intro->setChecked(use);
387 void DvdWizardVob::slotCheckVobList()
389 emit completeChanged();
390 int max = m_vobList->topLevelItemCount();
391 QTreeWidgetItem *item = m_vobList->currentItem();
393 if (item == NULL) hasItem = false;
394 m_view.button_delete->setEnabled(hasItem);
395 if (hasItem && m_vobList->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
396 else m_view.button_up->setEnabled(hasItem);
397 if (hasItem && m_vobList->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
398 else m_view.button_down->setEnabled(hasItem);
400 qint64 totalSize = 0;
401 for (int i = 0; i < max; i++) {
402 item = m_vobList->topLevelItem(i);
403 if (item) totalSize += (qint64) item->data(2, Qt::UserRole).toInt();
406 qint64 maxSize = (qint64) 47000 * 100000;
407 m_capacityBar->setValue(100 * totalSize / maxSize);
408 m_capacityBar->setText(KIO::convertSize(totalSize));
411 void DvdWizardVob::slotItemUp()
413 QTreeWidgetItem *item = m_vobList->currentItem();
414 if (item == NULL) return;
415 int index = m_vobList->indexOfTopLevelItem(item);
416 if (index == 0) return;
417 m_vobList->insertTopLevelItem(index - 1, m_vobList->takeTopLevelItem(index));
420 void DvdWizardVob::slotItemDown()
422 int max = m_vobList->topLevelItemCount();
423 QTreeWidgetItem *item = m_vobList->currentItem();
424 if (item == NULL) return;
425 int index = m_vobList->indexOfTopLevelItem(item);
426 if (index == max - 1) return;
427 m_vobList->insertTopLevelItem(index + 1, m_vobList->takeTopLevelItem(index));
430 DVDFORMAT DvdWizardVob::dvdFormat() const
432 return (DVDFORMAT) m_view.dvd_profile->currentIndex();
435 const QString DvdWizardVob::dvdProfile() const
438 switch (m_view.dvd_profile->currentIndex()) {
440 profile = "dv_pal_wide";
446 profile = "dv_ntsc_wide";
455 QString DvdWizardVob::getDvdProfile(DVDFORMAT format)
460 profile = "dv_pal_wide";
466 profile = "dv_ntsc_wide";
474 void DvdWizardVob::setProfile(const QString& profile)
476 if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(PAL_WIDE);
477 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(NTSC);
478 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(NTSC_WIDE);
479 else m_view.dvd_profile->setCurrentIndex(PAL);
482 void DvdWizardVob::clear()
487 void DvdWizardVob::slotTranscodeFiles()
489 // Find transcoding infos related to selected DVD profile
490 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc", KConfig::CascadeConfig);
491 KConfigGroup transConfig(config, "Transcoding");
493 QString profileEasyName;
496 switch (m_view.dvd_profile->currentIndex()) {
498 profileEasyName = "DVD PAL 16:9";
499 destSize = QSize(1024, 576);
500 finalSize = QSize(720, 576);
503 profileEasyName = "DVD NTSC 4:3";
504 destSize = QSize(640, 480);
505 finalSize = QSize(720, 480);
508 profileEasyName = "DVD NTSC 16:9";
509 destSize = QSize(853, 480);
510 finalSize = QSize(720, 480);
513 profileEasyName = "DVD PAL 4:3";
514 destSize = QSize(768, 576);
515 finalSize = QSize(720, 576);
517 QString params = transConfig.readEntry(profileEasyName);
519 // Transcode files that do not match selected profile
520 int max = m_vobList->topLevelItemCount();
521 int format = m_view.dvd_profile->currentIndex();
522 for (int i = 0; i < max; i++) {
523 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
524 if (item->data(0, Qt::UserRole + 1).toInt() != format) {
525 // File needs to be transcoded
526 m_transcodeAction->setEnabled(false);
527 QSize original = item->data(0, Qt::UserRole + 2).toSize();
528 double input_aspect= (double) original.width() / original.height();
529 QStringList postParams;
530 if (input_aspect > (double) destSize.width() / destSize.height()) {
532 int conv_height = (int) (destSize.width() / input_aspect);
533 int conv_pad = (int) (((double) (destSize.height() - conv_height)) / 2.0);
534 if (conv_pad %2 == 1) conv_pad --;
535 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);
538 int conv_width = (int) (destSize.height() * input_aspect);
539 int conv_pad = (int) (((double) (destSize.width() - conv_width)) / destSize.width() * finalSize.width() / 2.0);
540 if (conv_pad %2 == 1) conv_pad --;
541 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);
543 ClipTranscode *d = new ClipTranscode(KUrl::List () << KUrl(item->text(0)), params.section(';', 0, 0), postParams, i18n("Transcoding to DVD format"), true, this);
544 connect(d, SIGNAL(transcodedClip(KUrl,KUrl)), this, SLOT(slotTranscodedClip(KUrl, KUrl)));
545 d->slotStartTransCode();
552 void DvdWizardVob::slotTranscodedClip(KUrl src, KUrl transcoded)
554 if (transcoded.isEmpty()) {
555 // Transcoding canceled or failed
556 m_transcodeAction->setEnabled(true);
559 int max = m_vobList->topLevelItemCount();
560 for (int i = 0; i < max; i++) {
561 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
562 if (KUrl(item->text(0)).path() == src.path()) {
563 // Replace movie with transcoded version
564 item->setText(0, transcoded.path());
566 QFile f(transcoded.path());
567 qint64 fileSize = f.size();
569 Mlt::Profile profile;
570 profile.set_explicit(false);
571 item->setText(2, KIO::convertSize(fileSize));
572 item->setData(2, Qt::UserRole, fileSize);
573 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
574 item->setToolTip(0, transcoded.path());
576 QString resource = transcoded.path();
577 resource.prepend("avformat:");
578 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
579 if (producer && producer->is_valid() && !producer->is_blank()) {
580 profile.from_producer(*producer);
581 int width = 45.0 * profile.dar();
582 int swidth = 45.0 * profile.width() / profile.height();
583 if (width % 2 == 1) width++;
584 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
585 int playTime = producer->get_playtime();
586 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
587 item->setData(1, Qt::UserRole, playTime);
589 int aspect = profile.dar() * 100;
590 if (profile.height() == 576) {
591 if (aspect > 150) standard = 1;
594 else if (profile.height() == 480) {
595 if (aspect > 150) standard = 3;
598 QString standardName;
601 standardName = i18n("NTSC 16:9");
604 standardName = i18n("NTSC 4:3");
607 standardName = i18n("PAL 16:9");
610 standardName = i18n("PAL 4:3");
613 standardName = i18n("Unknown");
615 item->setData(0, Qt::UserRole, standardName);
616 item->setData(0, Qt::UserRole + 1, standard);
617 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
620 // Cannot load movie, reject
621 showError(i18n("The clip %1 is invalid.", transcoded.fileName()));
623 if (producer) delete producer;
631 void DvdWizardVob::showProfileError()
633 #if KDE_IS_VERSION(4,7,0)
634 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
635 m_warnMessage->setCloseButtonVisible(false);
636 m_warnMessage->addAction(m_transcodeAction);
637 m_warnMessage->animatedShow();
639 m_view.error_message->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
640 m_view.error_message->setVisible(true);
644 void DvdWizardVob::showError(const QString error)
646 #if KDE_IS_VERSION(4,7,0)
647 m_warnMessage->setText(error);
648 m_warnMessage->setCloseButtonVisible(true);
649 m_warnMessage->removeAction(m_transcodeAction);
650 m_warnMessage->animatedShow();
652 m_view.error_message->setText(error);
653 m_view.error_message->setVisible(true);