summaryrefslogtreecommitdiffstats
path: root/src/videopreview.cpp
blob: 6a9c2adfac964af735d20c143b0e906867f25f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/***************************************************************************
   Copyright (C) 2006
   by Marco Gulino <marco@kmobiletools.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 ***************************************************************************/

#include "videopreview.h"

#include <ntqfile.h>
#include <ntqpixmap.h>
#include <ntqimage.h>

#include <kstandarddirs.h>
#include <kpixmapsplitter.h>
#include <kmimetype.h>
#include <ntqpainter.h>
#include <krandomsequence.h>
#include <ntqdatetime.h>
#include <ntqregexp.h>
#include "videopreview.h"
#include <ntqprocess.h>
#include <kdebug.h>
#include <ktempdir.h>
#include <kurl.h>
#include <math.h>
#include <ntqfileinfo.h>

#include "mplayerthumbs.h"

#include "config.h"
extern "C"
{
    KDE_EXPORT ThumbCreator *new_creator()
    {
        return new VideoPreview;
    }
}

VideoPreview::VideoPreview()
    : m_splitter(0),
      m_data(0),
      m_dataSize(0)
{
}

VideoPreview::~VideoPreview()
{
    delete m_splitter;
    delete [] m_data;
    delete tmpdir;
    delete rand;
    delete mplayerprocess;
}

bool VideoPreview::create(const TQString &path, int width, int height, TQImage &img)
{
    MPlayerThumbsCfg *cfg=new MPlayerThumbsCfg();
    TQFileInfo fi(path);
        kdDebug() << "videopreview: file extension=\"" << fi.extension().stripWhiteSpace() << "\"\n";
        if( fi.extension().stripWhiteSpace().length() && !cfg->noextensions().grep(fi.extension().stripWhiteSpace(), false)
         .isEmpty() )
    {
        delete cfg;
        kdDebug() << "videopreview: matched extension " << fi.extension().prepend('.') << "; exiting.\n";
        return false;
    }
    playerBin=cfg->mplayerbin();
    customargs=TQStringList::split(" ", cfg->customargs() );
    kdDebug() << "videopreview: customargs=" << cfg->customargs() << " ;;;; " << customargs << endl;
    delete cfg;
    if(playerBin.length()) kdDebug() << "videopreview: found playerbin from config: " << playerBin << endl;
    else
    {
        playerBin=TDEStandardDirs::findExe("mplayer-bin");
        if(!playerBin.length()) playerBin=TDEStandardDirs::findExe("mplayer");
        if(!playerBin.length())
        {
            kdDebug() << "videopreview: mplayer not found, exiting. Run mplayerthumbsconfig to setup mplayer path manually.\n";
            return false;
        }
        kdDebug() << "videopreview: found playerbin from path: " << playerBin << endl;
    }
    fileinfo.seconds=0;
    fileinfo.fps=0;
    tmpdir=new KTempDir();
    if(tmpdir->name().isNull() ) return false;

    rand=new KRandomSequence(TQDateTime::currentDateTime().toTime_t());
    mplayerprocess=new TQProcess();
    int flags=0;
    KURL furl(path);
    kdDebug() << "videopreview: url=" << furl << "; local:" << furl.isLocalFile() << endl;
    fileinfo.towidth=width;
    fileinfo.toheight=height;
    TQPixmap pix;
//    if(furl.isLocalFile())
//    {
        flags=framerandom;
        TQStringList args;
        args << playerBin << path << "-nocache" << "-identify" << "-vo" << "null" << "-frames" << "0"/* << "-nosound" */<< "-ao" << "null";
        args+= customargs;
        mplayerprocess->setArguments(args);
        mplayerprocess->setCommunication( TQProcess::Stdout );
        mplayerprocess->start();
        TQString lineout;
        TQRegExp findsecs("^ID_LENGTH=([\\d]*)");
        TQRegExp findfps("^ID_VIDEO_FPS=([\\d]*)");
        while (mplayerprocess->isRunning() ) usleep (10);
        do {
            lineout=mplayerprocess->readLineStdout();
            if(findsecs.search( lineout) != -1)
                fileinfo.seconds =findsecs.cap(1).toInt();
            if(findfps.search( lineout) != -1)
                fileinfo.fps=findfps.cap(1).toInt();
            if( fileinfo.fps!=0 && fileinfo.seconds!=0 ) break;
        } while (lineout != TQString::null );
        kdDebug() << "videopreview: find length=" << fileinfo.seconds << ", fps=" << fileinfo.fps << endl;
/*    } else
    {
        flags=frameend;
    }*/
#define LASTTRY 3
    for(int i=0; i<=LASTTRY; i++)
    {
        kdDebug() << "videopreview: try " << i << endl;
        pix=getFrame(path, ((i<LASTTRY) ? flags : framestart ) );
        if(!pix.isNull()) {
            uint variance=imageVariance(pix.convertToImage()/*.bits(),( (width+ 7) & ~0x7), width, height, 1 */);
            kdDebug() << "videopreview: " << TQFileInfo(path).fileName() << " frame variance: " << variance << "; " << 
                    ((variance<=40 && ( i!=LASTTRY-1))? "!!!DROPPING!!!" : "GOOD :-)" ) << endl;
            if(variance>40 || i==LASTTRY-1 ) break;
        }
    }
    if(pix.isNull() )
    {
        if(tmpdir) tmpdir->unlink();
        return false;
    }
    /** From videocreator.cpp - xine_artsplugin
    Copyright (C) 2002 Simon MacMullen
    Copyright (C) 2003 Ewald Snel <ewald@rambo.its.tudelft.nl>
     * */
//     TQPixmap pix( createThumbnail( &frame, width, height ) );
#ifdef STRIPS_SUPPORT
    TQPainter painter( &pix );
    TQPixmap sprocket;

    if (pix.height() < 60)
        sprocket = TQPixmap(locate( "data", "videothumbnail/sprocket-small.png" ));
    else if (pix.height() < 90)
        sprocket = TQPixmap(locate( "data", "videothumbnail/sprocket-medium.png" ));
    else
        sprocket = TQPixmap(locate( "data", "videothumbnail/sprocket-large.png" ));

    for (int y = 0; y < pix.height() + sprocket.height(); y += sprocket.height()) {
        painter.drawPixmap( 0, y, sprocket );
    }

    // End of xine-artsplugin code
#endif
    img = pix.convertToImage();

    if(tmpdir) tmpdir->unlink();
    return true;
}

