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));
94 if (KStandardDirs::findExe("dvdauthor").isEmpty()) m_errorMessage.append(i18n("<strong>Program %1 is required for the DVD wizard.</strong>", i18n("dvdauthor")));
95 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")));
96 if (m_errorMessage.isEmpty()) m_view.error_message->setVisible(false);
98 m_view.error_message->setText(m_errorMessage);
99 m_installCheck = 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->setMessageType(KMessageWidget::Warning);
122 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
123 m_warnMessage->setCloseButtonVisible(false);
124 m_warnMessage->addAction(m_transcodeAction);
125 QGridLayout *s = static_cast <QGridLayout*> (layout());
126 s->addWidget(m_warnMessage, 2, 0, 1, -1);
127 m_warnMessage->hide();
128 m_view.button_transcode->setHidden(true);
130 m_view.button_transcode->setDefaultAction(m_transcodeAction);
131 m_view.button_transcode->setEnabled(false);
137 DvdWizardVob::~DvdWizardVob()
139 delete m_capacityBar;
142 void DvdWizardVob::slotCheckProfiles()
144 bool conflict = false;
145 int comboProfile = m_view.dvd_profile->currentIndex();
146 for (int i = 0; i < m_vobList->topLevelItemCount(); i++) {
147 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
148 if (item->data(0, Qt::UserRole + 1).toInt() != comboProfile) {
153 m_transcodeAction->setEnabled(conflict);
158 #if KDE_IS_VERSION(4,7,0)
159 m_warnMessage->animatedHide();
161 if (m_installCheck) m_view.error_message->setVisible(false);
166 void DvdWizardVob::slotAddVobList(QList <QUrl>list)
168 foreach (const QUrl url, list) {
169 slotAddVobFile(KUrl(url), QString(), false);
175 void DvdWizardVob::slotAddVobFile(KUrl url, const QString &chapters, bool checkFormats)
177 if (url.isEmpty()) url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///projectfolder"), "video/mpeg", this, i18n("Add new video file"));
178 if (url.isEmpty()) return;
180 qint64 fileSize = f.size();
182 Mlt::Profile profile;
183 profile.set_explicit(false);
184 QTreeWidgetItem *item = new QTreeWidgetItem(m_vobList, QStringList() << url.path() << QString() << KIO::convertSize(fileSize));
185 item->setData(2, Qt::UserRole, fileSize);
186 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
187 item->setToolTip(0, url.path());
189 QString resource = url.path();
190 resource.prepend("avformat:");
191 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
192 if (producer && producer->is_valid() && !producer->is_blank()) {
193 //Mlt::Frame *frame = producer->get_frame();
195 profile.from_producer(*producer);
196 int width = 45.0 * profile.dar();
197 int swidth = 45.0 * profile.width() / profile.height();
198 if (width % 2 == 1) width++;
199 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
200 int playTime = producer->get_playtime();
201 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
202 item->setData(1, Qt::UserRole, playTime);
204 int aspect = profile.dar() * 100;
205 if (profile.height() == 576 && profile.fps() == 25.0) {
206 if (aspect > 150) standard = 1;
209 else if (profile.height() == 480 && qAbs(profile.fps() - 30000.0 / 1001) < 0.2) {
210 if (aspect > 150) standard = 3;
213 QString standardName;
216 standardName = i18n("NTSC 16:9");
219 standardName = i18n("NTSC 4:3");
222 standardName = i18n("PAL 16:9");
225 standardName = i18n("PAL 4:3");
228 standardName = i18n("Unknown");
230 standardName.append(QString(" | %1x%2, %3fps").arg(profile.width()).arg(profile.height()).arg(profile.fps()));
231 item->setData(0, Qt::UserRole, standardName);
232 item->setData(0, Qt::UserRole + 1, standard);
233 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
234 if (m_vobList->topLevelItemCount() == 1) {
235 // This is the first added movie, auto select DVD format
237 m_view.dvd_profile->blockSignals(true);
238 m_view.dvd_profile->setCurrentIndex(standard);
239 m_view.dvd_profile->blockSignals(false);
245 // Cannot load movie, reject
246 showError(i18n("The clip %1 is invalid.", url.fileName()));
248 if (producer) delete producer;
250 if (chapters.isEmpty() == false) {
251 item->setData(1, Qt::UserRole + 1, chapters);
253 else if (QFile::exists(url.path() + ".dvdchapter")) {
254 // insert chapters as children
255 QFile file(url.path() + ".dvdchapter");
256 if (file.open(QIODevice::ReadOnly)) {
258 if (doc.setContent(&file) == false) {
263 QDomNodeList chapters = doc.elementsByTagName("chapter");
264 QStringList chaptersList;
265 for (int j = 0; j < chapters.count(); j++) {
266 chaptersList.append(QString::number(chapters.at(j).toElement().attribute("time").toInt()));
268 item->setData(1, Qt::UserRole + 1, chaptersList.join(";"));
270 } else // Explicitly add a chapter at 00:00:00:00
271 item->setData(1, Qt::UserRole + 1, "0");
279 void DvdWizardVob::slotDeleteVobFile()
281 QTreeWidgetItem *item = m_vobList->currentItem();
282 if (item == NULL) return;
290 bool DvdWizardVob::isComplete() const
292 if (!m_installCheck) return false;
293 if (m_vobList->topLevelItemCount() == 0) return false;
297 void DvdWizardVob::setUrl(const QString &url)
299 slotAddVobFile(KUrl(url));
302 QStringList DvdWizardVob::selectedUrls() const
306 int max = m_vobList->topLevelItemCount();
308 if (m_view.use_intro->isChecked()) {
309 // First movie is only for intro
312 for (; i < max; i++) {
313 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
314 if (item) result.append(item->text(0));
320 QStringList DvdWizardVob::durations() const
324 int max = m_vobList->topLevelItemCount();
325 for (int i = 0; i < max; i++) {
326 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
327 if (item) result.append(QString::number(item->data(1, Qt::UserRole).toInt()));
332 QStringList DvdWizardVob::chapters() const
336 int max = m_vobList->topLevelItemCount();
337 for (int i = 0; i < max; i++) {
338 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
340 result.append(item->data(1, Qt::UserRole + 1).toString());
346 void DvdWizardVob::updateChapters(QMap <QString, QString> chaptersdata)
348 int max = m_vobList->topLevelItemCount();
349 for (int i = 0; i < max; i++) {
350 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
351 if (chaptersdata.contains(item->text(0))) item->setData(1, Qt::UserRole + 1, chaptersdata.value(item->text(0)));
355 int DvdWizardVob::duration(int ix) const
358 QTreeWidgetItem *item = m_vobList->topLevelItem(ix);
360 result = item->data(1, Qt::UserRole).toInt();
365 const QString DvdWizardVob::introMovie() const
368 if (m_view.use_intro->isChecked() && m_vobList->topLevelItemCount() > 0) url = m_vobList->topLevelItem(0)->text(0);
372 void DvdWizardVob::setUseIntroMovie(bool use)
374 m_view.use_intro->setChecked(use);
377 void DvdWizardVob::slotCheckVobList()
379 emit completeChanged();
380 int max = m_vobList->topLevelItemCount();
381 QTreeWidgetItem *item = m_vobList->currentItem();
383 if (item == NULL) hasItem = false;
384 m_view.button_delete->setEnabled(hasItem);
385 if (hasItem && m_vobList->indexOfTopLevelItem(item) == 0) m_view.button_up->setEnabled(false);
386 else m_view.button_up->setEnabled(hasItem);
387 if (hasItem && m_vobList->indexOfTopLevelItem(item) == max - 1) m_view.button_down->setEnabled(false);
388 else m_view.button_down->setEnabled(hasItem);
390 qint64 totalSize = 0;
391 for (int i = 0; i < max; i++) {
392 item = m_vobList->topLevelItem(i);
393 if (item) totalSize += (qint64) item->data(2, Qt::UserRole).toInt();
396 qint64 maxSize = (qint64) 47000 * 100000;
397 m_capacityBar->setValue(100 * totalSize / maxSize);
398 m_capacityBar->setText(KIO::convertSize(totalSize));
401 void DvdWizardVob::slotItemUp()
403 QTreeWidgetItem *item = m_vobList->currentItem();
404 if (item == NULL) return;
405 int index = m_vobList->indexOfTopLevelItem(item);
406 if (index == 0) return;
407 m_vobList->insertTopLevelItem(index - 1, m_vobList->takeTopLevelItem(index));
410 void DvdWizardVob::slotItemDown()
412 int max = m_vobList->topLevelItemCount();
413 QTreeWidgetItem *item = m_vobList->currentItem();
414 if (item == NULL) return;
415 int index = m_vobList->indexOfTopLevelItem(item);
416 if (index == max - 1) return;
417 m_vobList->insertTopLevelItem(index + 1, m_vobList->takeTopLevelItem(index));
420 DVDFORMAT DvdWizardVob::dvdFormat() const
422 return (DVDFORMAT) m_view.dvd_profile->currentIndex();
425 const QString DvdWizardVob::dvdProfile() const
428 switch (m_view.dvd_profile->currentIndex()) {
430 profile = "dv_pal_wide";
436 profile = "dv_ntsc_wide";
445 QString DvdWizardVob::getDvdProfile(DVDFORMAT format)
450 profile = "dv_pal_wide";
456 profile = "dv_ntsc_wide";
464 void DvdWizardVob::setProfile(const QString& profile)
466 if (profile == "dv_pal_wide") m_view.dvd_profile->setCurrentIndex(PAL_WIDE);
467 else if (profile == "dv_ntsc") m_view.dvd_profile->setCurrentIndex(NTSC);
468 else if (profile == "dv_ntsc_wide") m_view.dvd_profile->setCurrentIndex(NTSC_WIDE);
469 else m_view.dvd_profile->setCurrentIndex(PAL);
472 void DvdWizardVob::clear()
477 void DvdWizardVob::slotTranscodeFiles()
479 // Find transcoding infos related to selected DVD profile
480 KSharedConfigPtr config = KSharedConfig::openConfig("kdenlivetranscodingrc", KConfig::CascadeConfig);
481 KConfigGroup transConfig(config, "Transcoding");
483 QString profileEasyName;
486 switch (m_view.dvd_profile->currentIndex()) {
488 profileEasyName = "DVD PAL 16:9";
489 destSize = QSize(1024, 576);
490 finalSize = QSize(720, 576);
493 profileEasyName = "DVD NTSC 4:3";
494 destSize = QSize(640, 480);
495 finalSize = QSize(720, 480);
498 profileEasyName = "DVD NTSC 16:9";
499 destSize = QSize(853, 480);
500 finalSize = QSize(720, 480);
503 profileEasyName = "DVD PAL 4:3";
504 destSize = QSize(768, 576);
505 finalSize = QSize(720, 576);
507 QString params = transConfig.readEntry(profileEasyName);
509 // Transcode files that do not match selected profile
510 int max = m_vobList->topLevelItemCount();
511 int format = m_view.dvd_profile->currentIndex();
512 for (int i = 0; i < max; i++) {
513 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
514 if (item->data(0, Qt::UserRole + 1).toInt() != format) {
515 // File needs to be transcoded
516 m_transcodeAction->setEnabled(false);
517 QSize original = item->data(0, Qt::UserRole + 2).toSize();
518 double input_aspect= (double) original.width() / original.height();
519 QStringList postParams;
520 if (input_aspect > (double) destSize.width() / destSize.height()) {
522 int conv_height = (int) (destSize.width() / input_aspect);
523 int conv_pad = (int) (((double) (destSize.height() - conv_height)) / 2.0);
524 if (conv_pad %2 == 1) conv_pad --;
525 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);
528 int conv_width = (int) (destSize.height() * input_aspect);
529 int conv_pad = (int) (((double) (destSize.width() - conv_width)) / destSize.width() * finalSize.width() / 2.0);
530 if (conv_pad %2 == 1) conv_pad --;
531 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);
533 ClipTranscode *d = new ClipTranscode(KUrl::List () << KUrl(item->text(0)), params.section(';', 0, 0), postParams, i18n("Transcoding to DVD format"), true, this);
534 connect(d, SIGNAL(transcodedClip(KUrl,KUrl)), this, SLOT(slotTranscodedClip(KUrl, KUrl)));
535 d->slotStartTransCode();
542 void DvdWizardVob::slotTranscodedClip(KUrl src, KUrl transcoded)
544 if (transcoded.isEmpty()) {
545 // Transcoding canceled or failed
546 m_transcodeAction->setEnabled(true);
549 int max = m_vobList->topLevelItemCount();
550 for (int i = 0; i < max; i++) {
551 QTreeWidgetItem *item = m_vobList->topLevelItem(i);
552 if (KUrl(item->text(0)).path() == src.path()) {
553 // Replace movie with transcoded version
554 item->setText(0, transcoded.path());
556 QFile f(transcoded.path());
557 qint64 fileSize = f.size();
559 Mlt::Profile profile;
560 profile.set_explicit(false);
561 item->setText(2, KIO::convertSize(fileSize));
562 item->setData(2, Qt::UserRole, fileSize);
563 item->setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(60, 45));
564 item->setToolTip(0, transcoded.path());
566 QString resource = transcoded.path();
567 resource.prepend("avformat:");
568 Mlt::Producer *producer = new Mlt::Producer(profile, resource.toUtf8().data());
569 if (producer && producer->is_valid() && !producer->is_blank()) {
570 profile.from_producer(*producer);
571 int width = 45.0 * profile.dar();
572 int swidth = 45.0 * profile.width() / profile.height();
573 if (width % 2 == 1) width++;
574 item->setData(0, Qt::DecorationRole, QPixmap::fromImage(KThumb::getFrame(producer, 0, swidth, width, 45)));
575 int playTime = producer->get_playtime();
576 item->setText(1, Timecode::getStringTimecode(playTime, profile.fps()));
577 item->setData(1, Qt::UserRole, playTime);
579 int aspect = profile.dar() * 100;
580 if (profile.height() == 576) {
581 if (aspect > 150) standard = 1;
584 else if (profile.height() == 480) {
585 if (aspect > 150) standard = 3;
588 QString standardName;
591 standardName = i18n("NTSC 16:9");
594 standardName = i18n("NTSC 4:3");
597 standardName = i18n("PAL 16:9");
600 standardName = i18n("PAL 4:3");
603 standardName = i18n("Unknown");
605 item->setData(0, Qt::UserRole, standardName);
606 item->setData(0, Qt::UserRole + 1, standard);
607 item->setData(0, Qt::UserRole + 2, QSize(profile.dar() * profile.height(), profile.height()));
610 // Cannot load movie, reject
611 showError(i18n("The clip %1 is invalid.", transcoded.fileName()));
613 if (producer) delete producer;
621 void DvdWizardVob::showProfileError()
623 #if KDE_IS_VERSION(4,7,0)
624 m_warnMessage->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
625 m_warnMessage->setCloseButtonVisible(false);
626 m_warnMessage->addAction(m_transcodeAction);
627 m_warnMessage->animatedShow();
629 m_view.error_message->setText(i18n("Your clips do not match selected DVD format, transcoding required."));
630 m_view.error_message->setVisible(true);
634 void DvdWizardVob::showError(const QString error)
636 #if KDE_IS_VERSION(4,7,0)
637 m_warnMessage->setText(error);
638 m_warnMessage->setCloseButtonVisible(true);
639 m_warnMessage->removeAction(m_transcodeAction);
640 m_warnMessage->animatedShow();
642 m_view.error_message->setText(error);
643 m_view.error_message->setVisible(true);