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