TQPixmap VideoPreview::getFrame(const TQString &path, int flags)
{
    TQStringList args;
    kdDebug() << "videopreview: using flags " << flags << endl;
#define START ((fileinfo.seconds*15)/100)
#define END ((fileinfo.seconds*70)/100)
    mplayerprocess->setCommunication( 0 );
    mplayerprocess->clearArguments();
    args.clear();
    args << playerBin << path;
    if(fileinfo.towidth>fileinfo.toheight) fileinfo.toheight=-2; else fileinfo.towidth=-2;
//     switch( flags ){
//         case random
//     }
    if( flags & framerandom )
    {
        kdDebug() << "videopreview: framerandom\n";
        unsigned long start=(unsigned long)(START+(rand->getDouble() * (END - START) ) );
        args << "-ss" << TQString::number( start )
                << "-frames" << "4";
    } else if (flags & frameend )
    {
        kdDebug() << "videopreview: frameend\n";
        args << "-ss" << TQString::number( fileinfo.seconds - 10 )
                << "-frames" << "4";
    } else if (flags & framestart)
    {
        kdDebug() << "videopreview: framestart\n";
        if(!fileinfo.fps) fileinfo.fps=25; // if we've not autodetected a fps rate, let's assume 25fps.. even if it's wrong it shouldn't hurt.
        // If we can't skip to a random frame, let's try playing 10 seconds.
        args << "-frames" << TQString::number( fileinfo.fps*10 );
    }
    args << "-nocache" << "-idx" /*@TODO check if it's too slow..*/ << "-ao" << "null"/*"-nosound" << */<< "-speed" << "99"  /*<< "-sstep" << "5"*/
            << "-vo" << TQString("jpeg:outdir=%1").arg(tmpdir->name() ) << "-vf" << TQString("scale=%1:%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
    args+=customargs;
    kdDebug() << "videopreview: Starting MPlayer with" <<  args.join( " ") << endl;
//     return false;
    mplayerprocess->setArguments( args);
    mplayerprocess->start();
    while (mplayerprocess->isRunning() ) usleep (10);
    if (tmpdir->qDir()->entryList( "*.jpg" ).isEmpty() ) return false;

    TQString lastframe=tmpdir->qDir()->entryList( "*.jpg" ).last();
    kdDebug() << "videopreview: LastFrame==" << lastframe << endl;
    TQPixmap retpix(tmpdir->name().append( lastframe ));
    return retpix;
}

ThumbCreator::Flags VideoPreview::flags() const
{
    return (Flags)(DrawFrame);
}


uint VideoPreview::imageVariance(TQImage image )
{
    uint delta=0;
    uint avg=0;
    uint bytes=image.numBytes();
    uint STEPS=bytes/2;
    uchar pivot[STEPS];
    kdDebug() << "Using " << STEPS << " steps\n";
    uchar *bits=image.bits();
    // First pass: get pivots and taking average
    for( uint i=0; i<STEPS ; i++ ){
        pivot[i]=bits[i*(bytes/STEPS)];
        avg+=pivot[i];
    }
    avg=avg/STEPS;
    // Second Step: calculate delta (average?)
    for (uint i=0; i<STEPS; i++)
    {
        int curdelta=abs(int(avg-pivot[i]));
        delta+=curdelta;
    }
    return delta/STEPS;
}
#include "videopreview.moc"