]> git.sesse.net Git - kdenlive/blob - src/dvdwizard.cpp
Open dvd wizard after rendering if user asked it
[kdenlive] / src / dvdwizard.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20 #include <QLabel>
21 #include <QFile>
22 #include <QXmlStreamWriter>
23 #include <QApplication>
24 #include <QTimer>
25 #include <QGraphicsView>
26 #include <QDomDocument>
27
28 #include <KStandardDirs>
29 #include <KLocale>
30 #include <KFileDialog>
31 #include <kmimetype.h>
32 #include <KIO/NetAccess>
33 #include <KMessageBox>
34
35 #include "kdenlivesettings.h"
36 #include "profilesdialog.h"
37 #include "dvdwizard.h"
38
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     QWizardPage *page1 = new QWizardPage;
43     page1->setTitle(i18n("Select Files For Your Dvd"));
44     m_vob.setupUi(page1);
45     addPage(page1);
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);
52
53     m_width = 720;
54     if (m_profile.startsWith("dv_pal")) m_height = 576;
55     else m_height = 480;
56
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);
63
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);
68
69     // create background image
70     m_background = new QGraphicsPixmapItem();
71     //m_scene->addItem(m_background);
72
73     // create menu button
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);
85
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);
90     QPen pen(Qt::red);
91     pen.setStyle(Qt::DashLine);
92     pen.setWidth(3);
93     m_safeRect->setPen(pen);
94     m_safeRect->setZValue(3);
95     m_scene->addItem(m_safeRect);
96
97     addPage(page2);
98     //m_menu.menu_preview->resize(m_width / 2, m_height / 2);
99     m_menu.menu_preview->setSceneRect(0, 0, m_width, m_height);
100     QMatrix matrix;
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);
105
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);
115
116
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);
124     addPage(page3);
125
126     QWizardPage *page4 = new QWizardPage;
127     page4->setTitle(i18n("Creating Dvd Image"));
128     m_status.setupUi(page4);
129     addPage(page4);
130
131     connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(slotPageChanged(int)));
132
133 //    connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
134 }
135
136 DvdWizard::~DvdWizard() {
137     delete m_button;
138     delete m_color;
139     delete m_safeRect;
140     delete m_background;
141     delete m_scene;
142     m_menuFile.remove();
143 }
144
145
146 void DvdWizard::slotPageChanged(int page) {
147     kDebug() << "// PAGE CHGD: " << page;
148     if (page == 1) {
149         //if (m_vob.vob_1->text().isEmpty()) back();
150     }
151     if (page == 3) {
152         KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
153         QTimer::singleShot(300, this, SLOT(generateDvd()));
154     }
155 }
156
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";
161     if (count == 1) {
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();
172             delete vob;
173         }
174     } else {
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 &)));
180     }
181 }
182
183 void DvdWizard::buildColor() {
184     m_color->setBrush(m_menu.background_color->color());
185 }
186
187 void DvdWizard::buildImage() {
188     if (m_menu.background_image->url().isEmpty()) {
189         m_scene->removeItem(m_background);
190         return;
191     }
192     QPixmap pix;
193     if (!pix.load(m_menu.background_image->url().path())) {
194         m_scene->removeItem(m_background);
195         return;
196     }
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);
200 }
201
202 void DvdWizard::checkBackground() {
203     if (m_menu.is_color->isChecked()) {
204         m_scene->addItem(m_color);
205         m_scene->removeItem(m_background);
206     } else {
207         m_scene->addItem(m_background);
208         m_scene->removeItem(m_color);
209     }
210 }
211
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());
219 }
220
221
222 void DvdWizard::generateDvd() {
223     KTemporaryFile temp1;
224     temp1.setSuffix(".png");
225     //temp1.setAutoRemove(false);
226     temp1.open();
227
228     KTemporaryFile temp2;
229     temp2.setSuffix(".png");
230     //temp2.setAutoRemove(false);
231     temp2.open();
232
233     KTemporaryFile temp3;
234     temp3.setSuffix(".png");
235     //temp3.setAutoRemove(false);
236     temp3.open();
237
238     KTemporaryFile temp4;
239     temp4.setSuffix(".png");
240     //temp4.setAutoRemove(false);
241     temp4.open();
242
243     KTemporaryFile temp5;
244     temp5.setSuffix(".vob");
245     //temp5.setAutoRemove(false);
246     temp5.open();
247
248     KTemporaryFile temp6;
249     temp6.setSuffix(".xml");
250     //temp6.setAutoRemove(false);
251     temp6.open();
252
253     m_menuFile.setSuffix(".mpg");
254     m_menuFile.setAutoRemove(false);
255     m_menuFile.open();
256
257     m_authorFile.setSuffix(".xml");
258     m_authorFile.setAutoRemove(false);
259     m_authorFile.open();
260
261     QListWidgetItem *images =  m_status.job_progress->item(0);
262     images->setIcon(KIcon("system-run"));
263     qApp->processEvents();
264
265     if (m_menu.create_menu->isChecked()) {
266
267         QImage img(m_width, m_height, QImage::Format_ARGB32);
268         QPainter p(&img);
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));
273         p.end();
274         img.setNumColors(4);
275         img.save(temp1.fileName());
276
277         m_button->setDefaultTextColor(m_menu.selected_color->color());
278         p.begin(&img);
279         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
280         p.end();
281         img.setNumColors(4);
282         img.save(temp2.fileName());
283
284         m_button->setDefaultTextColor(m_menu.highlighted_color->color());
285         p.begin(&img);
286         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
287         p.end();
288         img.setNumColors(4);
289         img.save(temp3.fileName());
290
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);
294
295
296         p.begin(&img);
297         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
298         p.end();
299         img.save(temp4.fileName());
300
301
302         images->setIcon(KIcon("dialog-ok"));
303
304         kDebug() << "/// STARTING MLT VOB CREATION";
305
306         // create menu vob file
307         QListWidgetItem *vobitem =  m_status.job_progress->item(1);
308         vobitem->setIcon(KIcon("system-run"));
309         qApp->processEvents();
310
311         QStringList args;
312         args.append("-profile");
313         args.append(m_profile);
314         args.append(temp4.fileName());
315         args.append("in=0");
316         args.append("out=100");
317         args << "-consumer" << "avformat:" + temp5.fileName();
318         if (m_profile == "dv_pal") {
319             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";
320         } else if (m_profile == "dv_ntsc") {
321             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";
322         } else if (m_profile == "dv_pal_wide") {
323             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";
324         } else if (m_profile == "dv_ntsc_wide") {
325             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";
326         }
327
328         kDebug() << "MLT ARGS: " << args;
329         QProcess renderbg;
330         renderbg.start("inigo", args);
331         if (renderbg.waitForFinished()) {
332             if (renderbg.exitStatus() == QProcess::CrashExit) {
333                 kDebug() << "/// RENDERING MENU vob crashed";
334                 vobitem->setIcon(KIcon("dialog-close"));
335                 return;
336             }
337         } else {
338             kDebug() << "/// RENDERING MENU vob timed out";
339             vobitem->setIcon(KIcon("dialog-close"));
340             return;
341         }
342
343         vobitem->setIcon(KIcon("dialog-ok"));
344         kDebug() << "/// STARTING SPUMUX";
345
346         // create xml spumux file
347         QListWidgetItem *spuitem =  m_status.job_progress->item(2);
348         spuitem->setIcon(KIcon("system-run"));
349         qApp->processEvents();
350         QDomDocument doc;
351         QDomElement sub = doc.createElement("subpictures");
352         doc.appendChild(sub);
353         QDomElement stream = doc.createElement("stream");
354         sub.appendChild(stream);
355         QDomElement spu = doc.createElement("spu");
356         stream.appendChild(spu);
357         spu.setAttribute("force", "yes");
358         spu.setAttribute("start", "00:00:00.00");
359         spu.setAttribute("image", temp1.fileName());
360         spu.setAttribute("select", temp2.fileName());
361         spu.setAttribute("highlight", temp3.fileName());
362         spu.setAttribute("autooutline", "infer");
363         spu.setAttribute("outlinewidth", "12");
364         spu.setAttribute("autoorder", "rows");
365
366         QFile data(temp6.fileName());
367         if (data.open(QFile::WriteOnly)) {
368             QTextStream out(&data);
369             out << doc.toString();
370         }
371         data.close();
372
373         kDebug() << " SPUMUX DATA: " << doc.toString();
374
375         args.clear();
376         args.append(temp6.fileName());
377         kDebug() << "SPM ARGS: " << args;
378
379         QProcess spumux;
380         spumux.setStandardInputFile(temp5.fileName());
381         spumux.setStandardOutputFile(m_menuFile.fileName());
382         spumux.start("spumux", args);
383         spumux.setProcessChannelMode(QProcess::MergedChannels);
384         if (spumux.waitForFinished()) {
385             kDebug() << QString(spumux.readAll()).simplified();
386             if (spumux.exitStatus() == QProcess::CrashExit) {
387                 kDebug() << "/// RENDERING SPUMUX MENU crashed";
388                 spuitem->setIcon(KIcon("dialog-close"));
389                 return;
390             }
391         } else {
392             kDebug() << "/// RENDERING SPUMUX MENU timed out";
393             spuitem->setIcon(KIcon("dialog-close"));
394             return;
395         }
396
397         spuitem->setIcon(KIcon("dialog-ok"));
398         kDebug() << "/// DONE: " << m_menuFile.fileName();
399     }
400
401     // create dvdauthor xml
402     QListWidgetItem *authitem =  m_status.job_progress->item(3);
403     authitem->setIcon(KIcon("system-run"));
404     qApp->processEvents();
405     KIO::NetAccess::mkdir(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
406
407     QDomDocument dvddoc;
408     QDomElement auth = dvddoc.createElement("dvdauthor");
409     auth.setAttribute("dest", m_iso.tmp_folder->url().path() + "/DVD");
410     dvddoc.appendChild(auth);
411     QDomElement vmgm = dvddoc.createElement("vmgm");
412     auth.appendChild(vmgm);
413     if (m_menu.create_menu->isChecked()) {
414         QDomElement menus = dvddoc.createElement("menus");
415         vmgm.appendChild(menus);
416         QDomElement pgc = dvddoc.createElement("pgc");
417         menus.appendChild(pgc);
418         QDomElement button = dvddoc.createElement("button");
419         QDomText nametext = dvddoc.createTextNode("jump title 1;");
420         button.appendChild(nametext);
421         pgc.appendChild(button);
422         QDomElement menuvob = dvddoc.createElement("vob");
423         menuvob.setAttribute("file", m_menuFile.fileName());
424         menuvob.setAttribute("pause", "inf");
425         pgc.appendChild(menuvob);
426     }
427     QDomElement titleset = dvddoc.createElement("titleset");
428     auth.appendChild(titleset);
429     QDomElement titles = dvddoc.createElement("titles");
430     titleset.appendChild(titles);
431     QDomElement pgc2 = dvddoc.createElement("pgc");
432     titles.appendChild(pgc2);
433     if (m_menu.create_menu->isChecked()) {
434         QDomElement post = dvddoc.createElement("post");
435         QDomText call = dvddoc.createTextNode("call vmgm menu 1;");
436         post.appendChild(call);
437         pgc2.appendChild(post);
438     }
439     QList<KUrlRequester *> allUrls = m_vob.vob_list->findChildren<KUrlRequester *>();
440     for (int i = 0; i < allUrls.count(); i++) {
441         if (!allUrls.at(i)->url().path().isEmpty()) {
442             // Add vob entry
443             QDomElement vob = dvddoc.createElement("vob");
444             vob.setAttribute("file", allUrls.at(i)->url().path());
445             pgc2.appendChild(vob);
446         }
447     }
448
449     QFile data2(m_authorFile.fileName());
450     if (data2.open(QFile::WriteOnly)) {
451         QTextStream out(&data2);
452         out << dvddoc.toString();
453     }
454     data2.close();
455
456
457     QStringList args;
458     args << "-x" << m_authorFile.fileName();
459     kDebug() << "// DVDAUTH ARGS: " << args;
460     QProcess *dvdauth = new QProcess(this);
461     connect(dvdauth, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slotRenderFinished(int, QProcess::ExitStatus)));
462     dvdauth->setProcessChannelMode(QProcess::MergedChannels);
463     dvdauth->start("dvdauthor", args);
464
465 }
466
467 void DvdWizard::slotRenderFinished(int exitCode, QProcess::ExitStatus status) {
468     QListWidgetItem *authitem =  m_status.job_progress->item(3);
469     if (status == QProcess::CrashExit) {
470         kDebug() << "DVDAuthor process crashed";
471         authitem->setIcon(KIcon("dialog-close"));
472         return;
473     }
474     authitem->setIcon(KIcon("dialog-ok"));
475     qApp->processEvents();
476     QStringList args;
477     args << "-dvd-video" << "-v" << "-o" << m_iso.iso_image->url().path() << m_iso.tmp_folder->url().path() + "/DVD";
478     QProcess *mkiso = new QProcess(this);
479     connect(mkiso, SIGNAL(finished(int , QProcess::ExitStatus)), this, SLOT(slotIsoFinished(int, QProcess::ExitStatus)));
480     mkiso->setProcessChannelMode(QProcess::MergedChannels);
481     QListWidgetItem *isoitem =  m_status.job_progress->item(4);
482     isoitem->setIcon(KIcon("system-run"));
483     mkiso->start("mkisofs", args);
484
485 }
486
487 void DvdWizard::slotIsoFinished(int exitCode, QProcess::ExitStatus status) {
488     QListWidgetItem *isoitem =  m_status.job_progress->item(4);
489     if (status == QProcess::CrashExit) {
490         m_authorFile.remove();
491         m_menuFile.remove();
492         KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
493         kDebug() << "Iso process crashed";
494         isoitem->setIcon(KIcon("dialog-close"));
495         return;
496     }
497     isoitem->setIcon(KIcon("dialog-ok"));
498     kDebug() << "ISO IMAGE " << m_iso.iso_image->url().path() << " Successfully created";
499     m_authorFile.remove();
500     m_menuFile.remove();
501     KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
502     KMessageBox::information(this, i18n("Dvd iso image %1 successfully created.", m_iso.iso_image->url().path()));
503
504 }
505
506