1 /***************************************************************************
2 * Copyright (C) 2007 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 ***************************************************************************/
22 #include <QXmlStreamWriter>
23 #include <QApplication>
25 #include <QGraphicsView>
26 #include <QDomDocument>
28 #include <KStandardDirs>
30 #include <KFileDialog>
31 #include <kmimetype.h>
32 #include <KIO/NetAccess>
33 #include <KMessageBox>
35 #include "kdenlivesettings.h"
36 #include "profilesdialog.h"
37 #include "dvdwizard.h"
39 DvdWizard::DvdWizard(const QString &url, bool isPal, QWidget *parent): QWizard(parent), m_isPal(isPal) {
40 //setPixmap(QWizard::WatermarkPixmap, QPixmap(KStandardDirs::locate("appdata", "banner.png")));
41 setAttribute(Qt::WA_DeleteOnClose);
42 QWizardPage *page1 = new QWizardPage;
43 page1->setTitle(i18n("Select Files For Your Dvd"));
46 m_vob.intro_vob->setEnabled(false);
47 m_vob.vob_1->setFilter("video/mpeg");
48 m_vob.intro_vob->setFilter("video/mpeg");
49 connect(m_vob.use_intro, SIGNAL(toggled(bool)), m_vob.intro_vob, SLOT(setEnabled(bool)));
50 connect(m_vob.vob_1, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckVobList(const QString &)));
51 if (!url.isEmpty()) m_vob.vob_1->setPath(url);
54 if (m_isPal) m_height = 576;
57 QWizardPage *page2 = new QWizardPage;
58 page2->setTitle(i18n("Create Dvd Menu"));
59 m_menu.setupUi(page2);
60 m_menu.play_text->setText(i18n("Play"));
61 m_scene = new QGraphicsScene(0, 0, m_width, m_height, this);
62 m_menu.menu_preview->setScene(m_scene);
64 // Create color background
65 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
66 m_color->setBrush(m_menu.background_color->color());
67 m_scene->addItem(m_color);
69 // create background image
70 m_background = new QGraphicsPixmapItem();
71 //m_scene->addItem(m_background);
74 m_button = new QGraphicsTextItem(m_menu.play_text->text());
75 QFont font = m_menu.font_family->currentFont();
76 font.setPixelSize(m_menu.font_size->value());
77 font.setStyleStrategy(QFont::NoAntialias);
78 m_button->setFont(font);
79 m_button->setDefaultTextColor(m_menu.text_color->color());
80 m_button->setZValue(2);
81 m_button->setFlags(QGraphicsItem::ItemIsMovable);
82 QRectF r = m_button->sceneBoundingRect();
83 m_button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
84 m_scene->addItem(m_button);
86 // create safe zone rect
87 int safeW = m_width / 20;
88 int safeH = m_height / 20;
89 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
91 pen.setStyle(Qt::DashLine);
93 m_safeRect->setPen(pen);
94 m_safeRect->setZValue(3);
95 m_scene->addItem(m_safeRect);
98 //m_menu.menu_preview->resize(m_width / 2, m_height / 2);
99 m_menu.menu_preview->setSceneRect(0, 0, m_width, m_height);
101 matrix.scale(0.5, 0.5);
102 m_menu.menu_preview->setMatrix(matrix);
103 m_menu.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
104 //m_menu.menu_preview->resizefitInView(0, 0, m_width, m_height);
106 connect(m_menu.is_color, SIGNAL(toggled(bool)), this, SLOT(checkBackground()));
107 connect(m_menu.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
108 connect(m_menu.text_color, SIGNAL(changed(const QColor &)), this, SLOT(buildButton()));
109 connect(m_menu.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
110 connect(m_menu.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
111 connect(m_menu.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
112 connect(m_menu.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
113 connect(m_menu.create_menu, SIGNAL(toggled(bool)), m_menu.menu_box, SLOT(setEnabled(bool)));
114 m_menu.menu_box->setEnabled(false);
117 QWizardPage *page3 = new QWizardPage;
118 page3->setTitle(i18n("Dvd Image"));
119 m_iso.setupUi(page3);
120 m_iso.tmp_folder->setPath(KdenliveSettings::currenttmpfolder());
121 m_iso.iso_image->setPath(QDir::homePath() + "/untitled.iso");
122 m_iso.iso_image->setFilter("*.iso");
123 m_iso.iso_image->fileDialog()->setOperationMode(KFileDialog::Saving);
126 QWizardPage *page4 = new QWizardPage;
127 page4->setTitle(i18n("Creating Dvd Image"));
128 m_status.setupUi(page4);
131 connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(slotPageChanged(int)));
133 // connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
136 DvdWizard::~DvdWizard() {
146 void DvdWizard::slotPageChanged(int page) {
147 kDebug() << "// PAGE CHGD: " << page;
149 //if (m_vob.vob_1->text().isEmpty()) back();
152 KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
153 QTimer::singleShot(300, this, SLOT(generateDvd()));
157 void DvdWizard::slotCheckVobList(const QString &text) {
158 QList<KUrlRequester *> allUrls = m_vob.vob_list->findChildren<KUrlRequester *>();
159 int count = allUrls.count();
160 kDebug() << "// FOUND " << count << " URLS";
162 if (text.isEmpty()) return;
163 // insert second urlrequester
164 KUrlRequester *vob = new KUrlRequester(this);
165 vob->setFilter("video/mpeg");
166 m_vob.vob_list->layout()->addWidget(vob);
167 connect(vob, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckVobList(const QString &)));
168 } else if (text.isEmpty()) {
169 if (allUrls.at(count - 1)->url().path().isEmpty() && allUrls.at(count - 2)->url().path().isEmpty()) {
170 // The last 2 urlrequesters are empty, remove last one
171 KUrlRequester *vob = allUrls.takeLast();
175 if (allUrls.at(count - 1)->url().path().isEmpty()) return;
176 KUrlRequester *vob = new KUrlRequester(this);
177 vob->setFilter("video/mpeg");
178 m_vob.vob_list->layout()->addWidget(vob);
179 connect(vob, SIGNAL(textChanged(const QString &)), this, SLOT(slotCheckVobList(const QString &)));
183 void DvdWizard::buildColor() {
184 m_color->setBrush(m_menu.background_color->color());
187 void DvdWizard::buildImage() {
188 if (m_menu.background_image->url().isEmpty()) {
189 m_scene->removeItem(m_background);
193 if (!pix.load(m_menu.background_image->url().path())) {
194 m_scene->removeItem(m_background);
197 pix = pix.scaled(m_width, m_height);
198 m_background->setPixmap(pix);
199 if (!m_menu.is_color->isChecked()) m_scene->addItem(m_background);
202 void DvdWizard::checkBackground() {
203 if (m_menu.is_color->isChecked()) {
204 m_scene->addItem(m_color);
205 m_scene->removeItem(m_background);
207 m_scene->addItem(m_background);
208 m_scene->removeItem(m_color);
212 void DvdWizard::buildButton() {
213 m_button->setPlainText(m_menu.play_text->text());
214 QFont font = m_menu.font_family->currentFont();
215 font.setPixelSize(m_menu.font_size->value());
216 font.setStyleStrategy(QFont::NoAntialias);
217 m_button->setFont(font);
218 m_button->setDefaultTextColor(m_menu.text_color->color());
222 void DvdWizard::generateDvd() {
223 KTemporaryFile temp1;
224 temp1.setSuffix(".png");
225 //temp1.setAutoRemove(false);
228 KTemporaryFile temp2;
229 temp2.setSuffix(".png");
230 //temp2.setAutoRemove(false);
233 KTemporaryFile temp3;
234 temp3.setSuffix(".png");
235 //temp3.setAutoRemove(false);
238 KTemporaryFile temp4;
239 temp4.setSuffix(".png");
240 //temp4.setAutoRemove(false);
243 KTemporaryFile temp5;
244 temp5.setSuffix(".vob");
245 //temp5.setAutoRemove(false);
248 KTemporaryFile temp6;
249 temp6.setSuffix(".xml");
250 //temp6.setAutoRemove(false);
253 m_menuFile.setSuffix(".mpg");
254 m_menuFile.setAutoRemove(false);
257 m_authorFile.setSuffix(".xml");
258 m_authorFile.setAutoRemove(false);
261 QListWidgetItem *images = m_status.job_progress->item(0);
262 images->setIcon(KIcon("system-run"));
263 qApp->processEvents();
265 if (m_menu.create_menu->isChecked()) {
267 QImage img(m_width, m_height, QImage::Format_ARGB32);
269 m_scene->removeItem(m_safeRect);
270 m_scene->removeItem(m_color);
271 m_scene->removeItem(m_background);
272 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
275 img.save(temp1.fileName());
277 m_button->setDefaultTextColor(m_menu.selected_color->color());
279 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
282 img.save(temp2.fileName());
284 m_button->setDefaultTextColor(m_menu.highlighted_color->color());
286 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
289 img.save(temp3.fileName());
291 m_scene->removeItem(m_button);
292 if (m_menu.is_color->isChecked()) m_scene->addItem(m_color);
293 else m_scene->addItem(m_background);
297 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
299 img.save(temp4.fileName());
302 images->setIcon(KIcon("dialog-ok"));
304 kDebug() << "/// STARTING MLT VOB CREATION";
306 // create menu vob file
307 QListWidgetItem *vobitem = m_status.job_progress->item(1);
308 vobitem->setIcon(KIcon("system-run"));
309 qApp->processEvents();
312 args.append("-profile");
313 if (m_isPal) args.append("dv_pal");
314 else args.append("dv_ntsc");
315 args.append(temp4.fileName());
317 args.append("out=100");
318 args << "-consumer" << "avformat:" + temp5.fileName();
320 args << "f=dvd" << "vcodec=mpeg2video" << "acodec=ac3" << "b=5000k" << "maxrate=8000k" << "minrate=0" << "bufsize=1835008" << "mux_packet_s=2048" << "mux_rate=10080000" << "ab=192k" << "ar=48000" << "s=720x576" << "g=15" << "me_range=63" << "trellis=1" << "profile=dv_pal";
322 args << "f=dvd" << "vcodec=mpeg2video" << "acodec=ac3" << "b=6000k" << "maxrate=9000k" << "minrate=0" << "bufsize=1835008" << "mux_packet_s=2048" << "mux_rate=10080000" << "ab=192k" << "ar=48000" << "s=720x480" << "g=18" << "me_range=63" << "trellis=1" << "profile=dv_ntsc";
326 kDebug() << "MLT ARGS: " << args;
328 renderbg.start("inigo", args);
329 if (renderbg.waitForFinished()) {
330 if (renderbg.exitStatus() == QProcess::CrashExit) {
331 kDebug() << "/// RENDERING MENU vob crashed";
332 vobitem->setIcon(KIcon("dialog-close"));
336 kDebug() << "/// RENDERING MENU vob timed out";
337 vobitem->setIcon(KIcon("dialog-close"));
341 vobitem->setIcon(KIcon("dialog-ok"));
342 kDebug() << "/// STARTING SPUMUX";
344 // create xml spumux file
345 QListWidgetItem *spuitem = m_status.job_progress->item(2);
346 spuitem->setIcon(KIcon("system-run"));
347 qApp->processEvents();
349 QDomElement sub = doc.createElement("subpictures");
350 doc.appendChild(sub);
351 QDomElement stream = doc.createElement("stream");
352 sub.appendChild(stream);
353 QDomElement spu = doc.createElement("spu");
354 stream.appendChild(spu);
355 spu.setAttribute("force", "yes");
356 spu.setAttribute("start", "00:00:00.00");
357 spu.setAttribute("image", temp1.fileName());
358 spu.setAttribute("select", temp2.fileName());
359 spu.setAttribute("highlight", temp3.fileName());
360 spu.setAttribute("autooutline", "infer");
361 spu.setAttribute("outlinewidth", "12");
362 spu.setAttribute("autoorder", "rows");
364 QFile data(temp6.fileName());
365 if (data.open(QFile::WriteOnly)) {
366 QTextStream out(&data);
367 out << doc.toString();
371 kDebug() << " SPUMUX DATA: " << doc.toString();
374 args.append(temp6.fileName());
375 kDebug() << "SPM ARGS: " << args;
378 spumux.setStandardInputFile(temp5.fileName());
379 spumux.setStandardOutputFile(m_menuFile.fileName());
380 spumux.start("spumux", args);
381 spumux.setProcessChannelMode(QProcess::MergedChannels);
382 if (spumux.waitForFinished()) {
383 kDebug() << QString(spumux.readAll()).simplified();
384 if (spumux.exitStatus() == QProcess::CrashExit) {
385 kDebug() << "/// RENDERING SPUMUX MENU crashed";
386 spuitem->setIcon(KIcon("dialog-close"));
390 kDebug() << "/// RENDERING SPUMUX MENU timed out";
391 spuitem->setIcon(KIcon("dialog-close"));
395 spuitem->setIcon(KIcon("dialog-ok"));
396 kDebug() << "/// DONE: " << m_menuFile.fileName();
399 // create dvdauthor xml
400 QListWidgetItem *authitem = m_status.job_progress->item(3);
401 authitem->setIcon(KIcon("system-run"));
402 qApp->processEvents();
403 KIO::NetAccess::mkdir(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
406 QDomElement auth = dvddoc.createElement("dvdauthor");
407 auth.setAttribute("dest", m_iso.tmp_folder->url().path() + "/DVD");
408 dvddoc.appendChild(auth);
409 QDomElement vmgm = dvddoc.createElement("vmgm");
410 auth.appendChild(vmgm);
411 if (m_menu.create_menu->isChecked()) {
412 QDomElement menus = dvddoc.createElement("menus");
413 vmgm.appendChild(menus);
414 QDomElement pgc = dvddoc.createElement("pgc");
415 menus.appendChild(pgc);
416 QDomElement button = dvddoc.createElement("button");
417 QDomText nametext = dvddoc.createTextNode("jump title 1;");
418 button.appendChild(nametext);
419 pgc.appendChild(button);
420 QDomElement menuvob = dvddoc.createElement("vob");
421 menuvob.setAttribute("file", m_menuFile.fileName());
422 menuvob.setAttribute("pause", "inf");
423 pgc.appendChild(menuvob);
425 QDomElement titleset = dvddoc.createElement("titleset");
426 auth.appendChild(titleset);
427 QDomElement titles = dvddoc.createElement("titles");
428 titleset.appendChild(titles);
429 QDomElement pgc2 = dvddoc.createElement("pgc");
430 titles.appendChild(pgc2);
431 if (m_menu.create_menu->isChecked()) {
432 QDomElement post = dvddoc.createElement("post");
433 QDomText call = dvddoc.createTextNode("call vmgm menu 1;");
434 post.appendChild(call);
435 pgc2.appendChild(post);
437 QList<KUrlRequester *> allUrls = m_vob.vob_list->findChildren<KUrlRequester *>();
438 for (int i = 0; i < allUrls.count(); i++) {
439 if (!allUrls.at(i)->url().path().isEmpty()) {
441 QDomElement vob = dvddoc.createElement("vob");
442 vob.setAttribute("file", allUrls.at(i)->url().path());
443 pgc2.appendChild(vob);
447 QFile data2(m_authorFile.fileName());
448 if (data2.open(QFile::WriteOnly)) {
449 QTextStream out(&data2);
450 out << dvddoc.toString();
456 args << "-x" << m_authorFile.fileName();
457 kDebug() << "// DVDAUTH ARGS: " << args;
458 QProcess *dvdauth = new QProcess(this);
459 connect(dvdauth, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slotRenderFinished(int, QProcess::ExitStatus)));
460 dvdauth->setProcessChannelMode(QProcess::MergedChannels);
461 dvdauth->start("dvdauthor", args);
465 void DvdWizard::slotRenderFinished(int exitCode, QProcess::ExitStatus status) {
466 QListWidgetItem *authitem = m_status.job_progress->item(3);
467 if (status == QProcess::CrashExit) {
468 kDebug() << "DVDAuthor process crashed";
469 authitem->setIcon(KIcon("dialog-close"));
472 authitem->setIcon(KIcon("dialog-ok"));
473 qApp->processEvents();
475 args << "-dvd-video" << "-v" << "-o" << m_iso.iso_image->url().path() << m_iso.tmp_folder->url().path() + "/DVD";
476 QProcess *mkiso = new QProcess(this);
477 connect(mkiso, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slotIsoFinished(int, QProcess::ExitStatus)));
478 mkiso->setProcessChannelMode(QProcess::MergedChannels);
479 QListWidgetItem *isoitem = m_status.job_progress->item(4);
480 isoitem->setIcon(KIcon("system-run"));
481 mkiso->start("mkisofs", args);
485 void DvdWizard::slotIsoFinished(int exitCode, QProcess::ExitStatus status) {
486 QListWidgetItem *isoitem = m_status.job_progress->item(4);
487 if (status == QProcess::CrashExit) {
488 m_authorFile.remove();
490 KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
491 kDebug() << "Iso process crashed";
492 isoitem->setIcon(KIcon("dialog-close"));
495 isoitem->setIcon(KIcon("dialog-ok"));
496 kDebug() << "ISO IMAGE " << m_iso.iso_image->url().path() << " Successfully created";
497 m_authorFile.remove();
499 KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
500 KMessageBox::information(this, i18n("Dvd iso image %1 successfully created.", m_iso.iso_image->url().path()));