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