summarylogtreecommitdiffstats
path: root/video_thumbnail-1.1.4.patch
blob: bf56146e56db62e878d44b38c467aeecc9980029 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
commit 5cc9da9a6b79fb03d357aeb43c8392f693a4da90
Author: Eduardo Rocha <folhabranca@gmail.com>
Date:   Wed Sep 3 19:53:04 2014 -0300

    Adding thumbnail generation support of a video file.

diff --git a/Makefile.am b/Makefile.am
index 9273aa5..a19afbb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -47,7 +47,7 @@ minidlnad_LDADD = \
 	@LIBEXIF_LIBS@ \
 	@LIBINTL@ \
 	@LIBICONV@ \
-	-lFLAC  $(flacoggflag) $(vorbisflag)
+	-lFLAC  $(flacoggflag) $(vorbisflag) @LIBFFMPEGTHUMBNAILER_LIBS@
 
 minidlnad_LDFLAGS = @STATIC_LDFLAGS@
 
diff --git a/albumart.c b/albumart.c
index 20ed14e..994b893 100644
--- a/albumart.c
+++ b/albumart.c
@@ -32,6 +32,10 @@
 
 #include <jpeglib.h>
 
+#ifdef THUMBNAIL_CREATION
+#include <libffmpegthumbnailer/videothumbnailerc.h>
+#endif
+
 #include "upnpglobalvars.h"
 #include "albumart.h"
 #include "sql.h"
@@ -345,14 +349,68 @@ found_file:
 	return NULL;
 }
 
+#ifdef THUMBNAIL_CREATION
+char *
+generate_thumbnail(const char * path)
+{
+	char *tfile = NULL;
+	video_thumbnailer *vt = NULL;
+	char cache_dir[MAXPATHLEN];
+
+	if( art_cache_exists(path, &tfile) )
+		return tfile;
+
+	if ( is_video(path) )
+	{
+
+		vt = video_thumbnailer_create();
+		if ( !vt )
+		{
+			free(tfile);
+			return 0;
+		}
+		vt->thumbnail_image_type = Jpeg;
+		vt->thumbnail_image_quality = runtime_vars.thumb_quality;
+		vt->thumbnail_size = runtime_vars.thumb_width;
+		vt->seek_percentage = 20;
+		vt->overlay_film_strip = (GETFLAG(THUMB_FILMSTRIP))?1:0;
+
+		DPRINTF(E_DEBUG, L_METADATA, "generating thumbnail: %s\n", path);
+
+		strncpyt(cache_dir, tfile, sizeof(cache_dir));
+		if ( !make_dir(dirname(cache_dir), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) &&
+			!video_thumbnailer_generate_thumbnail_to_file(vt, path, tfile) )
+		{
+			video_thumbnailer_destroy(vt);
+			return tfile;
+		}
+
+		video_thumbnailer_destroy(vt);
+	}
+	free(tfile);
+
+	return 0;
+}
+#endif
+
 int64_t
 find_album_art(const char *path, uint8_t *image_data, int image_size)
 {
 	char *album_art = NULL;
 	int64_t ret = 0;
 
-	if( (image_size && (album_art = check_embedded_art(path, image_data, image_size))) ||
-	    (album_art = check_for_album_file(path)) )
+	if(image_size)
+		album_art = check_embedded_art(path, image_data, image_size);
+
+	if(!album_art)
+		album_art = check_for_album_file(path);
+
+#ifdef THUMBNAIL_CREATION
+	if(!album_art && GETFLAG(THUMB_MASK))
+		album_art = generate_thumbnail(path);
+#endif
+
+	if(album_art)
 	{
 		ret = sql_get_int_field(db, "SELECT ID from ALBUM_ART where PATH = '%q'", album_art);
 		if( !ret )
diff --git a/configure.ac b/configure.ac
index 3a8a54c..8d448ac 100644
--- a/configure.ac
+++ b/configure.ac
@@ -607,6 +607,21 @@ AC_ARG_ENABLE(static,
         ]
 )
 
+AC_ARG_ENABLE(thumbnail,
+	[  --enable-thumbnail       enable video thumbnail generation using libffmpegthumbnailer],[
+	if test "$enableval" = "yes"; then
+		AC_DEFINE([THUMBNAIL_CREATION],[1],[Define to 1 if you want to enable video thumbnail generation])
+		PKG_CHECK_MODULES([LIBFFMPEGTHUMBNAILER], libffmpegthumbnailer, ,
+			AC_MSG_ERROR([Unable to find libffmpegthumbnailer]))
+		AC_SUBST([LIBFFMPEGTHUMBNAILER_CFLAGS])
+		AC_SUBST([LIBFFMPEGTHUMBNAILER_LIBS])
+        else
+                AC_MSG_RESULT([no])
+        fi
+        ],[
+                AC_MSG_RESULT([no])
+        ]
+)
 
 case "$target_os" in
 	darwin*)
