From c7e3315b663566f71f83dcc9d2259aac262081c1 Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 27 Nov 2016 20:25:49 +0100 Subject: 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). --- helper.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'helper.cpp') diff --git a/helper.cpp b/helper.cpp index db113ec..a895ec4 100644 --- a/helper.cpp +++ b/helper.cpp @@ -4,6 +4,11 @@ #include #include #include +#include +#include +#include +#include +#include #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){ -- cgit v1.2.3-70-g09d2