diff options
Diffstat (limited to 'helper.cpp')
-rw-r--r-- | helper.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -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){ |