You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
263 lines
9.2 KiB
263 lines
9.2 KiB
11 years ago
|
/***************************************************************************
|
||
|
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.
|
||
|
***************************************************************************/
|
||
|
|
||
3 years ago
|
#include "mplayer-thumbnailer.h"
|
||
11 years ago
|
|
||
6 years ago
|
#include <tqfile.h>
|
||
|
#include <tqpixmap.h>
|
||
|
#include <tqimage.h>
|
||
11 years ago
|
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <kpixmapsplitter.h>
|
||
|
#include <kmimetype.h>
|
||
6 years ago
|
#include <tqpainter.h>
|
||
11 years ago
|
#include <krandomsequence.h>
|
||
6 years ago
|
#include <tqdatetime.h>
|
||
|
#include <tqregexp.h>
|
||
|
|
||
|
#include <tqprocess.h>
|
||
11 years ago
|
#include <kdebug.h>
|
||
|
#include <ktempdir.h>
|
||
|
#include <kurl.h>
|
||
|
#include <math.h>
|
||
6 years ago
|
#include <tqfileinfo.h>
|
||
11 years ago
|
|
||
11 years ago
|
#include <cstdlib>
|
||
11 years ago
|
#include <unistd.h>
|
||
11 years ago
|
|
||
3 years ago
|
#include "mplayer-thumbnailer-cfg.h"
|
||
11 years ago
|
#include "config.h"
|
||
3 years ago
|
|
||
11 years ago
|
extern "C"
|
||
|
{
|
||
|
KDE_EXPORT ThumbCreator *new_creator()
|
||
|
{
|
||
3 years ago
|
return new MPlayerThumbnailer;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
MPlayerThumbnailer::MPlayerThumbnailer()
|
||
11 years ago
|
: m_splitter(0),
|
||
|
m_data(0),
|
||
|
m_dataSize(0)
|
||
|
{
|
||
|
}
|
||
|
|
||
3 years ago
|
MPlayerThumbnailer::~MPlayerThumbnailer()
|
||
11 years ago
|
{
|
||
|
delete m_splitter;
|
||
|
delete [] m_data;
|
||
|
delete tmpdir;
|
||
|
delete rand;
|
||
|
delete mplayerprocess;
|
||
|
}
|
||
|
|
||
3 years ago
|
bool MPlayerThumbnailer::create(const TQString &path, int width, int height, TQImage &img)
|
||
11 years ago
|
{
|
||
3 years ago
|
MPlayerThumbnailerCfg *cfg=new MPlayerThumbnailerCfg();
|
||
11 years ago
|
TQFileInfo fi(path);
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: file extension=\"" << fi.extension().stripWhiteSpace() << "\"\n";
|
||
11 years ago
|
if( fi.extension().stripWhiteSpace().length() && !cfg->noextensions().grep(fi.extension().stripWhiteSpace(), false)
|
||
|
.isEmpty() )
|
||
|
{
|
||
|
delete cfg;
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: matched extension " << fi.extension().prepend('.') << "; exiting.\n";
|
||
11 years ago
|
return false;
|
||
|
}
|
||
|
playerBin=cfg->mplayerbin();
|
||
11 years ago
|
customargs=TQStringList::split(" ", cfg->customargs() );
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: customargs=" << cfg->customargs() << " ;;;; " << customargs << endl;
|
||
11 years ago
|
delete cfg;
|
||
3 years ago
|
if(playerBin.length()) kdDebug() << "mplayer-thumbnailer: found playerbin from config: " << playerBin << endl;
|
||
11 years ago
|
else
|
||
|
{
|
||
11 years ago
|
playerBin=TDEStandardDirs::findExe("mplayer-bin");
|
||
|
if(!playerBin.length()) playerBin=TDEStandardDirs::findExe("mplayer");
|
||
11 years ago
|
if(!playerBin.length())
|
||
|
{
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: mplayer not found, exiting. Run mplayer-thumbnailer-config to setup mplayer path manually.\n";
|
||
11 years ago
|
return false;
|
||
|
}
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: found playerbin from path: " << playerBin << endl;
|
||
11 years ago
|
}
|
||
|
fileinfo.seconds=0;
|
||
|
fileinfo.fps=0;
|
||
|
tmpdir=new KTempDir();
|
||
|
if(tmpdir->name().isNull() ) return false;
|
||
|
|
||
11 years ago
|
rand=new KRandomSequence(TQDateTime::currentDateTime().toTime_t());
|
||
|
mplayerprocess=new TQProcess();
|
||
11 years ago
|
int flags=0;
|
||
|
KURL furl(path);
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: url=" << furl << "; local:" << furl.isLocalFile() << endl;
|
||
11 years ago
|
fileinfo.towidth=width;
|
||
|
fileinfo.toheight=height;
|
||
11 years ago
|
TQPixmap pix;
|
||
11 years ago
|
// if(furl.isLocalFile())
|
||
|
// {
|
||
|
flags=framerandom;
|
||
11 years ago
|
TQStringList args;
|
||
11 years ago
|
args << playerBin << path << "-nocache" << "-identify" << "-vo" << "null" << "-frames" << "0"/* << "-nosound" */<< "-ao" << "null";
|
||
|
args+= customargs;
|
||
|
mplayerprocess->setArguments(args);
|
||
11 years ago
|
mplayerprocess->setCommunication( TQProcess::Stdout );
|
||
11 years ago
|
mplayerprocess->start();
|
||
11 years ago
|
TQString lineout;
|
||
|
TQRegExp findsecs("^ID_LENGTH=([\\d]*)");
|
||
|
TQRegExp findfps("^ID_VIDEO_FPS=([\\d]*)");
|
||
11 years ago
|
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;
|
||
11 years ago
|
} while (lineout != TQString::null );
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: find length=" << fileinfo.seconds << ", fps=" << fileinfo.fps << endl;
|
||
11 years ago
|
/* } else
|
||
|
{
|
||
|
flags=frameend;
|
||
|
}*/
|
||
|
#define LASTTRY 3
|
||
|
for(int i=0; i<=LASTTRY; i++)
|
||
|
{
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: try " << i << endl;
|
||
11 years ago
|
pix=getFrame(path, ((i<LASTTRY) ? flags : framestart ) );
|
||
|
if(!pix.isNull()) {
|
||
|
uint variance=imageVariance(pix.convertToImage()/*.bits(),( (width+ 7) & ~0x7), width, height, 1 */);
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: " << TQFileInfo(path).fileName() << " frame variance: " << variance << "; " <<
|
||
11 years ago
|
((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>
|
||
|
* */
|
||
11 years ago
|
// TQPixmap pix( createThumbnail( &frame, width, height ) );
|
||
11 years ago
|
#ifdef STRIPS_SUPPORT
|
||
11 years ago
|
TQPainter painter( &pix );
|
||
|
TQPixmap sprocket;
|
||
11 years ago
|
|
||
|
if (pix.height() < 60)
|
||
3 years ago
|
sprocket = TQPixmap(locate( "data", "mplayer-thumbnailer/sprocket-small.png" ));
|
||
11 years ago
|
else if (pix.height() < 90)
|
||
3 years ago
|
sprocket = TQPixmap(locate( "data", "mplayer-thumbnailer/sprocket-medium.png" ));
|
||
11 years ago
|
else
|
||
3 years ago
|
sprocket = TQPixmap(locate( "data", "mplayer-thumbnailer/sprocket-large.png" ));
|
||
11 years ago
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
3 years ago
|
TQPixmap MPlayerThumbnailer::getFrame(const TQString &path, int flags)
|
||
11 years ago
|
{
|
||
11 years ago
|
TQStringList args;
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: using flags " << flags << endl;
|
||
11 years ago
|
#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 )
|
||
|
{
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: framerandom\n";
|
||
11 years ago
|
unsigned long start=(unsigned long)(START+(rand->getDouble() * (END - START) ) );
|
||
11 years ago
|
args << "-ss" << TQString::number( start )
|
||
11 years ago
|
<< "-frames" << "4";
|
||
|
} else if (flags & frameend )
|
||
|
{
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: frameend\n";
|
||
11 years ago
|
args << "-ss" << TQString::number( fileinfo.seconds - 10 )
|
||
11 years ago
|
<< "-frames" << "4";
|
||
|
} else if (flags & framestart)
|
||
|
{
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: framestart\n";
|
||
11 years ago
|
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.
|
||
11 years ago
|
args << "-frames" << TQString::number( fileinfo.fps*10 );
|
||
11 years ago
|
}
|
||
|
args << "-nocache" << "-idx" /*@TODO check if it's too slow..*/ << "-ao" << "null"/*"-nosound" << */<< "-speed" << "99" /*<< "-sstep" << "5"*/
|
||
11 years ago
|
<< "-vo" << TQString("jpeg:outdir=%1").arg(tmpdir->name() ) << "-vf" << TQString("scale=%1:%2").arg(fileinfo.towidth).arg(fileinfo.toheight);
|
||
11 years ago
|
args+=customargs;
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: Starting MPlayer with" << args.join( " ") << endl;
|
||
11 years ago
|
// return false;
|
||
|
mplayerprocess->setArguments( args);
|
||
|
mplayerprocess->start();
|
||
|
while (mplayerprocess->isRunning() ) usleep (10);
|
||
8 years ago
|
if (tmpdir->qDir()->entryList( "*.jpg" ).isEmpty() ) return 0;
|
||
11 years ago
|
|
||
11 years ago
|
TQString lastframe=tmpdir->qDir()->entryList( "*.jpg" ).last();
|
||
3 years ago
|
kdDebug() << "mplayer-thumbnailer: LastFrame==" << lastframe << endl;
|
||
11 years ago
|
TQPixmap retpix(tmpdir->name().append( lastframe ));
|
||
11 years ago
|
return retpix;
|
||
|
}
|
||
|
|
||
3 years ago
|
ThumbCreator::Flags MPlayerThumbnailer::flags() const
|
||
11 years ago
|
{
|
||
|
return (Flags)(DrawFrame);
|
||
|
}
|
||
|
|
||
|
|
||
3 years ago
|
uint MPlayerThumbnailer::imageVariance(TQImage image )
|
||
11 years ago
|
{
|
||
|
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;
|
||
|
}
|
||
|
|
||
3 years ago
|
#include "mplayer-thumbnailer.moc"
|