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