diff --git a/inotify.c b/inotify.c
index 03aff5e..f9de7fb 100644
--- a/inotify.c
+++ b/inotify.c
@@ -600,6 +600,16 @@ inotify_remove_file(const char * path)
 		sql_exec(db, "DELETE from OBJECTS where DETAIL_ID = %lld", detailID);
 	}
 	snprintf(art_cache, sizeof(art_cache), "%s/art_cache%s", db_path, path);
+
+#ifdef THUMBNAIL_CREATION
+	/* Remove video thumbnails */
+	if ( is_video(path) )
+	{
+		char *vthumb = art_cache;
+		strcpy(strchr(vthumb, '\0')-4, ".jpg");
+	}
+#endif
+
 	remove(art_cache);
 
 	return 0;
@@ -649,7 +659,11 @@ start_inotify()
 	int length, i = 0;
 	char * esc_name = NULL;
 	struct stat st;
-        
+#ifdef THUMBNAIL_CREATION
+	char renpath_buf[PATH_MAX];
+	int cookie = 0;
+#endif
+
 	pollfds[0].fd = inotify_init();
 	pollfds[0].events = POLLIN;
 
@@ -710,6 +724,16 @@ start_inotify()
 				{
 					DPRINTF(E_DEBUG, L_INOTIFY,  "The directory %s was %s.\n",
 						path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "created"));
+#ifdef THUMBNAIL_CREATION
+					/* We do not want to regenerate the thumbnails if e rename a directory.
+					   We should keep at least four cookies/olddir since IN_MOVED_FROM/IN_MOVED_TO may
+					   not arrive in sequence, but one should cover most cases */
+					if (event->cookie == cookie && event->mask & IN_MOVED_TO)
+					{
+						DPRINTF(E_DEBUG, L_INOTIFY, "Directory rename: %s -> %s \n", renpath_buf, path_buf);
+						rename_artcache_dir(renpath_buf, path_buf);
+					}
+#endif
 					inotify_insert_directory(pollfds[0].fd, esc_name, path_buf);
 				}
 				else if ( (event->mask & (IN_CLOSE_WRITE|IN_MOVED_TO|IN_CREATE)) &&
@@ -741,7 +765,18 @@ start_inotify()
 						(event->mask & IN_ISDIR ? "directory" : "file"),
 						path_buf, (event->mask & IN_MOVED_FROM ? "moved away" : "deleted"));
 					if ( event->mask & IN_ISDIR )
+#ifdef THUMBNAIL_CREATION
+					{
+						if ( event->mask & IN_MOVED_FROM )
+						{
+							strncpy(renpath_buf, path_buf, sizeof(renpath_buf));
+							cookie = event->cookie;
+						}
+#endif
 						inotify_remove_directory(pollfds[0].fd, path_buf);
+#ifdef THUMBNAIL_CREATION
+					}
+#endif
 					else
 						inotify_remove_file(path_buf);
 				}
diff --git a/minidlna.c b/minidlna.c
index be34fe3..87a1140 100644
--- a/minidlna.c
+++ b/minidlna.c
@@ -532,6 +532,11 @@ init(int argc, char **argv)
 	runtime_vars.root_container = NULL;
 	runtime_vars.ifaces[0] = NULL;
 
+#ifdef THUMBNAIL_CREATION
+	runtime_vars.thumb_width = 160;
+	runtime_vars.thumb_quality = 8;
+#endif
+
 	/* read options file first since
 	 * command line arguments have final say */
 	if (readoptionsfile(optionsfile) < 0)
@@ -734,6 +739,30 @@ init(int argc, char **argv)
 			if (strtobool(ary_options[i].value))
 				SETFLAG(MERGE_MEDIA_DIRS_MASK);
 			break;
+#ifdef THUMBNAIL_CREATION
+		case ENABLE_THUMB:
+			if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) )
+				SETFLAG(THUMB_MASK);
+		break;
+		case THUMB_WIDTH:
+			runtime_vars.thumb_width = atoi(ary_options[i].value);
+			if (runtime_vars.thumb_width < 120)
+				runtime_vars.thumb_width = 120;
+			if (runtime_vars.thumb_width > 480)
+				runtime_vars.thumb_width = 480;
+			break;
+		case THUMB_QUALITY:
+			runtime_vars.thumb_quality = atoi(ary_options[i].value);
+			if (runtime_vars.thumb_quality < 5)
+				runtime_vars.thumb_quality = 5;
+			if (runtime_vars.thumb_quality > 30)
+				runtime_vars.thumb_quality = 30;
+		break;
+		case ENABLE_THUMB_FILMSTRIP:
+			if( (strcmp(ary_options[i].value, "yes") == 0) || atoi(ary_options[i].value) )
+				SETFLAG(THUMB_FILMSTRIP);
+		break;
+#endif
 		default:
 			DPRINTF(E_ERROR, L_GENERAL, "Unknown option in file %s\n",
 				optionsfile);
diff --git a/minidlna.conf b/minidlna.conf
index 7e00e89..e541a63 100644
--- a/minidlna.conf
+++ b/minidlna.conf
@@ -81,3 +81,15 @@ model_number=1
 # maximum number of simultaneous connections
 # note: many clients open several simultaneous connections while streaming
 #max_connections=50
