1 /* Copyright Rafaël Carré (licence WTFPL) */
2 /* A video thumbnailer compatible with nautilus */
3 /* Copyright © 2007-2011 Rafaël Carré <funman@videolanorg> */
5 /* Works with : libvlc 1.2.0
6 gcc -pedantic -Wall -Werror -Wextra `pkg-config --cflags --libs libvlc` -lpthread
8 # to register the thumbnailer:
9 list=`grep ^Mime vlc.desktop|cut -d= -f2-|sed -e s/";"/\\\n/g -e s,/,@,g`
10 vid=`echo $mimes|grep ^vid`
13 key=/desktop/gnome/thumbnailers/$i/enable
14 gconftool-2 -t boolean -s $key true
15 gconftool-2 -t string -s $key "vlc-thumb -s %s %u %o"
29 #if !defined (_POSIX_CLOCK_SELECTION)
30 # define _POSIX_CLOCK_SELECTION (-1)
33 #if (_POSIX_CLOCK_SELECTION < 0)
34 # error Clock selection is not available!
39 /* position at which the snapshot is taken */
40 #define VLC_THUMBNAIL_POSITION (30./100.)
42 static void usage(const char *name
, int ret
)
44 fprintf(stderr
, "Usage: %s [-s width] <video> <output.png>\n", name
);
48 /* extracts options from command line */
49 static void cmdline(int argc
, const char **argv
, const char **in
,
50 char **out
, char **out_with_ext
, int *w
)
55 if (argc
!= 3 && argc
!= 5)
56 usage(argv
[0], argc
!= 2 || strcmp(argv
[1], "-h"));
61 if (strcmp(argv
[1], "-s"))
64 idx
+= 2; /* skip "-s width" */
69 *out
= strdup(argv
[idx
++]);
74 if (len
>= 4 && !strcmp(*out
+ len
- 4, ".png")) {
79 /* We need to add .png extension to filename,
80 * VLC relies on it to detect output format,
81 * and nautilus doesn't give filenames ending in .png */
83 *out_with_ext
= malloc(len
+ sizeof ".png");
86 strcpy(*out_with_ext
, *out
);
87 strcat(*out_with_ext
, ".png");
90 static libvlc_instance_t
*create_libvlc(void)
92 static const char* const args
[] = {
93 "--intf", "dummy", /* no interface */
94 "--vout", "dummy", /* we don't want video (output) */
95 "--no-audio", /* we don't want audio (decoding) */
96 "--no-video-title-show", /* nor the filename displayed */
97 "--no-stats", /* no stats */
98 "--no-sub-autodetect-file", /* we don't want subtitles */
99 "--no-inhibit", /* we don't want interfaces */
100 "--no-disable-screensaver", /* we don't want interfaces */
101 "--no-snapshot-preview", /* no blending in dummy vout */
103 "--verbose=2", /* full log */
107 return libvlc_new(sizeof args
/ sizeof *args
, args
);
110 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
111 static pthread_cond_t wait
;
114 static void callback(const libvlc_event_t
*ev
, void *param
)
119 pthread_mutex_lock(&lock
);
121 case libvlc_MediaPlayerPositionChanged
:
122 new_position
= ev
->u
.media_player_position_changed
.new_position
;
123 if (new_position
< VLC_THUMBNAIL_POSITION
* .9 /* 90% margin */)
125 case libvlc_MediaPlayerSnapshotTaken
:
127 pthread_cond_signal(&wait
);
133 pthread_mutex_unlock(&lock
);
136 static void event_wait(const char *error
)
140 #define VLC_THUMBNAIL_TIMEOUT 5.0 /* 5 secs */
142 clock_gettime(CLOCK_MONOTONIC
, &ts
);
143 ts
.tv_sec
+= VLC_THUMBNAIL_TIMEOUT
;
144 pthread_mutex_lock(&lock
);
145 ret
= done
? 0 : pthread_cond_timedwait(&wait
, &lock
, &ts
);
146 pthread_mutex_unlock(&lock
);
148 assert(!ret
|| ret
== ETIMEDOUT
);
152 "%s (timeout after %.2f secs!\n", error
, VLC_THUMBNAIL_TIMEOUT
);
157 static void set_position(libvlc_media_player_t
*mp
)
159 libvlc_event_manager_t
*em
= libvlc_media_player_event_manager(mp
);
162 libvlc_event_attach(em
, libvlc_MediaPlayerPositionChanged
, callback
, NULL
);
164 libvlc_media_player_set_position(mp
, VLC_THUMBNAIL_POSITION
);
165 event_wait("Couldn't set position");
166 libvlc_event_detach(em
, libvlc_MediaPlayerPositionChanged
, callback
, NULL
);
169 static void snapshot(libvlc_media_player_t
*mp
, int width
, char *out_with_ext
)
171 libvlc_event_manager_t
*em
= libvlc_media_player_event_manager(mp
);
174 libvlc_event_attach(em
, libvlc_MediaPlayerSnapshotTaken
, callback
, NULL
);
176 libvlc_video_take_snapshot(mp
, 0, out_with_ext
, width
, 0);
177 event_wait("Snapshot has not been written");
178 libvlc_event_detach(em
, libvlc_MediaPlayerSnapshotTaken
, callback
, NULL
);
181 int main(int argc
, const char **argv
)
184 char *out
, *out_with_ext
;
186 pthread_condattr_t attr
;
187 libvlc_instance_t
*libvlc
;
188 libvlc_media_player_t
*mp
;
191 /* mandatory to support UTF-8 filenames (provided the locale is well set)*/
192 setlocale(LC_ALL
, "");
194 cmdline(argc
, argv
, &in
, &out
, &out_with_ext
, &width
);
196 pthread_condattr_init(&attr
);
197 pthread_condattr_setclock(&attr
, CLOCK_MONOTONIC
);
198 pthread_cond_init(&wait
, &attr
);
199 pthread_condattr_destroy(&attr
);
202 libvlc
= create_libvlc();
205 m
= libvlc_media_new_path(libvlc
, in
);
208 mp
= libvlc_media_player_new_from_media(m
);
211 libvlc_media_player_play(mp
);
215 snapshot(mp
, width
, out_with_ext
);
217 libvlc_media_player_stop(mp
);
220 if (out
!= out_with_ext
) {
221 rename(out_with_ext
, out
);
226 libvlc_media_player_release(mp
);
227 libvlc_media_release(m
);
228 libvlc_release(libvlc
);
230 pthread_cond_destroy(&wait
);