LibVLC SampleCode Thumbnailer
This sample code will generate a thumbnail from any media. It can be used by nautilus instead of totem-video-thumbnailer
/* Copyright Rafaël Carré (licence WTFPL) */
#include <stdio.h>
#include <vlc/libvlc.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static libvlc_exception_t ex;
static void catch (void)
{
if (libvlc_exception_raised (&ex))
{
fprintf (stderr, "Exception: %s\n",
libvlc_exception_get_message (&ex));
exit(1);
}
libvlc_exception_clear (&ex);
}
#define TEMP "/tmp/vlc-video-thumbnailer.png"
#define usage() \
fprintf( stderr, \
"Usage: %s input output\n" \
"input being any video VLC can read, and output a png file\n" \
, argv[0] )
int main( int argc, const char **argv )
{
if( argc != 3 && argc != 5 )
{
usage();
if( argc == 2 && ( !strcmp( argv[1], "-h" ) || strcmp( argv[1], "--help") ) )
return 0;
else
return 1;
}
const char *input = argv[1];
const char *output = argv[2];
int i_width = 0;
int i_height = 0;
if( argc == 5 )
{
if( memcmp( argv[1], "-s", 3 ) != 0 )
{
usage();
return 1;
}
i_width = atoi( argv[2] );
input = argv[3];
output = argv[4];
}
libvlc_exception_init( &ex );
const char* const args[] = {
"-I", "dummy", /* no interface */
"--vout", "dummy", /* we don't want video */
"--no-audio", /* we don't want audio */
"--verbose=1", /* show only errors & warnings */
"--no-media-library", /* don't want that */
"--services-discovery", "", /* nor that */
"--no-video-title-show", /* nor the filename displayed */
"--no-sub-autodetect", /* don't want subtitles */
"--control", "", /* don't want interface */
"--extraintf", "" /* AT ALL ! */
};
int nargs = sizeof(args) / sizeof(args[0]);
libvlc_instance_t *libvlc = libvlc_new( nargs, args, &ex );
catch();
libvlc_media_instance_t *mi;
libvlc_media_descriptor_t *md;
md = libvlc_media_descriptor_new( libvlc, input, &ex );
catch();
mi = libvlc_media_instance_new_from_media_descriptor( md, &ex );
catch();
libvlc_media_descriptor_release( md );
libvlc_media_instance_play( mi, &ex );
catch();
/* go to position 30% to avoid introductions */
libvlc_media_instance_set_position( mi, 0.30, &ex );
catch();
int max_try = 5; /* wait maximum 5 seconds */
while(1)
{
/* leave vlc some time to start playing */
sleep(1); /* FIXME use a status change */
libvlc_video_take_snapshot( mi, TEMP, i_width, i_height, &ex );
if(max_try-- && libvlc_exception_raised (&ex) &&
!strcmp( libvlc_exception_get_message (&ex),
"No active video output" ))
libvlc_exception_clear (&ex);
else
break;
}
catch();
/* wait till the snapshot is written, could be done in a better way */
sleep(1);
char *mv;
if( asprintf( &mv, "mv %s %s", TEMP, output ) == -1 )
return 1;
system( mv );
free( mv );
libvlc_media_instance_stop( mi, &ex );
catch();
libvlc_media_instance_release( mi );
libvlc_release( libvlc, &ex );
catch();
return 0;
}