source: smplayer/trunk/src/videopreview/videopreview.cpp@ 119

Last change on this file since 119 was 119, checked in by Silvan Scherrer, 13 years ago

SMPlayer: latest svn update

  • Property svn:eol-style set to LF
File size: 18.6 KB
Line 
1/* smplayer, GUI front-end for mplayer.
2 Copyright (C) 2006-2011 Ricardo Villalba <rvm@escomposlinux.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 Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19#include "videopreview.h"
20#include "videopreviewconfigdialog.h"
21#include <QProcess>
22#include <QRegExp>
23#include <QDir>
24#include <QTime>
25#include <QProgressDialog>
26#include <QWidget>
27#include <QGridLayout>
28#include <QLabel>
29#include <QScrollArea>
30#include <QDialogButtonBox>
31#include <QPushButton>
32#include <QPainter>
33#include <QFileDialog>
34#include <QMessageBox>
35#include <QSettings>
36#include <QApplication>
37#include <QPixmapCache>
38#include <QImageWriter>
39#include <QImageReader>
40
41#include <cmath>
42
43#define RENAME_PICTURES 0
44
45VideoPreview::VideoPreview(QString mplayer_path, QWidget * parent) : QWidget(parent, Qt::Window)
46{
47 setMplayerPath(mplayer_path);
48
49 set = 0; // settings
50 save_last_directory = true;
51
52 prop.input_video.clear();
53 prop.dvd_device.clear();
54 prop.n_cols = 4;
55 prop.n_rows = 4;
56 prop.initial_step = 20;
57 prop.max_width = 800;
58 prop.aspect_ratio = 0;
59 prop.display_osd = true;
60 prop.extract_format = JPEG;
61
62 output_dir = "smplayer_preview";
63 full_output_dir = QDir::tempPath() +"/"+ output_dir;
64
65 progress = new QProgressDialog(this);
66 progress->setMinimumDuration(0);
67 connect( progress, SIGNAL(canceled()), this, SLOT(cancelPressed()) );
68
69 w_contents = new QWidget(this);
70 QPalette p = w_contents->palette();
71 p.setColor(w_contents->backgroundRole(), Qt::white);
72 p.setColor(w_contents->foregroundRole(), Qt::black);
73 w_contents->setPalette(p);
74
75 info = new QLabel(this);
76
77 foot = new QLabel(this);
78 foot->setAlignment(Qt::AlignRight);
79
80 grid_layout = new QGridLayout;
81 grid_layout->setSpacing(2);
82
83 QVBoxLayout * l = new QVBoxLayout;
84 l->setSizeConstraint(QLayout::SetFixedSize);
85 l->addWidget(info);
86 l->addLayout(grid_layout);
87 l->addWidget(foot);
88
89 w_contents->setLayout(l);
90
91 scroll_area = new QScrollArea(this);
92 scroll_area->setWidgetResizable(true);
93 scroll_area->setAlignment(Qt::AlignCenter);
94 scroll_area->setWidget( w_contents );
95
96 button_box = new QDialogButtonBox(QDialogButtonBox::Close | QDialogButtonBox::Save, Qt::Horizontal, this);
97 connect( button_box, SIGNAL(rejected()), this, SLOT(close()) );
98 connect( button_box->button(QDialogButtonBox::Save), SIGNAL(clicked()), this, SLOT(saveImage()) );
99
100 QVBoxLayout * my_layout = new QVBoxLayout;
101 my_layout->addWidget(scroll_area);
102 my_layout->addWidget(button_box);
103 setLayout(my_layout);
104
105 retranslateStrings();
106
107 QList<QByteArray> r_formats = QImageReader::supportedImageFormats();
108 QString read_formats;
109 for (int n=0; n < r_formats.count(); n++) {
110 read_formats.append(r_formats[n]+" ");
111 }
112 qDebug("VideoPreview::VideoPreview: supported formats for reading: %s", read_formats.toUtf8().constData());
113
114 QList<QByteArray> w_formats = QImageWriter::supportedImageFormats();
115 QString write_formats;
116 for (int n=0; n < w_formats.count(); n++) {
117 write_formats.append(w_formats[n]+" ");
118 }
119 qDebug("VideoPreview::VideoPreview: supported formats for writing: %s", write_formats.toUtf8().constData());
120
121 toggleInfoAct = new QAction(this);
122 toggleInfoAct->setCheckable(true);
123 toggleInfoAct->setChecked(true);
124 toggleInfoAct->setShortcut( QKeySequence("Ctrl+H") );
125 connect( toggleInfoAct, SIGNAL(toggled(bool)), this, SLOT(showInfo(bool)) );
126 addAction(toggleInfoAct);
127}
128
129VideoPreview::~VideoPreview() {
130 if (set) saveSettings();
131}
132
133void VideoPreview::retranslateStrings() {
134 progress->setWindowTitle(tr("Video preview"));
135 progress->setCancelButtonText( tr("Cancel") );
136
137 foot->setText("<i>"+ tr("Generated by SMPlayer") +" </i>");
138}
139
140void VideoPreview::setMplayerPath(QString mplayer_path) {
141 mplayer_bin = mplayer_path;
142 QFileInfo fi(mplayer_bin);
143 if (fi.exists() && fi.isExecutable() && !fi.isDir()) {
144 mplayer_bin = fi.absoluteFilePath();
145 }
146
147 qDebug("VideoPreview::setMplayerPath: mplayer_bin: '%s'", mplayer_bin.toUtf8().constData());
148}
149
150void VideoPreview::setSettings(QSettings * settings) {
151 set = settings;
152 loadSettings();
153}
154
155void VideoPreview::clearThumbnails() {
156 for (int n=0; n < label_list.count(); n++) {
157 grid_layout->removeWidget( label_list[n] );
158 delete label_list[n];
159 }
160 label_list.clear();
161 info->clear();
162}
163
164QString VideoPreview::framePicture() {
165 if (prop.extract_format == PNG)
166 return "00000005.png";
167 else
168 return "00000005.jpg";
169}
170
171bool VideoPreview::createThumbnails() {
172 clearThumbnails();
173 error_message.clear();
174
175 button_box->setEnabled(false);
176
177 bool result = extractImages();
178
179 progress->close();
180
181 if ((result == false) && (!error_message.isEmpty())) {
182 QMessageBox::critical(this, tr("Error"),
183 tr("The following error has occurred while creating the thumbnails:")+"\n"+ error_message );
184 }
185
186 button_box->setEnabled(true);
187
188 // Adjust size
189 //resize( w_contents->sizeHint() );
190
191 cleanDir(full_output_dir);
192 return result;
193}
194
195bool VideoPreview::extractImages() {
196 VideoInfo i = getInfo(mplayer_bin, prop.input_video);
197 int length = i.length;
198
199 if (length == 0) {
200 if (error_message.isEmpty()) error_message = tr("The length of the video is 0");
201 return false;
202 }
203
204 // Create a temporary directory
205 QDir d(QDir::tempPath());
206 if (!d.exists(output_dir)) {
207 if (!d.mkpath(output_dir)) {
208 qDebug("VideoPreview::extractImages: error: can't create '%s'", full_output_dir.toUtf8().constData());
209 error_message = tr("The temporary directory (%1) can't be created").arg(full_output_dir);
210 return false;
211 }
212 }
213
214 displayVideoInfo(i);
215
216 // Let's begin
217 run.thumbnail_width = 0;
218
219 int num_pictures = prop.n_cols * prop.n_rows;
220 length -= prop.initial_step;
221 int s_step = length / num_pictures;
222
223 int current_time = prop.initial_step;
224
225 canceled = false;
226 progress->setLabelText(tr("Creating thumbnails..."));
227 progress->setRange(0, num_pictures-1);
228 progress->show();
229
230 double aspect_ratio = i.aspect;
231 if (prop.aspect_ratio != 0) aspect_ratio = prop.aspect_ratio;
232
233 for (int n = 0; n < num_pictures; n++) {
234 qDebug("VideoPreview::extractImages: getting frame %d of %d...", n+1, num_pictures);
235 progress->setValue(n);
236 qApp->processEvents();
237
238 if (canceled) return false;
239
240 if (!runMplayer(current_time, aspect_ratio)) return false;
241
242 QString frame_picture = full_output_dir + "/" + framePicture();
243 if (!QFile::exists(frame_picture)) {
244 error_message = tr("The file %1 doesn't exist").arg(frame_picture);
245 return false;
246 }
247
248#if RENAME_PICTURES
249 QString extension = (extractFormat()==PNG) ? "png" : "jpg";
250 QString output_file = output_dir + QString("/picture_%1.%2").arg(current_time, 8, 10, QLatin1Char('0')).arg(extension);
251 d.rename(output_dir + "/" + framePicture(), output_file);
252#else
253 QString output_file = output_dir + "/" + framePicture();
254#endif
255
256 if (!addPicture(QDir::tempPath() +"/"+ output_file, n, current_time)) {
257 return false;
258 }
259
260 current_time += s_step;
261 }
262
263 return true;
264}
265
266bool VideoPreview::runMplayer(int seek, double aspect_ratio) {
267 QStringList args;
268 args << "-nosound";
269
270 if (prop.extract_format == PNG) {
271 args << "-vo"
272 << "png:outdir=\""+full_output_dir+"\"";
273 } else {
274 args << "-vo"
275 << "jpeg:outdir=\""+full_output_dir+"\"";
276 }
277
278 args << "-frames" << "6" << "-ss" << QString::number(seek);
279
280 if (aspect_ratio != 0) {
281 args << "-aspect" << QString::number(aspect_ratio) << "-zoom";
282 }
283
284 if (!prop.dvd_device.isEmpty()) {
285 args << "-dvd-device" << prop.dvd_device;
286 }
287
288 /*
289 if (display_osd) {
290 args << "-vf" << "expand=osd=1" << "-osdlevel" << "2";
291 }
292 */
293
294 args << prop.input_video;
295
296 QString command = mplayer_bin + " ";
297 for (int n = 0; n < args.count(); n++) command = command + args[n] + " ";
298 qDebug("VideoPreview::runMplayer: command: %s", command.toUtf8().constData());
299
300 QProcess p;
301 p.start(mplayer_bin, args);
302 if (!p.waitForFinished()) {
303 qDebug("VideoPreview::runMplayer: error running process");
304 error_message = tr("The mplayer process didn't run");
305 return false;
306 }
307
308 return true;
309}
310
311
312bool VideoPreview::addPicture(const QString & filename, int num, int time) {
313 int row = num / prop.n_cols;
314 int col = num % prop.n_cols;
315
316 qDebug("VideoPreview::addPicture: %d (row: %d col: %d) file: '%s'", num, row, col, filename.toUtf8().constData());
317
318 QPixmapCache::clear();
319 QPixmap picture;
320 if (!picture.load(filename)) {
321 qDebug("VideoPreview::addPicture: can't load file");
322 error_message = tr("The file %1 can't be loaded").arg(filename);
323 return false;
324 }
325
326 if (run.thumbnail_width == 0) {
327 int spacing = grid_layout->horizontalSpacing() * (prop.n_cols-1);
328 if (spacing < 0) spacing = 0;
329 qDebug("VideoPreview::addPicture: spacing: %d", spacing);
330 run.thumbnail_width = (prop.max_width - spacing) / prop.n_cols;
331 if (run.thumbnail_width > picture.width()) run.thumbnail_width = picture.width();
332 qDebug("VideoPreview::addPicture: thumbnail_width set to %d", run.thumbnail_width);
333 }
334
335 QPixmap scaled_picture = picture.scaledToWidth(run.thumbnail_width, Qt::SmoothTransformation);
336
337 // Add current time text
338 if (prop.display_osd) {
339 QString stime = QTime().addSecs(time).toString("hh:mm:ss");
340 QFont font("Arial");
341 font.setBold(true);
342 QPainter painter(&scaled_picture);
343 painter.setPen( Qt::white );
344 painter.setFont(font);
345 painter.drawText(scaled_picture.rect(), Qt::AlignRight | Qt::AlignBottom, stime);
346 }
347
348 QLabel * l = new QLabel(this);
349 label_list.append(l);
350 l->setPixmap(scaled_picture);
351 //l->setPixmap(picture);
352 grid_layout->addWidget(l, row, col);
353
354 return true;
355}
356
357void VideoPreview::displayVideoInfo(const VideoInfo & i) {
358 // Display info about the video
359 QTime t = QTime().addSecs(i.length);
360
361 QString aspect = QString::number(i.aspect);
362 if (fabs(1.77 - i.aspect) < 0.1) aspect = "16:9";
363 else
364 if (fabs(1.33 - i.aspect) < 0.1) aspect = "4:3";
365 else
366 if (fabs(2.35 - i.aspect) < 0.1) aspect = "2.35:1";
367
368 QString no_info = tr("No info");
369
370 QString fps = (i.fps==0 || i.fps==1000) ? no_info : QString("%1").arg(i.fps);
371 QString video_bitrate = (i.video_bitrate==0) ? no_info : tr("%1 kbps").arg(i.video_bitrate/1000);
372 QString audio_bitrate = (i.audio_bitrate==0) ? no_info : tr("%1 kbps").arg(i.audio_bitrate/1000);
373 QString audio_rate = (i.audio_rate==0) ? no_info : tr("%1 Hz").arg(i.audio_rate);
374
375 info->setText(
376 "<b><font size=+1>" + i.filename +"</font></b>"
377 "<table cellspacing=4 cellpadding=4><tr>"
378 "<td>" +
379 tr("Size: %1 MB").arg(i.size / (1024*1024)) + "<br>" +
380 tr("Resolution: %1x%2").arg(i.width).arg(i.height) + "<br>" +
381 tr("Length: %1").arg(t.toString("hh:mm:ss")) +
382 "</td>"
383 "<td>" +
384 tr("Video format: %1").arg(i.video_format) + "<br>" +
385 tr("Frames per second: %1").arg(fps) + "<br>" +
386 tr("Aspect ratio: %1").arg(aspect) + //"<br>" +
387 "</td>"
388 "<td>" +
389 tr("Video bitrate: %1").arg(video_bitrate) + "<br>" +
390 tr("Audio bitrate: %1").arg(audio_bitrate) + "<br>" +
391 tr("Audio rate: %1").arg(audio_rate) + //"<br>" +
392 "</td>"
393 "</tr></table>"
394 );
395 setWindowTitle( tr("Video preview") + " - " + i.filename );
396}
397
398void VideoPreview::cleanDir(QString directory) {
399 QStringList filter;
400 if (prop.extract_format == PNG) {
401 filter.append("*.png");
402 } else {
403 filter.append("*.jpg");
404 }
405
406 QDir d(directory);
407 QStringList l = d.entryList( filter, QDir::Files, QDir::Unsorted);
408
409 for (int n = 0; n < l.count(); n++) {
410 qDebug("VideoPreview::cleanDir: deleting '%s'", l[n].toUtf8().constData());
411 d.remove(l[n]);
412 }
413 qDebug("VideoPreview::cleanDir: removing directory '%s'", directory.toUtf8().constData());
414 d.rmpath(directory);
415}
416
417VideoInfo VideoPreview::getInfo(const QString & mplayer_path, const QString & filename) {
418 VideoInfo i;
419
420 if (filename.isEmpty()) {
421 error_message = tr("No filename");
422 return i;
423 }
424
425 QFileInfo fi(filename);
426 if (fi.exists()) {
427 i.filename = fi.fileName();
428 i.size = fi.size();
429 }
430
431 QRegExp rx("^ID_(.*)=(.*)");
432
433 QProcess p;
434 p.setProcessChannelMode( QProcess::MergedChannels );
435
436 QStringList args;
437 args << "-vo" << "null" << "-ao" << "null" << "-frames" << "1" << "-identify" << "-nocache" << "-noquiet" << filename;
438
439 if (!prop.dvd_device.isEmpty()) {
440 args << "-dvd-device" << prop.dvd_device;
441 }
442
443 p.start(mplayer_path, args);
444
445 if (p.waitForFinished()) {
446 QByteArray line;
447 while (p.canReadLine()) {
448 line = p.readLine().trimmed();
449 qDebug("VideoPreview::getInfo: '%s'", line.constData());
450 if (rx.indexIn(line) > -1) {
451 QString tag = rx.cap(1);
452 QString value = rx.cap(2);
453 qDebug("VideoPreview::getInfo: tag: '%s', value: '%s'", tag.toUtf8().constData(), value.toUtf8().constData());
454
455 if (tag == "LENGTH") i.length = (int) value.toDouble();
456 else
457 if (tag == "VIDEO_WIDTH") i.width = value.toInt();
458 else
459 if (tag == "VIDEO_HEIGHT") i.height = value.toInt();
460 else
461 if (tag == "VIDEO_FPS") i.fps = value.toDouble();
462 else
463 if (tag == "VIDEO_ASPECT") {
464 i.aspect = value.toDouble();
465 if ((i.aspect == 0) && (i.width != 0) && (i.height != 0)) {
466 i.aspect = (double) i.width / i.height;
467 }
468 }
469 else
470 if (tag == "VIDEO_BITRATE") i.video_bitrate = value.toInt();
471 else
472 if (tag == "AUDIO_BITRATE") i.audio_bitrate = value.toInt();
473 else
474 if (tag == "AUDIO_RATE") i.audio_rate = value.toInt();
475 else
476 if (tag == "VIDEO_FORMAT") i.video_format = value;
477 }
478 }
479 } else {
480 qDebug("VideoPreview::getInfo: error: process didn't start");
481 error_message = tr("The mplayer process didn't start while trying to get info about the video");
482 }
483
484 qDebug("VideoPreview::getInfo: filename: '%s'", i.filename.toUtf8().constData());
485 qDebug("VideoPreview::getInfo: resolution: '%d x %d'", i.width, i.height);
486 qDebug("VideoPreview::getInfo: length: '%d'", i.length);
487 qDebug("VideoPreview::getInfo: size: '%d'", (int) i.size);
488
489 return i;
490}
491
492void VideoPreview::showInfo(bool visible) {
493 qDebug("VideoPreview::showInfo: %d", visible);
494 info->setShown(visible);
495 foot->setShown(visible);
496}
497
498void VideoPreview::saveImage() {
499 qDebug("VideoPreview::saveImage");
500
501 // Proposed name
502 QString proposed_name = "";
503 if (save_last_directory) proposed_name = last_directory;
504
505 QFileInfo fi(prop.input_video);
506 if (fi.exists()) {
507 if (!save_last_directory) proposed_name = fi.absolutePath();
508 QString extension = (extractFormat()==PNG) ? "png" : "jpg";
509 proposed_name += "/"+ fi.completeBaseName() +"_preview."+ extension;
510 }
511
512 // Formats
513 QList<QByteArray> w_formats = QImageWriter::supportedImageFormats();
514 QString write_formats;
515 for (int n=0; n < w_formats.count(); n++) {
516 write_formats.append("*."+w_formats[n]+" ");
517 }
518 if (write_formats.isEmpty()) {
519 // Shouldn't happen!
520 write_formats = "*.png *.jpg";
521 }
522
523 QString filename = QFileDialog::getSaveFileName(this, tr("Save file"),
524 proposed_name, tr("Images") +" ("+ write_formats +")");
525
526 if (!filename.isEmpty()) {
527 QPixmap image = QPixmap::grabWidget(w_contents);
528 if (!image.save(filename)) {
529 // Failed!!!
530 qDebug("VideoPreview::saveImage: error saving '%s'", filename.toUtf8().constData());
531 QMessageBox::warning(this, tr("Error saving file"),
532 tr("The file couldn't be saved") );
533 } else {
534 last_directory = QFileInfo(filename).absolutePath();
535 }
536 }
537}
538
539bool VideoPreview::showConfigDialog(QWidget * parent) {
540 VideoPreviewConfigDialog d(parent);
541
542 d.setVideoFile( videoFile() );
543 d.setDVDDevice( DVDDevice() );
544 d.setCols( cols() );
545 d.setRows( rows() );
546 d.setInitialStep( initialStep() );
547 d.setMaxWidth( maxWidth() );
548 d.setDisplayOSD( displayOSD() );
549 d.setAspectRatio( aspectRatio() );
550 d.setFormat( extractFormat() );
551 d.setSaveLastDirectory( save_last_directory );
552
553 if (d.exec() == QDialog::Accepted) {
554 setVideoFile( d.videoFile() );
555 setDVDDevice( d.DVDDevice() );
556 setCols( d.cols() );
557 setRows( d.rows() );
558 setInitialStep( d.initialStep() );
559 setMaxWidth( d.maxWidth() );
560 setDisplayOSD( d.displayOSD() );
561 setAspectRatio( d.aspectRatio() );
562 setExtractFormat(d.format() );
563 save_last_directory = d.saveLastDirectory();
564
565 return true;
566 }
567
568 return false;
569}
570
571void VideoPreview::saveSettings() {
572 qDebug("VideoPreview::saveSettings");
573
574 set->beginGroup("videopreview");
575
576 set->setValue("columns", cols());
577 set->setValue("rows", rows());
578 set->setValue("initial_step", initialStep());
579 set->setValue("max_width", maxWidth());
580 set->setValue("osd", displayOSD());
581 set->setValue("format", extractFormat());
582 set->setValue("save_last_directory", save_last_directory);
583
584 if (save_last_directory) {
585 set->setValue("last_directory", last_directory);
586 }
587
588 set->setValue("filename", videoFile());
589 set->setValue("dvd_device", DVDDevice());
590
591 set->setValue("show_info", toggleInfoAct->isChecked());
592
593 set->endGroup();
594}
595
596void VideoPreview::loadSettings() {
597 qDebug("VideoPreview::loadSettings");
598
599 set->beginGroup("videopreview");
600
601 setCols( set->value("columns", cols()).toInt() );
602 setRows( set->value("rows", rows()).toInt() );
603 setInitialStep( set->value("initial_step", initialStep()).toInt() );
604 setMaxWidth( set->value("max_width", maxWidth()).toInt() );
605 setDisplayOSD( set->value("osd", displayOSD()).toBool() );
606 setExtractFormat( (ExtractFormat) set->value("format", extractFormat()).toInt() );
607 save_last_directory = set->value("save_last_directory", save_last_directory).toBool();
608 last_directory = set->value("last_directory", last_directory).toString();
609
610 setVideoFile( set->value("filename", videoFile()).toString() );
611 setDVDDevice( set->value("dvd_device", DVDDevice()).toString() );
612
613 toggleInfoAct->setChecked(set->value("show_info", true).toBool());
614
615 set->endGroup();
616}
617
618void VideoPreview::adjustWindowSize() {
619 qDebug("VideoPreview::adjustWindowSize: window size: %d %d", width(), height());
620 qDebug("VideoPreview::adjustWindowSize: scroll_area size: %d %d", scroll_area->width(), scroll_area->height());
621
622 int diff_width = width() - scroll_area->maximumViewportSize().width();
623 int diff_height = height() - scroll_area->maximumViewportSize().height();
624
625 qDebug("VideoPreview::adjustWindowSize: diff_width: %d diff_height: %d", diff_width, diff_height);
626
627 QSize new_size = w_contents->size() + QSize( diff_width, diff_height);
628
629 qDebug("VideoPreview::adjustWindowSize: new_size: %d %d", new_size.width(), new_size.height());
630
631 resize(new_size);
632}
633
634void VideoPreview::cancelPressed() {
635 canceled = true;
636}
637
638// Language change stuff
639void VideoPreview::changeEvent(QEvent *e) {
640 if (e->type() == QEvent::LanguageChange) {
641 retranslateStrings();
642 } else {
643 QWidget::changeEvent(e);
644 }
645}
646
647#include "moc_videopreview.cpp"
648
Note: See TracBrowser for help on using the repository browser.