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, const QString &profile, QWidget *parent): QWizard(parent), m_profile(profile) {
40 //setPixmap(QWizard::WatermarkPixmap, QPixmap(KStandardDirs::locate("appdata", "banner.png")));
41 setAttribute(Qt::WA_DeleteOnClose);
42 m_pageVob = new DvdWizardVob(this);
43 m_pageVob->setTitle(i18n("Select Files For Your Dvd"));
46 if (!url.isEmpty()) m_pageVob->setUrl(url);
49 if (m_profile.startsWith("dv_pal")) m_height = 576;
52 QWizardPage *page2 = new QWizardPage;
53 page2->setTitle(i18n("Create Dvd Menu"));
54 m_menu.setupUi(page2);
55 m_menu.play_text->setText(i18n("Play"));
56 m_scene = new QGraphicsScene(0, 0, m_width, m_height, this);
57 m_menu.menu_preview->setScene(m_scene);
59 // Create color background
60 m_color = new QGraphicsRectItem(0, 0, m_width, m_height);
61 m_color->setBrush(m_menu.background_color->color());
62 m_scene->addItem(m_color);
64 // create background image
65 m_background = new QGraphicsPixmapItem();
66 //m_scene->addItem(m_background);
69 m_button = new QGraphicsTextItem(m_menu.play_text->text());
70 QFont font = m_menu.font_family->currentFont();
71 font.setPixelSize(m_menu.font_size->value());
72 font.setStyleStrategy(QFont::NoAntialias);
73 m_button->setFont(font);
74 m_button->setDefaultTextColor(m_menu.text_color->color());
75 m_button->setZValue(2);
76 m_button->setFlags(QGraphicsItem::ItemIsMovable);
77 QRectF r = m_button->sceneBoundingRect();
78 m_button->setPos((m_width - r.width()) / 2, (m_height - r.height()) / 2);
79 m_scene->addItem(m_button);
81 // create safe zone rect
82 int safeW = m_width / 20;
83 int safeH = m_height / 20;
84 m_safeRect = new QGraphicsRectItem(safeW, safeH, m_width - 2 * safeW, m_height - 2 * safeH);
86 pen.setStyle(Qt::DashLine);
88 m_safeRect->setPen(pen);
89 m_safeRect->setZValue(3);
90 m_scene->addItem(m_safeRect);
93 //m_menu.menu_preview->resize(m_width / 2, m_height / 2);
94 m_menu.menu_preview->setSceneRect(0, 0, m_width, m_height);
96 matrix.scale(0.5, 0.5);
97 m_menu.menu_preview->setMatrix(matrix);
98 m_menu.menu_preview->setMinimumSize(m_width / 2 + 4, m_height / 2 + 8);
99 //m_menu.menu_preview->resizefitInView(0, 0, m_width, m_height);
101 connect(m_menu.is_color, SIGNAL(toggled(bool)), this, SLOT(checkBackground()));
102 connect(m_menu.play_text, SIGNAL(textChanged(const QString &)), this, SLOT(buildButton()));
103 connect(m_menu.text_color, SIGNAL(changed(const QColor &)), this, SLOT(buildButton()));
104 connect(m_menu.font_size, SIGNAL(valueChanged(int)), this, SLOT(buildButton()));
105 connect(m_menu.font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(buildButton()));
106 connect(m_menu.background_image, SIGNAL(textChanged(const QString &)), this, SLOT(buildImage()));
107 connect(m_menu.background_color, SIGNAL(changed(const QColor &)), this, SLOT(buildColor()));
108 connect(m_menu.create_menu, SIGNAL(toggled(bool)), m_menu.menu_box, SLOT(setEnabled(bool)));
109 m_menu.menu_box->setEnabled(false);
112 QWizardPage *page3 = new QWizardPage;
113 page3->setTitle(i18n("Dvd Image"));
114 m_iso.setupUi(page3);
115 m_iso.tmp_folder->setPath(KdenliveSettings::currenttmpfolder());
116 m_iso.iso_image->setPath(QDir::homePath() + "/untitled.iso");
117 m_iso.iso_image->setFilter("*.iso");
118 m_iso.iso_image->fileDialog()->setOperationMode(KFileDialog::Saving);
121 QWizardPage *page4 = new QWizardPage;
122 page4->setTitle(i18n("Creating Dvd Image"));
123 m_status.setupUi(page4);
126 connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(slotPageChanged(int)));
128 // connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
131 DvdWizard::~DvdWizard() {
141 void DvdWizard::slotPageChanged(int page) {
142 kDebug() << "// PAGE CHGD: " << page;
144 //if (m_vob.vob_1->text().isEmpty()) back();
147 KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
148 QTimer::singleShot(300, this, SLOT(generateDvd()));
152 void DvdWizard::buildColor() {
153 m_color->setBrush(m_menu.background_color->color());
156 void DvdWizard::buildImage() {
157 if (m_menu.background_image->url().isEmpty()) {
158 m_scene->removeItem(m_background);
162 if (!pix.load(m_menu.background_image->url().path())) {
163 m_scene->removeItem(m_background);
166 pix = pix.scaled(m_width, m_height);
167 m_background->setPixmap(pix);
168 if (!m_menu.is_color->isChecked()) m_scene->addItem(m_background);
171 void DvdWizard::checkBackground() {
172 if (m_menu.is_color->isChecked()) {
173 m_scene->addItem(m_color);
174 m_scene->removeItem(m_background);
176 m_scene->addItem(m_background);
177 m_scene->removeItem(m_color);
181 void DvdWizard::buildButton() {
182 m_button->setPlainText(m_menu.play_text->text());
183 QFont font = m_menu.font_family->currentFont();
184 font.setPixelSize(m_menu.font_size->value());
185 font.setStyleStrategy(QFont::NoAntialias);
186 m_button->setFont(font);
187 m_button->setDefaultTextColor(m_menu.text_color->color());
191 void DvdWizard::generateDvd() {
192 KTemporaryFile temp1;
193 temp1.setSuffix(".png");
194 //temp1.setAutoRemove(false);
197 KTemporaryFile temp2;
198 temp2.setSuffix(".png");
199 //temp2.setAutoRemove(false);
202 KTemporaryFile temp3;
203 temp3.setSuffix(".png");
204 //temp3.setAutoRemove(false);
207 KTemporaryFile temp4;
208 temp4.setSuffix(".png");
209 //temp4.setAutoRemove(false);
212 KTemporaryFile temp5;
213 temp5.setSuffix(".vob");
214 //temp5.setAutoRemove(false);
217 KTemporaryFile temp6;
218 temp6.setSuffix(".xml");
219 //temp6.setAutoRemove(false);
222 m_menuFile.setSuffix(".mpg");
223 m_menuFile.setAutoRemove(false);
226 m_authorFile.setSuffix(".xml");
227 m_authorFile.setAutoRemove(false);
230 QListWidgetItem *images = m_status.job_progress->item(0);
231 images->setIcon(KIcon("system-run"));
232 qApp->processEvents();
234 if (m_menu.create_menu->isChecked()) {
236 QImage img(m_width, m_height, QImage::Format_ARGB32);
238 m_scene->removeItem(m_safeRect);
239 m_scene->removeItem(m_color);
240 m_scene->removeItem(m_background);
241 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
244 img.save(temp1.fileName());
246 m_button->setDefaultTextColor(m_menu.selected_color->color());
248 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
251 img.save(temp2.fileName());
253 m_button->setDefaultTextColor(m_menu.highlighted_color->color());
255 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
258 img.save(temp3.fileName());
260 m_scene->removeItem(m_button);
261 if (m_menu.is_color->isChecked()) m_scene->addItem(m_color);
262 else m_scene->addItem(m_background);
266 m_scene->render(&p, QRectF(0, 0, m_width, m_height));
268 img.save(temp4.fileName());
271 images->setIcon(KIcon("dialog-ok"));
273 kDebug() << "/// STARTING MLT VOB CREATION";
275 // create menu vob file
276 QListWidgetItem *vobitem = m_status.job_progress->item(1);
277 vobitem->setIcon(KIcon("system-run"));
278 qApp->processEvents();
281 args.append("-profile");
282 args.append(m_profile);
283 args.append(temp4.fileName());
285 args.append("out=100");
286 args << "-consumer" << "avformat:" + temp5.fileName();
287 if (m_profile == "dv_pal") {
288 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";
289 } else if (m_profile == "dv_ntsc") {
290 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";
291 } else if (m_profile == "dv_pal_wide") {
292 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_wide";
293 } else if (m_profile == "dv_ntsc_wide") {
294 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_wide";
297 kDebug() << "MLT ARGS: " << args;
299 renderbg.start("inigo", args);
300 if (renderbg.waitForFinished()) {
301 if (renderbg.exitStatus() == QProcess::CrashExit) {
302 kDebug() << "/// RENDERING MENU vob crashed";
303 vobitem->setIcon(KIcon("dialog-close"));
307 kDebug() << "/// RENDERING MENU vob timed out";
308 vobitem->setIcon(KIcon("dialog-close"));
312 vobitem->setIcon(KIcon("dialog-ok"));
313 kDebug() << "/// STARTING SPUMUX";
315 // create xml spumux file
316 QListWidgetItem *spuitem = m_status.job_progress->item(2);
317 spuitem->setIcon(KIcon("system-run"));
318 qApp->processEvents();
320 QDomElement sub = doc.createElement("subpictures");
321 doc.appendChild(sub);
322 QDomElement stream = doc.createElement("stream");
323 sub.appendChild(stream);
324 QDomElement spu = doc.createElement("spu");
325 stream.appendChild(spu);
326 spu.setAttribute("force", "yes");
327 spu.setAttribute("start", "00:00:00.00");
328 spu.setAttribute("image", temp1.fileName());
329 spu.setAttribute("select", temp2.fileName());
330 spu.setAttribute("highlight", temp3.fileName());
331 spu.setAttribute("autooutline", "infer");
332 spu.setAttribute("outlinewidth", "12");
333 spu.setAttribute("autoorder", "rows");
335 QFile data(temp6.fileName());
336 if (data.open(QFile::WriteOnly)) {
337 QTextStream out(&data);
338 out << doc.toString();
342 kDebug() << " SPUMUX DATA: " << doc.toString();
345 args.append(temp6.fileName());
346 kDebug() << "SPM ARGS: " << args;
349 spumux.setStandardInputFile(temp5.fileName());
350 spumux.setStandardOutputFile(m_menuFile.fileName());
351 spumux.start("spumux", args);
352 spumux.setProcessChannelMode(QProcess::MergedChannels);
353 if (spumux.waitForFinished()) {
354 kDebug() << QString(spumux.readAll()).simplified();
355 if (spumux.exitStatus() == QProcess::CrashExit) {
356 kDebug() << "/// RENDERING SPUMUX MENU crashed";
357 spuitem->setIcon(KIcon("dialog-close"));
361 kDebug() << "/// RENDERING SPUMUX MENU timed out";
362 spuitem->setIcon(KIcon("dialog-close"));
366 spuitem->setIcon(KIcon("dialog-ok"));
367 kDebug() << "/// DONE: " << m_menuFile.fileName();
370 // create dvdauthor xml
371 QListWidgetItem *authitem = m_status.job_progress->item(3);
372 authitem->setIcon(KIcon("system-run"));
373 qApp->processEvents();
374 KIO::NetAccess::mkdir(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
377 QDomElement auth = dvddoc.createElement("dvdauthor");
378 auth.setAttribute("dest", m_iso.tmp_folder->url().path() + "/DVD");
379 dvddoc.appendChild(auth);
380 QDomElement vmgm = dvddoc.createElement("vmgm");
381 auth.appendChild(vmgm);
382 if (m_menu.create_menu->isChecked()) {
383 QDomElement menus = dvddoc.createElement("menus");
384 vmgm.appendChild(menus);
385 QDomElement pgc = dvddoc.createElement("pgc");
386 menus.appendChild(pgc);
387 QDomElement button = dvddoc.createElement("button");
388 QDomText nametext = dvddoc.createTextNode("jump title 1;");
389 button.appendChild(nametext);
390 pgc.appendChild(button);
391 QDomElement menuvob = dvddoc.createElement("vob");
392 menuvob.setAttribute("file", m_menuFile.fileName());
393 menuvob.setAttribute("pause", "inf");
394 pgc.appendChild(menuvob);
396 QDomElement titleset = dvddoc.createElement("titleset");
397 auth.appendChild(titleset);
398 QDomElement titles = dvddoc.createElement("titles");
399 titleset.appendChild(titles);
400 QDomElement pgc2 = dvddoc.createElement("pgc");
401 titles.appendChild(pgc2);
402 if (m_menu.create_menu->isChecked()) {
403 QDomElement post = dvddoc.createElement("post");
404 QDomText call = dvddoc.createTextNode("call vmgm menu 1;");
405 post.appendChild(call);
406 pgc2.appendChild(post);
409 QStringList voburls = m_pageVob->selectedUrls();
410 for (int i = 0; i < voburls.count(); i++) {
411 if (!voburls.at(i).isEmpty()) {
413 QDomElement vob = dvddoc.createElement("vob");
414 vob.setAttribute("file", voburls.at(i));
415 pgc2.appendChild(vob);
419 QFile data2(m_authorFile.fileName());
420 if (data2.open(QFile::WriteOnly)) {
421 QTextStream out(&data2);
422 out << dvddoc.toString();
428 args << "-x" << m_authorFile.fileName();
429 kDebug() << "// DVDAUTH ARGS: " << args;
430 QProcess *dvdauth = new QProcess(this);
431 connect(dvdauth, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slotRenderFinished(int, QProcess::ExitStatus)));
432 dvdauth->setProcessChannelMode(QProcess::MergedChannels);
433 dvdauth->start("dvdauthor", args);
437 void DvdWizard::slotRenderFinished(int exitCode, QProcess::ExitStatus status) {
438 QListWidgetItem *authitem = m_status.job_progress->item(3);
439 if (status == QProcess::CrashExit) {
440 kDebug() << "DVDAuthor process crashed";
441 authitem->setIcon(KIcon("dialog-close"));
444 authitem->setIcon(KIcon("dialog-ok"));
445 qApp->processEvents();
447 args << "-dvd-video" << "-v" << "-o" << m_iso.iso_image->url().path() << m_iso.tmp_folder->url().path() + "/DVD";
448 QProcess *mkiso = new QProcess(this);
449 connect(mkiso, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slotIsoFinished(int, QProcess::ExitStatus)));
450 mkiso->setProcessChannelMode(QProcess::MergedChannels);
451 QListWidgetItem *isoitem = m_status.job_progress->item(4);
452 isoitem->setIcon(KIcon("system-run"));
453 mkiso->start("mkisofs", args);
457 void DvdWizard::slotIsoFinished(int exitCode, QProcess::ExitStatus status) {
458 QListWidgetItem *isoitem = m_status.job_progress->item(4);
459 if (status == QProcess::CrashExit) {
460 m_authorFile.remove();
462 KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
463 kDebug() << "Iso process crashed";
464 isoitem->setIcon(KIcon("dialog-close"));
467 isoitem->setIcon(KIcon("dialog-ok"));
468 kDebug() << "ISO IMAGE " << m_iso.iso_image->url().path() << " Successfully created";
469 m_authorFile.remove();
471 KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
472 KMessageBox::information(this, i18n("Dvd iso image %1 successfully created.", m_iso.iso_image->url().path()));