Browse Source

fix the thumbnail generation for videos, add test

pull/18/head
Christophe-Marie Duquesne 13 years ago
parent
commit
45020ef100
  1. 17
      sigal/gallery.py
  2. 22
      sigal/video.py
  3. 15
      sigal/writer.py
  4. 20
      tests/test_gallery.py

17
sigal/gallery.py

@ -148,11 +148,17 @@ class PathsDb(object):
self.db[path]['thumbnail'] = f
return
# else simply return the 1st image
# else try returning the 1st image
if self.db[path]['img']:
self.db[path]['thumbnail'] = self.db[path]['img'][0]
else:
self.db[path]['thumbnail'] = ''
return
# last, we try the first vid
if self.db[path]['vid']:
self.db[path]['thumbnail'] = self.db[path]['vid'][0]
return
self.db[path]['thumbnail'] = ''
class Gallery(object):
@ -232,7 +238,7 @@ class Gallery(object):
if f in imglist:
outname = join(outpath, filename)
else:
outname = os.path.splitext(filename)[0] + '.webm'
outname = join(outpath, os.path.splitext(filename)[0] + '.webm')
if os.path.isfile(outname) and not self.force:
self.logger.info("%s exists - skipping", filename)
@ -290,7 +296,8 @@ def process_video(filepath, outpath, settings):
if settings['make_thumbs']:
thumb_name = join(outpath, get_thumb(settings, filename))
sigal.video.generate_thumbnail(outname, thumb_name,
settings['thumb_size'])
settings['thumb_size'], None, fit=settings['thumb_fit'],
options=settings['jpg_options'])
def get_metadata(path):

22
sigal/video.py

@ -22,6 +22,8 @@
from __future__ import with_statement
import subprocess
import os
import sigal.image
def generate_video(source, outname, size, options=[]):
"""Video processor
@ -31,20 +33,24 @@ def generate_video(source, outname, size, options=[]):
:param options: array of options passed to ffmpeg
"""
# We don't keep the h param, because we want to keep the same ratio as
# the original file (resizeToFit is not available in ffmpeg)
# see http://stackoverflow.com/questions/8218363/maintaining-ffmpeg-aspect-ratio
# Until we find a better solution, we don't keep the h param: We want
# to preserve the ratio of the original file (resizeToFit is not
# available in ffmpeg) see
# http://stackoverflow.com/questions/8218363/maintaining-ffmpeg-aspect-ratio
(w, h) = size
with open("/dev/null") as devnull:
subprocess.call(['ffmpeg', '-i', source, '-y', '-vf',
"scale=%i:trunc(ow/a/2)*2'" % w] + options + [outname],
stderr=devnull)
def generate_thumbnail(source, outname, box, options=[]):
def generate_thumbnail(source, outname, box, format, fit=True, options=None):
"Create a thumbnail image"
# See comment in the previous function
(w, h) = box
# 1) dump an image of the video
tmpfile = outname + ".tmp.jpg"
with open("/dev/null") as devnull:
subprocess.call(['ffmpeg', '-i', source, '-an', '-r', '1',
'-vframes', '1', '-y', '-vf', "scale=%i:trunc(ow/a/2)*2" % h]
+ options + [outname], stderr=devnull)
'-vframes', '1', '-y', tmpfile], stderr=devnull)
# 2) use the generate_thumbnail function from sigal.image
sigal.image.generate_thumbnail(tmpfile, outname, box, format, fit, options)
# 3) remove the image
os.unlink(tmpfile)

15
sigal/writer.py

@ -33,7 +33,8 @@ from distutils.dir_util import copy_tree
from jinja2 import Environment, FileSystemLoader, ChoiceLoader, PrefixLoader
from jinja2.exceptions import TemplateNotFound
from .image import generate_thumbnail
import sigal.image
import sigal.video
from .settings import get_thumb, get_orig
from .pkgmeta import __url__ as sigal_link
@ -161,9 +162,15 @@ class Writer(object):
# settings['make_thumbs'] is False)
if not os.path.exists(thumb_path):
source = os.path.join(self.output_dir, dpath, alb_thumb)
generate_thumbnail(
source, thumb_path, self.settings['thumb_size'], None,
fit=self.settings['thumb_fit'])
base, ext = os.path.splitext(source)
if ext in self.settings['img_ext_list']:
sigal.image.generate_thumbnail(
source, thumb_path, self.settings['thumb_size'], None,
fit=self.settings['thumb_fit'])
else:
sigal.video.generate_thumbnail(
source, thumb_path, self.settings['thumb_size'],
None, fit=self.settings['thumb_fit'])
ctx['albums'].append({
'url': d + '/' + self.url_ext,

20
tests/test_gallery.py

@ -13,17 +13,20 @@ REF = {
'dir1': {
'title': 'An example gallery',
'thumbnail': 'test1/11.jpg',
'img': '',
'img': [],
'vid': []
},
'dir1/test1': {
'title': 'An example sub-category',
'thumbnail': '11.jpg',
'img': ['11.jpg', 'archlinux-kiss-1024x640.png'],
'vid': []
},
'dir1/test2': {
'title': 'Test2',
'thumbnail': '21.jpg',
'img': ['21.jpg', '22.jpg'],
'vid': []
},
'dir2': {
'title': 'Another example gallery',
@ -31,18 +34,20 @@ REF = {
'img': ['exo20101028-b-full.jpg',
'm57_the_ring_nebula-587px.jpg',
'Hubble ultra deep field.jpg',
'Hubble Interacting Galaxy NGC 5257.jpg']
'Hubble Interacting Galaxy NGC 5257.jpg'],
'vid': [],
},
u'accentué': {
'title': u'Accentué',
'thumbnail': u'hélicoïde.jpg',
'img': [u'hélicoïde.jpg', 'superdupont_source_wikipedia_en.jpg']
'img': [u'hélicoïde.jpg', 'superdupont_source_wikipedia_en.jpg'],
'vid': [],
},
'video': {
'title': 'Video',
'thumbnail': '',
'thumbnail': 'stallman-software-freedom-day-low.ogv',
'img': [],
'video': []
'vid': ['stallman-software-freedom-day-low.ogv']
}
}
@ -91,6 +96,11 @@ def test_imglist(db):
assert set(db[p]['img']) == set(REF[p]['img'])
def test_vidlist(db):
for p in REF.keys():
assert set(db[p]['vid']) == set(REF[p]['vid'])
def test_get_subdir(paths):
assert set(paths.get_subdirs('dir1')) == set(['dir1/test1', 'dir1/test2'])
assert set(paths.get_subdirs('.')) == set(['dir1', 'dir2', 'dir1/test1',

Loading…
Cancel
Save