+
+# Suport to Movie Thumbnail generation. To use this option, thumbnail generation must be enable at compile time.
+#enable_thumbnail=no
+
+# The width of the thumbnail image. Large images takes more time to generate.  To use this option, thumbnail generation must be enable at compile time.
+#thumbnail_width=160
+
+# Thumbnail Image quality. To use this option, thumbnail generation must be enable at compile time.
+#thumbnail_quality=8
+
+# Should the thumbnail have a film strip? To use this option, thumbnail generation must be enable at compile time.
+#enable_thumbnail_filmstrip=no
diff --git a/minidlnatypes.h b/minidlnatypes.h
index 6879b70..11befd9 100644
--- a/minidlnatypes.h
+++ b/minidlnatypes.h
@@ -51,6 +51,10 @@ struct runtime_vars_s {
 	int max_connections;	/* max number of simultaneous conenctions */
 	const char *root_container;	/* root ObjectID (instead of "0") */
 	const char *ifaces[MAX_LAN_ADDR];	/* list of configured network interfaces */
+#ifdef THUMBNAIL_CREATION
+	int thumb_width;
+	int thumb_quality;
+#endif
 };
 
 struct string_s {
diff --git a/options.c b/options.c
index 2fa8c06..21d1e78 100644
--- a/options.c
+++ b/options.c
@@ -64,7 +64,15 @@ static const struct {
 	{ USER_ACCOUNT, "user" },
 	{ FORCE_SORT_CRITERIA, "force_sort_criteria" },
 	{ MAX_CONNECTIONS, "max_connections" },
+#ifndef THUMBNAIL_CREATION
 	{ MERGE_MEDIA_DIRS, "merge_media_dirs" }
+#else
+	{ MERGE_MEDIA_DIRS, "merge_media_dirs" },
+	{ ENABLE_THUMB, "enable_thumbnail" },
+	{ THUMB_WIDTH, "thumbnail_width" },
+	{ THUMB_QUALITY, "thumbnail_quality" },
+	{ ENABLE_THUMB_FILMSTRIP, "enable_thumbnail_filmstrip" }
+#endif
 };
 
 int
diff --git a/options.h b/options.h
index 159255f..103c302 100644
--- a/options.h
+++ b/options.h
@@ -57,7 +57,15 @@ enum upnpconfigoptions {
 	USER_ACCOUNT,			/* user account to run as */
 	FORCE_SORT_CRITERIA,		/* force sorting by a given sort criteria */
 	MAX_CONNECTIONS,		/* maximum number of simultaneous connections */
+#ifndef THUMBNAIL_CREATION
 	MERGE_MEDIA_DIRS		/* don't add an extra directory level when there are multiple media dirs */
+#else
+	MERGE_MEDIA_DIRS,		/* don't add an extra directory level when there are multiple media dirs */
+	ENABLE_THUMB,                   /* enable thumbnail generation */
+	THUMB_WIDTH,                    /* thunbnail image with */
+	THUMB_QUALITY,                  /* thumnail image quality */
+	ENABLE_THUMB_FILMSTRIP          /* film strip overlay */
+#endif
 };
 
 /* readoptionsfile()
diff --git a/upnpglobalvars.h b/upnpglobalvars.h
index 224c374..457025d 100644
--- a/upnpglobalvars.h
+++ b/upnpglobalvars.h
@@ -191,6 +191,10 @@ extern uint32_t runtime_flags;
 #define NO_PLAYLIST_MASK      0x0008
 #define SYSTEMD_MASK          0x0010
 #define MERGE_MEDIA_DIRS_MASK 0x0020
+#ifdef THUMBNAIL_CREATION
+#define THUMB_MASK            0x0040
+#define THUMB_FILMSTRIP       0x0080
+#endif
 
 #define SETFLAG(mask)	runtime_flags |= mask
 #define GETFLAG(mask)	(runtime_flags & mask)
diff --git a/utils.c b/utils.c
index d728136..fbf042a 100644
--- a/utils.c
+++ b/utils.c
@@ -501,3 +501,16 @@ resolve_unknown_type(const char * path, media_types dir_type)
 	return type;
 }
 
+#ifdef THUMBNAIL_CREATION
+int
+rename_artcache_dir(const char * oldpath, const char * newpath)
+{
+	char old_artcache[PATH_MAX];
+	char new_artcache[PATH_MAX];
+
+	snprintf(old_artcache, sizeof(old_artcache), "%s/art_cache%s", db_path, oldpath);
+	snprintf(new_artcache, sizeof(old_artcache), "%s/art_cache%s", db_path, newpath);
+
+	return rename(old_artcache, new_artcache);
+}
+#endif
diff --git a/utils.h b/utils.h
index 433179e..809a5ce 100644
--- a/utils.h
+++ b/utils.h
@@ -95,5 +95,8 @@ const char *mime_to_ext(const char * mime);
 /* Others */
 int make_dir(char * path, mode_t mode);
 unsigned int DJBHash(uint8_t *data, int len);
+#ifdef THUMBNAIL_CREATION
+int rename_artcache_dir(const char * oldpath, const char * newpath);
+#endif
 
 #endif