]> git.sesse.net Git - kdenlive/blob - src/dvdwizard.cpp
start improving dvd wizard
[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     m_pageVob = new DvdWizardVob(this);
43     m_pageVob->setTitle(i18n("Select Files For Your Dvd"));
44     addPage(m_pageVob);
45
46     if (!url.isEmpty()) m_pageVob->setUrl(url);
47
48     m_width = 720;
49     if (m_profile.startsWith("dv_pal")) m_height = 576;
50     else m_height = 480;
51
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);
58
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);
63
64     // create background image
65     m_background = new QGraphicsPixmapItem();
66     //m_scene->addItem(m_background);
67
68     // create menu button
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);
80
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);
85     QPen pen(Qt::red);
86     pen.setStyle(Qt::DashLine);
87     pen.setWidth(3);
88     m_safeRect->setPen(pen);
89     m_safeRect->setZValue(3);
90     m_scene->addItem(m_safeRect);
91
92     addPage(page2);
93     //m_menu.menu_preview->resize(m_width / 2, m_height / 2);
94     m_menu.menu_preview->setSceneRect(0, 0, m_width, m_height);
95     QMatrix matrix;
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);
100
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);
110
111
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);
119     addPage(page3);
120
121     QWizardPage *page4 = new QWizardPage;
122     page4->setTitle(i18n("Creating Dvd Image"));
123     m_status.setupUi(page4);
124     addPage(page4);
125
126     connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(slotPageChanged(int)));
127
128 //    connect(m_standard.button_all, SIGNAL(toggled(bool)), this, SLOT(slotCheckStandard()));
129 }
130
131 DvdWizard::~DvdWizard() {
132     delete m_button;
133     delete m_color;
134     delete m_safeRect;
135     delete m_background;
136     delete m_scene;
137     m_menuFile.remove();
138 }
139
140
141 void DvdWizard::slotPageChanged(int page) {
142     kDebug() << "// PAGE CHGD: " << page;
143     if (page == 1) {
144         //if (m_vob.vob_1->text().isEmpty()) back();
145     }
146     if (page == 3) {
147         KIO::NetAccess::del(KUrl(m_iso.tmp_folder->url().path() + "/DVD"), this);
148         QTimer::singleShot(300, this, SLOT(generateDvd()));
149     }
150 }
151
152 void DvdWizard::buildColor() {
153     m_color->setBrush(m_menu.background_color->color());
154 }
155
156 void DvdWizard::buildImage() {
157     if (m_menu.background_image->url().isEmpty()) {
158         m_scene->removeItem(m_background);
159         return;
160     }
161     QPixmap pix;
162     if (!pix.load(m_menu.background_image->url().path())) {
163         m_scene->removeItem(m_background);
164         return;
165     }
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);
169 }
170
171 void DvdWizard::checkBackground() {
172     if (m_menu.is_color->isChecked()) {
173         m_scene->addItem(m_color);
174         m_scene->removeItem(m_background);
175     } else {
176         m_scene->addItem(m_background);
177         m_scene->removeItem(m_color);
178     }
179 }
180
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());
188 }
189
190
191 void DvdWizard::generateDvd() {
192     KTemporaryFile temp1;
193     temp1.setSuffix(".png");
194     //temp1.setAutoRemove(false);
195     temp1.open();
196
197     KTemporaryFile temp2;
198     temp2.setSuffix(".png");
199     //temp2.setAutoRemove(false);
200     temp2.open();
201
202     KTemporaryFile temp3;
203     temp3.setSuffix(".png");
204     //temp3.setAutoRemove(false);
205     temp3.open();
206
207     KTemporaryFile temp4;
208     temp4.setSuffix(".png");
209     //temp4.setAutoRemove(false);
210     temp4.open();
211
212     KTemporaryFile temp5;
213     temp5.setSuffix(".vob");
214     //temp5.setAutoRemove(false);
215     temp5.open();
216
217     KTemporaryFile temp6;
218     temp6.setSuffix(".xml");
219     //temp6.setAutoRemove(false);
220     temp6.open();
221
222     m_menuFile.setSuffix(".mpg");
223     m_menuFile.setAutoRemove(false);
224     m_menuFile.open();
225
226     m_authorFile.setSuffix(".xml");
227     m_authorFile.setAutoRemove(false);
228     m_authorFile.open();
229
230     QListWidgetItem *images =  m_status.job_progress->item(0);
231     images->setIcon(KIcon("system-run"));
232     qApp->processEvents();
233
234     if (m_menu.create_menu->isChecked()) {
235
236         QImage img(m_width, m_height, QImage::Format_ARGB32);
237         QPainter p(&img);
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));
242         p.end();
243         img.setNumColors(4);
244         img.save(temp1.fileName());
245
246         m_button->setDefaultTextColor(m_menu.selected_color->color());
247         p.begin(&img);
248         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
249         p.end();
250         img.setNumColors(4);
251         img.save(temp2.fileName());
252
253         m_button->setDefaultTextColor(m_menu.highlighted_color->color());
254         p.begin(&img);
255         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
256         p.end();
257         img.setNumColors(4);
258         img.save(temp3.fileName());
259
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);
263
264
265         p.begin(&img);
266         m_scene->render(&p, QRectF(0, 0, m_width, m_height));
267         p.end();
268         img.save(temp4.fileName());
269
270
271         images->setIcon(KIcon("dialog-ok"));
272
273         kDebug() << "/// STARTING MLT VOB CREATION";
274
275         // create menu vob file
276         QListWidgetItem *vobitem =  m_status.job_progress->item(1);
277         vobitem->setIcon(KIcon("system-run"));
278         qApp->processEvents();
279
280         QStringList args;
281         args.append("-profile");
282         args.append(m_profile);
283         args.append(temp4.fileName());
284         args.append("in=0");
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";
295         }
296
297         kDebug() << "MLT ARGS: " << args;
298         QProcess renderbg;
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"));
304                 return;
305             }
306         } else {
307             kDebug() << "/// RENDERING MENU vob timed out";
308             vobitem->setIcon(KIcon("dialog-close"));
309             return;
310         }
311
312         vobitem->setIcon(KIcon("dialog-ok"));
313         kDebug() << "/// STARTING SPUMUX";
314
315         // create xml spumux file
316         QListWidgetItem *spuitem =  m_status.job_progress->item(2);
317         spuitem->setIcon(KIcon("system-run"));
318         qApp->processEvents();
319         QDomDocument doc;
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");
334
335         QFile data(temp6.fileName());
336         if (data.open(QFile::WriteOnly)) {
337             QTextStream out(&data);
338             out << doc.toString();
339         }
340         data.close();
341
342         kDebug() << " SPUMUX DATA: " << doc.toString();
343
344         args.clear();
345         args.append(temp6.fileName());
346         kDebug() << "SPM ARGS: " << args;
347
348         QProcess spumux;
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"));
358                 return;
359             }
360         } else {
361             kDebug() << "/// RENDERING SPUMUX MENU timed out";
362             spuitem->setIcon(KIcon("dialog-close"));
363             return;
364         }
365
366         spuitem->setIcon(KIcon("dialog-ok"));
367         kDebug() << "/// DONE: " << m_menuFile.fileName();
368     }
369
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);
375
376     QDomDocument dvddoc;
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);
395     }
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);
407     }
408
409     QStringList voburls = m_pageVob->selectedUrls();
410     for (int i = 0; i < voburls.count(); i++) {
411         if (!voburls.at(i).isEmpty()) {
412             // Add vob entry
413             QDomElement vob = dvddoc.createElement("vob");
414             vob.setAttribute("file", voburls.at(i));
415             pgc2.appendChild(vob);
416         }
417     }
418
419     QFile data2(m_authorFile.fileName());
420     if (data2.open(QFile::WriteOnly)) {
421         QTextStream out(&data2);
422         out << dvddoc.toString();
423     }
424     data2.close();
425
426
427     QStringList args;
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);
434
435 }
436
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"));
442         return;
443     }
444     authitem->setIcon(KIcon("dialog-ok"));
445     qApp->processEvents();
446     QStringList args;
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);
454
455 }
456
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();
461         m_menuFile.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"));
465         return;
466     }
467     isoitem->setIcon(KIcon("dialog-ok"));
468     kDebug() << "ISO IMAGE " << m_iso.iso_image->url().path() << " Successfully created";
469     m_authorFile.remove();
470     m_menuFile.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()));
473
474 }
475
476