1
/* smplayer, GUI front-end for mplayer.
2
Copyright (C) 2006-2009 Ricardo Villalba <rvm@escomposlinux.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 Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include "videopreview.h"
20
#include "videopreviewconfigdialog.h"
25
#include <QProgressDialog>
27
#include <QGridLayout>
29
#include <QScrollArea>
30
#include <QDialogButtonBox>
31
#include <QPushButton>
33
#include <QFileDialog>
34
#include <QMessageBox>
36
#include <QApplication>
37
#include <QPixmapCache>
38
#include <QImageWriter>
39
#include <QImageReader>
43
#define RENAME_PICTURES 0
45
VideoPreview::VideoPreview(QString mplayer_path, QWidget * parent) : QWidget(parent, Qt::Window)
47
setMplayerPath(mplayer_path);
50
save_last_directory = true;
52
prop.input_video.clear();
53
prop.dvd_device.clear();
56
prop.initial_step = 20;
58
prop.aspect_ratio = 0;
59
prop.display_osd = true;
60
prop.extract_format = JPEG;
62
output_dir = "smplayer_preview";
63
full_output_dir = QDir::tempPath() +"/"+ output_dir;
65
progress = new QProgressDialog(this);
66
progress->setMinimumDuration(0);
67
connect( progress, SIGNAL(canceled()), this, SLOT(cancelPressed()) );
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);
75
info = new QLabel(this);
77
foot = new QLabel(this);
78
foot->setAlignment(Qt::AlignRight);
80
grid_layout = new QGridLayout;
81
grid_layout->setSpacing(2);
83
QVBoxLayout * l = new QVBoxLayout;
84
l->setSizeConstraint(QLayout::SetFixedSize);
86
l->addLayout(grid_layout);
89
w_contents->setLayout(l);
91
scroll_area = new QScrollArea(this);
92
scroll_area->setWidgetResizable(true);
93
scroll_area->setAlignment(Qt::AlignCenter);
94
scroll_area->setWidget( w_contents );
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()) );
100
QVBoxLayout * my_layout = new QVBoxLayout;
101
my_layout->addWidget(scroll_area);
102
my_layout->addWidget(button_box);
103
setLayout(my_layout);
105
retranslateStrings();
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]+" ");
112
qDebug("VideoPreview::VideoPreview: supported formats for reading: %s", read_formats.toUtf8().constData());
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]+" ");
119
qDebug("VideoPreview::VideoPreview: supported formats for writing: %s", write_formats.toUtf8().constData());
122
VideoPreview::~VideoPreview() {
123
if (set) saveSettings();
126
void VideoPreview::retranslateStrings() {
127
progress->setWindowTitle(tr("Video preview"));
128
progress->setCancelButtonText( tr("Cancel") );
130
foot->setText("<i>"+ tr("Generated by SMPlayer") +" </i>");
133
void VideoPreview::setMplayerPath(QString mplayer_path) {
134
mplayer_bin = mplayer_path;
135
QFileInfo fi(mplayer_bin);
136
if (fi.exists() && fi.isExecutable() && !fi.isDir()) {
137
mplayer_bin = fi.absoluteFilePath();
140
qDebug("VideoPreview::setMplayerPath: mplayer_bin: '%s'", mplayer_bin.toUtf8().constData());
143
void VideoPreview::setSettings(QSettings * settings) {
148
void VideoPreview::clearThumbnails() {
149
for (int n=0; n < label_list.count(); n++) {
150
grid_layout->removeWidget( label_list[n] );
151
delete label_list[n];
157
QString VideoPreview::framePicture() {
158
if (prop.extract_format == PNG)
159
return "00000005.png";
161
return "00000005.jpg";
164
bool VideoPreview::createThumbnails() {
166
error_message.clear();
168
button_box->setEnabled(false);
170
bool result = extractImages();
174
if ((result == false) && (!error_message.isEmpty())) {
175
QMessageBox::critical(this, tr("Error"),
176
tr("The following error has occurred while creating the thumbnails:")+"\n"+ error_message );
179
button_box->setEnabled(true);
182
//resize( w_contents->sizeHint() );
184
cleanDir(full_output_dir);
188
bool VideoPreview::extractImages() {
189
VideoInfo i = getInfo(mplayer_bin, prop.input_video);
190
int length = i.length;
193
if (error_message.isEmpty()) error_message = tr("The length of the video is 0");
197
// Create a temporary directory
198
QDir d(QDir::tempPath());
199
if (!d.exists(output_dir)) {
200
if (!d.mkpath(output_dir)) {
201
qDebug("VideoPreview::extractImages: error: can't create '%s'", full_output_dir.toUtf8().constData());
202
error_message = tr("The temporary directory (%1) can't be created").arg(full_output_dir);
210
run.thumbnail_width = 0;
212
int num_pictures = prop.n_cols * prop.n_rows;
213
length -= prop.initial_step;
214
int s_step = length / num_pictures;
216
int current_time = prop.initial_step;
219
progress->setLabelText(tr("Creating thumbnails..."));
220
progress->setRange(0, num_pictures-1);
223
double aspect_ratio = i.aspect;
224
if (prop.aspect_ratio != 0) aspect_ratio = prop.aspect_ratio;
226
for (int n = 0; n < num_pictures; n++) {
227
qDebug("VideoPreview::extractImages: getting frame %d of %d...", n+1, num_pictures);
228
progress->setValue(n);
229
qApp->processEvents();
231
if (canceled) return false;
233
if (!runMplayer(current_time, aspect_ratio)) return false;
235
QString frame_picture = full_output_dir + "/" + framePicture();
236
if (!QFile::exists(frame_picture)) {
237
error_message = tr("The file %1 doesn't exist").arg(frame_picture);
242
QString extension = (extractFormat()==PNG) ? "png" : "jpg";
243
QString output_file = output_dir + QString("/picture_%1.%2").arg(current_time, 8, 10, QLatin1Char('0')).arg(extension);
244
d.rename(output_dir + "/" + framePicture(), output_file);
246
QString output_file = output_dir + "/" + framePicture();
249
if (!addPicture(QDir::tempPath() +"/"+ output_file, n, current_time)) {
253
current_time += s_step;
259
bool VideoPreview::runMplayer(int seek, double aspect_ratio) {
263
if (prop.extract_format == PNG) {
265
<< "png:outdir=\""+full_output_dir+"\"";
268
<< "jpeg:outdir=\""+full_output_dir+"\"";
271
args << "-frames" << "6" << "-ss" << QString::number(seek);
273
if (aspect_ratio != 0) {
274
args << "-aspect" << QString::number(aspect_ratio) << "-zoom";
277
if (!prop.dvd_device.isEmpty()) {
278
args << "-dvd-device" << prop.dvd_device;
283
args << "-vf" << "expand=osd=1" << "-osdlevel" << "2";
287
args << prop.input_video;
289
QString command = mplayer_bin + " ";
290
for (int n = 0; n < args.count(); n++) command = command + args[n] + " ";
291
qDebug("VideoPreview::runMplayer: command: %s", command.toUtf8().constData());
294
p.start(mplayer_bin, args);
295
if (!p.waitForFinished()) {
296
qDebug("VideoPreview::runMplayer: error running process");
297
error_message = tr("The mplayer process didn't run");
305
bool VideoPreview::addPicture(const QString & filename, int num, int time) {
306
int row = num / prop.n_cols;
307
int col = num % prop.n_cols;
309
qDebug("VideoPreview::addPicture: %d (row: %d col: %d) file: '%s'", num, row, col, filename.toUtf8().constData());
311
QPixmapCache::clear();
313
if (!picture.load(filename)) {
314
qDebug("VideoPreview::addPicture: can't load file");
315
error_message = tr("The file %1 can't be loaded").arg(filename);
319
if (run.thumbnail_width == 0) {
320
int spacing = grid_layout->horizontalSpacing() * (prop.n_cols-1);
321
if (spacing < 0) spacing = 0;
322
qDebug("VideoPreview::addPicture: spacing: %d", spacing);
323
run.thumbnail_width = (prop.max_width - spacing) / prop.n_cols;
324
if (run.thumbnail_width > picture.width()) run.thumbnail_width = picture.width();
325
qDebug("VideoPreview::addPicture: thumbnail_width set to %d", run.thumbnail_width);
328
QPixmap scaled_picture = picture.scaledToWidth(run.thumbnail_width, Qt::SmoothTransformation);
330
// Add current time text
331
if (prop.display_osd) {
332
QString stime = QTime().addSecs(time).toString("hh:mm:ss");
335
QPainter painter(&scaled_picture);
336
painter.setPen( Qt::white );
337
painter.setFont(font);
338
painter.drawText(scaled_picture.rect(), Qt::AlignRight | Qt::AlignBottom, stime);
341
QLabel * l = new QLabel(this);
342
label_list.append(l);
343
l->setPixmap(scaled_picture);
344
//l->setPixmap(picture);
345
grid_layout->addWidget(l, row, col);
350
void VideoPreview::displayVideoInfo(const VideoInfo & i) {
351
// Display info about the video
352
QTime t = QTime().addSecs(i.length);
354
QString aspect = QString::number(i.aspect);
355
if (fabs(1.77 - i.aspect) < 0.1) aspect = "16:9";
357
if (fabs(1.33 - i.aspect) < 0.1) aspect = "4:3";
359
if (fabs(2.35 - i.aspect) < 0.1) aspect = "2.35:1";
361
QString no_info = tr("No info");
363
QString fps = (i.fps==0 || i.fps==1000) ? no_info : QString("%1").arg(i.fps);
364
QString video_bitrate = (i.video_bitrate==0) ? no_info : tr("%1 kbps").arg(i.video_bitrate/1000);
365
QString audio_bitrate = (i.audio_bitrate==0) ? no_info : tr("%1 kbps").arg(i.audio_bitrate/1000);
366
QString audio_rate = (i.audio_rate==0) ? no_info : tr("%1 Hz").arg(i.audio_rate);
369
"<b><font size=+1>" + i.filename +"</font></b>"
370
"<table cellspacing=4 cellpadding=4><tr>"
372
tr("Size: %1 MB").arg(i.size / (1024*1024)) + "<br>" +
373
tr("Resolution: %1x%2").arg(i.width).arg(i.height) + "<br>" +
374
tr("Length: %1").arg(t.toString("hh:mm:ss")) +
377
tr("Video format: %1").arg(i.video_format) + "<br>" +
378
tr("Frames per second: %1").arg(fps) + "<br>" +
379
tr("Aspect ratio: %1").arg(aspect) + //"<br>" +
382
tr("Video bitrate: %1").arg(video_bitrate) + "<br>" +
383
tr("Audio bitrate: %1").arg(audio_bitrate) + "<br>" +
384
tr("Audio rate: %1").arg(audio_rate) + //"<br>" +
388
setWindowTitle( tr("Video preview") + " - " + i.filename );
391
void VideoPreview::cleanDir(QString directory) {
393
if (prop.extract_format == PNG) {
394
filter.append("*.png");
396
filter.append("*.jpg");
400
QStringList l = d.entryList( filter, QDir::Files, QDir::Unsorted);
402
for (int n = 0; n < l.count(); n++) {
403
qDebug("VideoPreview::cleanDir: deleting '%s'", l[n].toUtf8().constData());
406
qDebug("VideoPreview::cleanDir: removing directory '%s'", directory.toUtf8().constData());
410
VideoInfo VideoPreview::getInfo(const QString & mplayer_path, const QString & filename) {
413
if (filename.isEmpty()) {
414
error_message = tr("No filename");
418
QFileInfo fi(filename);
420
i.filename = fi.fileName();
424
QRegExp rx("^ID_(.*)=(.*)");
427
p.setProcessChannelMode( QProcess::MergedChannels );
430
args << "-vo" << "null" << "-ao" << "null" << "-frames" << "1" << "-identify" << "-nocache" << "-noquiet" << filename;
432
if (!prop.dvd_device.isEmpty()) {
433
args << "-dvd-device" << prop.dvd_device;
436
p.start(mplayer_path, args);
438
if (p.waitForFinished()) {
440
while (p.canReadLine()) {
441
line = p.readLine().trimmed();
442
qDebug("VideoPreview::getInfo: '%s'", line.constData());
443
if (rx.indexIn(line) > -1) {
444
QString tag = rx.cap(1);
445
QString value = rx.cap(2);
446
qDebug("VideoPreview::getInfo: tag: '%s', value: '%s'", tag.toUtf8().constData(), value.toUtf8().constData());
448
if (tag == "LENGTH") i.length = (int) value.toDouble();
450
if (tag == "VIDEO_WIDTH") i.width = value.toInt();
452
if (tag == "VIDEO_HEIGHT") i.height = value.toInt();
454
if (tag == "VIDEO_FPS") i.fps = value.toDouble();
456
if (tag == "VIDEO_ASPECT") {
457
i.aspect = value.toDouble();
458
if ((i.aspect == 0) && (i.width != 0) && (i.height != 0)) {
459
i.aspect = (double) i.width / i.height;
463
if (tag == "VIDEO_BITRATE") i.video_bitrate = value.toInt();
465
if (tag == "AUDIO_BITRATE") i.audio_bitrate = value.toInt();
467
if (tag == "AUDIO_RATE") i.audio_rate = value.toInt();
469
if (tag == "VIDEO_FORMAT") i.video_format = value;
473
qDebug("VideoPreview::getInfo: error: process didn't start");
474
error_message = tr("The mplayer process didn't start while trying to get info about the video");
477
qDebug("VideoPreview::getInfo: filename: '%s'", i.filename.toUtf8().constData());
478
qDebug("VideoPreview::getInfo: resolution: '%d x %d'", i.width, i.height);
479
qDebug("VideoPreview::getInfo: length: '%d'", i.length);
480
qDebug("VideoPreview::getInfo: size: '%d'", (int) i.size);
486
void VideoPreview::saveImage() {
487
qDebug("VideoPreview::saveImage");
490
QString proposed_name = "";
491
if (save_last_directory) proposed_name = last_directory;
493
QFileInfo fi(prop.input_video);
495
if (!save_last_directory) proposed_name = fi.absolutePath();
496
QString extension = (extractFormat()==PNG) ? "png" : "jpg";
497
proposed_name += "/"+ fi.completeBaseName() +"_preview."+ extension;
501
QList<QByteArray> w_formats = QImageWriter::supportedImageFormats();
502
QString write_formats;
503
for (int n=0; n < w_formats.count(); n++) {
504
write_formats.append("*."+w_formats[n]+" ");
506
if (write_formats.isEmpty()) {
508
write_formats = "*.png *.jpg";
511
QString filename = QFileDialog::getSaveFileName(this, tr("Save file"),
512
proposed_name, tr("Images") +" ("+ write_formats +")");
514
if (!filename.isEmpty()) {
515
QPixmap image = QPixmap::grabWidget(w_contents);
516
if (!image.save(filename)) {
518
qDebug("VideoPreview::saveImage: error saving '%s'", filename.toUtf8().constData());
519
QMessageBox::warning(this, tr("Error saving file"),
520
tr("The file couldn't be saved") );
522
last_directory = QFileInfo(filename).absolutePath();
527
bool VideoPreview::showConfigDialog(QWidget * parent) {
528
VideoPreviewConfigDialog d(parent);
530
d.setVideoFile( videoFile() );
531
d.setDVDDevice( DVDDevice() );
534
d.setInitialStep( initialStep() );
535
d.setMaxWidth( maxWidth() );
536
d.setDisplayOSD( displayOSD() );
537
d.setAspectRatio( aspectRatio() );
538
d.setFormat( extractFormat() );
539
d.setSaveLastDirectory( save_last_directory );
541
if (d.exec() == QDialog::Accepted) {
542
setVideoFile( d.videoFile() );
543
setDVDDevice( d.DVDDevice() );
546
setInitialStep( d.initialStep() );
547
setMaxWidth( d.maxWidth() );
548
setDisplayOSD( d.displayOSD() );
549
setAspectRatio( d.aspectRatio() );
550
setExtractFormat(d.format() );
551
save_last_directory = d.saveLastDirectory();
559
void VideoPreview::saveSettings() {
560
qDebug("VideoPreview::saveSettings");
562
set->beginGroup("videopreview");
564
set->setValue("columns", cols());
565
set->setValue("rows", rows());
566
set->setValue("initial_step", initialStep());
567
set->setValue("max_width", maxWidth());
568
set->setValue("osd", displayOSD());
569
set->setValue("format", extractFormat());
570
set->setValue("save_last_directory", save_last_directory);
572
if (save_last_directory) {
573
set->setValue("last_directory", last_directory);
576
set->setValue("filename", videoFile());
577
set->setValue("dvd_device", DVDDevice());
582
void VideoPreview::loadSettings() {
583
qDebug("VideoPreview::loadSettings");
585
set->beginGroup("videopreview");
587
setCols( set->value("columns", cols()).toInt() );
588
setRows( set->value("rows", rows()).toInt() );
589
setInitialStep( set->value("initial_step", initialStep()).toInt() );
590
setMaxWidth( set->value("max_width", maxWidth()).toInt() );
591
setDisplayOSD( set->value("osd", displayOSD()).toBool() );
592
setExtractFormat( (ExtractFormat) set->value("format", extractFormat()).toInt() );
593
save_last_directory = set->value("save_last_directory", save_last_directory).toBool();
594
last_directory = set->value("last_directory", last_directory).toString();
596
setVideoFile( set->value("filename", videoFile()).toString() );
597
setDVDDevice( set->value("dvd_device", DVDDevice()).toString() );
602
void VideoPreview::adjustWindowSize() {
603
qDebug("VideoPreview::adjustWindowSize: window size: %d %d", width(), height());
604
qDebug("VideoPreview::adjustWindowSize: scroll_area size: %d %d", scroll_area->width(), scroll_area->height());
606
int diff_width = width() - scroll_area->maximumViewportSize().width();
607
int diff_height = height() - scroll_area->maximumViewportSize().height();
609
qDebug("VideoPreview::adjustWindowSize: diff_width: %d diff_height: %d", diff_width, diff_height);
611
QSize new_size = w_contents->size() + QSize( diff_width, diff_height);
613
qDebug("VideoPreview::adjustWindowSize: new_size: %d %d", new_size.width(), new_size.height());
618
void VideoPreview::cancelPressed() {
622
// Language change stuff
623
void VideoPreview::changeEvent(QEvent *e) {
624
if (e->type() == QEvent::LanguageChange) {
625
retranslateStrings();
627
QWidget::changeEvent(e);
631
#include "moc_videopreview.cpp"