]> git.sesse.net Git - kdenlive/blob - src/dvdwizard.cpp
New Dvd wizard (in menu File).
[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, 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"));
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_isPal) 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->text() + "/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)->text().isEmpty() && allUrls.at(count - 2)->text().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)->text().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         if (m_isPal) args.append("dv_pal");
314         else args.append("dv_ntsc");
315         args.append(temp4.fileName());
316         args.append("in=0");
317         args.append("out=100");
318         args << "-consumer" << "avformat:" + temp5.fileName();
319         if (m_isPal) {
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";
321         } else {
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";
323         }
324
325
326         kDebug() << "MLT ARGS: " << args;
327         QProcess renderbg;
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"));
333                 return;
334             }
335         } else {
336             kDebug() << "/// RENDERING MENU vob timed out";
337             vobitem->setIcon(KIcon("dialog-close"));
338             return;
339         }
340
341         vobitem->setIcon(KIcon("dialog-ok"));
342         kDebug() << "/// STARTING SPUMUX";
343
344         // create xml spumux file
345         QListWidgetItem *spuitem =  m_status.job_progress->item(2);
346         spuitem->setIcon(KIcon("system-run"));
347         qApp->processEvents();
348         QDomDocument doc;
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");
363
364         QFile data(temp6.fileName());
365         if (data.open(QFile::WriteOnly)) {
366             QTextStream out(&data);
367             out << doc.toString();
368         }
369         data.close();
370
371         kDebug() << " SPUMUX DATA: " << doc.toString();
372
373         args.clear();
374         args.append(temp6.fileName());
375         kDebug() << "SPM ARGS: " << args;
376
377         QProcess spumux;
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"));
387                 return;
388             }
389         } else {
390             kDebug() << "/// RENDERING SPUMUX MENU timed out";
391             spuitem->setIcon(KIcon("dialog-close"));
392             return;
393         }
394
395         spuitem->setIcon(KIcon("dialog-ok"));
396         kDebug() << "/// DONE: " << m_menuFile.fileName();
397     }
398
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->text() + "/DVD"), this);
404
405     QDomDocument dvddoc;
406     QDomElement auth = dvddoc.createElement("dvdauthor");
407     auth.setAttribute("dest", m_iso.tmp_folder->text() + "/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);
424     }
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);
436     }
437     QList<KUrlRequester *> allUrls = m_vob.vob_list->findChildren<KUrlRequester *>();
438     for (int i = 0; i < allUrls.count(); i++) {
439         if (!allUrls.at(i)->text().isEmpty()) {
440             // Add vob entry
441             QDomElement vob = dvddoc.createElement("vob");
442             vob.setAttribute("file", allUrls.at(i)->text());
443             pgc2.appendChild(vob);
444         }
445     }
446
447     QFile data2(m_authorFile.fileName());
448     if (data2.open(QFile::WriteOnly)) {
449         QTextStream out(&data2);
450         out << dvddoc.toString();
451     }
452     data2.close();
453
454
455     QStringList args;
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);
462
463 }
464
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"));
470         return;
471     }
472     authitem->setIcon(KIcon("dialog-ok"));
473     qApp->processEvents();
474     QStringList args;
475     args << "-dvd-video" << "-v" << "-o" << m_iso.iso_image->text() << m_iso.tmp_folder->text() + "/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);
482
483 }
484
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();
489         m_menuFile.remove();
490         KIO::NetAccess::del(KUrl(m_iso.tmp_folder->text() + "/DVD"), this);
491         kDebug() << "Iso process crashed";
492         isoitem->setIcon(KIcon("dialog-close"));
493         return;
494     }
495     isoitem->setIcon(KIcon("dialog-ok"));
496     kDebug() << "ISO IMAGE " << m_iso.iso_image->text() << " Successfully created";
497     m_authorFile.remove();
498     m_menuFile.remove();
499     KIO::NetAccess::del(KUrl(m_iso.tmp_folder->text() + "/DVD"), this);
500     KMessageBox::information(this, i18n("Dvd iso image %1 successfully created.", m_iso.iso_image->text()));
501
502 }
503
504