]> git.sesse.net Git - kdenlive/blob - thumbnailer/westleypreview.cpp
Improve selection of thumbnail (take a random image in the first 20 seconds)
[kdenlive] / thumbnailer / westleypreview.cpp
1 /***************************************************************************
2    Copyright (C) 2006-2008
3    by Marco Gulino <marco@kmobiletools.org>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the
17    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19  ***************************************************************************/
20
21 #include <qfile.h>
22 #include <qimage.h>
23 #include <QtCore/QVarLengthArray>
24
25 #include <kstandarddirs.h>
26 #include <krandomsequence.h>
27 #include <qdatetime.h>
28 #include <QProcess>
29 #include <kdebug.h>
30 #include <ktempdir.h>
31 #include <kurl.h>
32 #include <qfileinfo.h>
33 #include <KTemporaryFile>
34
35 #include <unistd.h>
36
37 #include "westleypreview.h"
38
39 #define DBG_AREA
40
41 //#include "config.h"
42 extern "C" {
43     KDE_EXPORT ThumbCreator *new_creator() {
44         return new WestleyPreview;
45     }
46 }
47
48 WestleyPreview::WestleyPreview()
49         : m_rand(0), m_inigoprocess(0) {
50 }
51
52 WestleyPreview::~WestleyPreview() {
53     delete m_rand;
54     delete m_inigoprocess;
55 }
56
57 bool WestleyPreview::startAndWaitProcess(const QStringList &args) {
58     kDebug(DBG_AREA) << "westleypreview: starting process with args: " << args << endl;
59     m_inigoprocess->start(args.join(" "));
60     if (! m_inigoprocess->waitForStarted()) {
61         kDebug(DBG_AREA) << "westleypreview: PROCESS NOT STARTED!!! exiting\n";
62         return false;
63     }
64     if (! m_inigoprocess->waitForFinished()) {
65         kDebug(DBG_AREA) << "westleypreview: PROCESS DIDN'T FINISH!! exiting\n";
66         m_inigoprocess->close();
67         return false;
68     }
69     kDebug() << "westleypreview: process started and ended correctly\n";
70     return true;
71 }
72
73 bool WestleyPreview::create(const QString &path, int width, int height, QImage &img) {
74     QFileInfo fi(path);
75     playerBin = KStandardDirs::findExe("inigo");
76     if (playerBin.isEmpty()) {
77         kDebug(DBG_AREA) << "westleypreview: inigo not found, exiting.\n";
78         return false;
79     }
80
81     fileinfo.seconds = 0;
82     fileinfo.fps = 0;
83
84     m_rand = new KRandomSequence(QDateTime::currentDateTime().toTime_t());
85     m_inigoprocess = new QProcess();
86     KUrl furl(path);
87     kDebug(DBG_AREA) << "videopreview: url=" << furl << "; local:" << furl.isLocalFile() << endl;
88     fileinfo.towidth = width;
89     fileinfo.toheight = width * 3 / 4;
90     QImage pix;
91 //    if(furl.isLocalFile())
92 //    {
93     QStringList args;
94     //TODO: modify inigo so that it can return some infos about a westley clip (duration, track number,fps,...)
95     // without actually playing the file
96     /*
97         args << playerBin << QString("\"" + path + "\"") << "-file-info";
98
99         kDebug(DBG_AREA) << "videopreview: starting process: --_" << " " << args.join(" ") << "_--\n";
100         if (! startAndWaitProcess(args) ) return NULL;
101
102         QString information=QString(inigoprocess->readAllStandardOutput() );
103         QRegExp findInfos("ID_VIDEO_FPS=([\\d]*).*ID_LENGTH=([\\d]*).*");
104         if(findInfos.indexIn( information) == -1 )
105         {
106             kDebug(DBG_AREA) << "videopreview: No information found, exiting\n";
107             return NULL;
108         }
109         fileinfo.seconds =findInfos.cap(2).toInt();
110         fileinfo.fps=findInfos.cap(1).toInt();
111         */
112     fileinfo.seconds = 250;
113     fileinfo.fps = 25;
114
115     const int LASTTRY = 3;
116     for (int i = 0; i <= LASTTRY; i++) {
117         pix = getFrame(path);
118         if (!pix.isNull()) {
119             uint variance = imageVariance(pix);
120             kDebug(DBG_AREA) << "videopreview: " << QFileInfo(path).fileName() << " frame variance: " << variance << "; " <<
121             ((variance <= 40 && (i != LASTTRY - 1)) ? "!!!DROPPING!!!" : "GOOD :-)") << endl;
122             if (variance > 40 || i == LASTTRY - 1) break;
123         }
124     }
125     if (pix.isNull()) {
126         return false;
127     }
128
129     if (pix.depth() != 32)
130         img = pix.convertToFormat( QImage::Format_RGB32 );
131     else img = pix;
132     return true;
133 }
134
135 QImage WestleyPreview::getFrame(const QString &path) {
136     QStringList args;
137     const int START = 25;
138     const int RANGE = 500;
139     args.clear();
140     args << playerBin << "\"" + path + "\"";
141
142     unsigned long start = (unsigned long)(START + (m_rand->getDouble() * RANGE));
143     args << QString("in=%1").arg(start) << QString("out=%1").arg(start) << "-consumer";
144
145     KTemporaryFile temp;
146     temp.setSuffix(".png");
147     temp.open();
148     args << QString("avformat:%1").arg(temp.fileName()) << "vframes=1" << "f=rawvideo" << "vcodec=png" << QString("s=%1x%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
149     if (! startAndWaitProcess(args)) return QImage();
150     QImage retpix(temp.fileName());
151     temp.close();
152     return retpix;
153 }
154
155
156 uint WestleyPreview::imageVariance(QImage image) {
157     uint delta = 0;
158     uint avg = 0;
159     uint bytes = image.numBytes();
160     uint STEPS = bytes / 2;
161     QVarLengthArray<uchar> pivot(STEPS);
162     kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
163     uchar *bits = image.bits();
164     // First pass: get pivots and taking average
165     for (uint i = 0; i < STEPS ; i++) {
166         pivot[i] = bits[i*(bytes/STEPS)];
167         avg += pivot[i];
168     }
169     avg = avg / STEPS;
170     // Second Step: calculate delta (average?)
171     for (uint i = 0; i < STEPS; i++) {
172         int curdelta = abs(int(avg - pivot[i]));
173         delta += curdelta;
174     }
175     return delta / STEPS;
176 }
177
178 ThumbCreator::Flags WestleyPreview::flags() const
179 {
180     return None;
181 }
182
183
184 #include "westleypreview.moc"
185