summaryrefslogtreecommitdiffstats
path: root/helper.cpp
diff options
context:
space:
mode:
authorArno <arno@disconnect.de>2016-11-27 20:25:49 +0100
committerArno <arno@disconnect.de>2016-11-27 20:25:49 +0100
commitc7e3315b663566f71f83dcc9d2259aac262081c1 (patch)
treed228aa2a385f6097efe319160a01f1d890d78b5b /helper.cpp
parentf5b9109987cd19bbc69e7a20f7a73ac3d86be96a (diff)
downloadShemovCleaner-c7e3315b663566f71f83dcc9d2259aac262081c1.tar.gz
ShemovCleaner-c7e3315b663566f71f83dcc9d2259aac262081c1.tar.bz2
ShemovCleaner-c7e3315b663566f71f83dcc9d2259aac262081c1.zip
Add preview for videos
Grab 4 frames from a video and display them in the Viewer. First frame is @00:01:00, last at length - 1 minute, and the other two are in between: length / 4 * 2 and 3 (hardcoded).
Diffstat (limited to 'helper.cpp')
-rw-r--r--helper.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/helper.cpp b/helper.cpp
index db113ec..a895ec4 100644
--- a/helper.cpp
+++ b/helper.cpp
@@ -4,6 +4,11 @@
#include <QByteArray>
#include <QSettings>
#include <QProcess>
+#include <QJsonObject>
+#include <QTemporaryFile>
+#include <QProcess>
+#include <QFontMetrics>
+#include <QPainter>
#include "helper.h"
@@ -66,6 +71,57 @@ namespace Helper {
return QJsonDocument::fromJson(ffData);
}
+ const QPixmap preview(const QString &file){
+ QJsonDocument jDoc = ffpmegData(file);
+ QJsonObject jObj = jDoc.object();
+ QJsonValue durationV = jObj["format"].toObject()["duration"];
+ int seconds = durationV.toVariant().toDouble();
+ int interval = seconds / 4;
+ QImage img1 = snapshot(file, 60);
+ QImage img2 = snapshot(file, interval * 2);
+ QImage img3 = snapshot(file, interval * 3);
+ QImage img4 = snapshot(file, seconds - 60);
+ QImage retval(640 * 2 + 10, img1.height() * 2 + 10, QImage::Format_ARGB32);
+ //retval.fill(Qt::red);
+ QPainter p(&retval);
+ p.drawImage(0, 0, img1);
+ p.drawImage(650, 0, img2);
+ p.drawImage(0, img1.height() + 10, img3);
+ p.drawImage(650, img1.height() + 10, img4);
+ return QPixmap::fromImage(retval);
+ }
+
+ const QImage snapshot(const QString &file, int offset){
+ const int fixedWith = 640;
+ QSettings s;
+ QString ffmpeg = s.value("ext/ffmpeg").toString();
+ QTemporaryFile tempFile;
+ tempFile.open();
+ QStringList ffmpegArgs = QStringList() << "-y" << "-ss" << QString::number(offset) << "-i" << file << "-f" << "image2" << "-vframes" << "1" << tempFile.fileName();
+ QProcess ffproc;
+ ffproc.start(ffmpeg, ffmpegArgs);
+ if(!ffproc.waitForStarted()){
+ return QImage();
+ }
+ if(ffproc.state() == QProcess::Running){
+ ffproc.waitForFinished();
+ }
+ QImage retval(tempFile.fileName());
+ retval = retval.scaledToWidth(fixedWith);
+ Duration dur(offset);
+ QFont font("Monospace", 10);
+ QFontMetrics fm(font);
+ int width = fm.width(dur.toString());
+ int height = fm.height();
+ QPainter p(&retval);
+ p.setBrush(QBrush(QColor(255, 255, 255, 70)));
+ QRect durationRect(fixedWith / 2 - width / 2 - 4, retval.height() - height - 8, width + 4, height + 4);
+ p.drawRect(durationRect);
+ p.setPen(Qt::black);
+ p.drawText(durationRect, Qt::AlignCenter, dur.toString());
+ return retval;
+ }
+
Duration::Duration() : mHours(0), mMinutes(0), mSeconds(0) {}
Duration::Duration(qint64 seconds){