Add video_converter to the settings dict
Also split arguments for image and video's generate_thumbnail functions.
This commit is contained in:
@@ -113,19 +113,21 @@ class Media(UnicodeMixin):
|
||||
"""Path to the thumbnail image (relative to the album directory)."""
|
||||
|
||||
if not isfile(self.thumb_path):
|
||||
# if thumbnail is missing (if settings['make_thumbs'] is False)
|
||||
if self.type == 'image':
|
||||
generator = image.generate_thumbnail
|
||||
elif self.type == 'video':
|
||||
generator = video.generate_thumbnail
|
||||
|
||||
self.logger.debug('Generating thumbnail for %r', self)
|
||||
path = (self.dst_path if os.path.exists(self.dst_path)
|
||||
else self.src_path)
|
||||
try:
|
||||
generator(path, self.thumb_path, self.settings['thumb_size'],
|
||||
self.settings['thumb_video_delay'],
|
||||
fit=self.settings['thumb_fit'])
|
||||
# if thumbnail is missing (if settings['make_thumbs'] is False)
|
||||
s = self.settings
|
||||
if self.type == 'image':
|
||||
image.generate_thumbnail(
|
||||
path, self.thumb_path, s['thumb_size'],
|
||||
fit=s['thumb_fit'])
|
||||
elif self.type == 'video':
|
||||
video.generate_thumbnail(
|
||||
path, self.thumb_path, s['thumb_size'],
|
||||
s['thumb_video_delay'], fit=s['thumb_fit'],
|
||||
converter=s['video_converter'])
|
||||
except Exception as e:
|
||||
self.logger.error('Failed to generate thumbnail: %s', e)
|
||||
return
|
||||
|
||||
@@ -137,7 +137,8 @@ def generate_image(source, outname, settings, options=None):
|
||||
save_image(img, outname, outformat, options=options, autoconvert=True)
|
||||
|
||||
|
||||
def generate_thumbnail(source, outname, box, delay, fit=True, options=None, thumb_fit_centering=(0.5, 0.5)):
|
||||
def generate_thumbnail(source, outname, box, fit=True, options=None,
|
||||
thumb_fit_centering=(0.5, 0.5)):
|
||||
"""Create a thumbnail image."""
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -145,7 +146,8 @@ def generate_thumbnail(source, outname, box, delay, fit=True, options=None, thum
|
||||
original_format = img.format
|
||||
|
||||
if fit:
|
||||
img = ImageOps.fit(img, box, PILImage.ANTIALIAS, centering=thumb_fit_centering)
|
||||
img = ImageOps.fit(img, box, PILImage.ANTIALIAS,
|
||||
centering=thumb_fit_centering)
|
||||
else:
|
||||
img.thumbnail(box, PILImage.ANTIALIAS)
|
||||
|
||||
@@ -176,7 +178,6 @@ def process_image(filepath, outpath, settings):
|
||||
if settings['make_thumbs']:
|
||||
thumb_name = os.path.join(outpath, get_thumb(settings, filename))
|
||||
generate_thumbnail(outname, thumb_name, settings['thumb_size'],
|
||||
settings['thumb_video_delay'],
|
||||
fit=settings['thumb_fit'], options=options,
|
||||
thumb_fit_centering=settings["thumb_fit_centering"])
|
||||
except Exception as e:
|
||||
|
||||
@@ -77,6 +77,7 @@ _DEFAULT_CONFIG = {
|
||||
'title': '',
|
||||
'use_assets_cdn': True,
|
||||
'use_orig': False,
|
||||
'video_converter': 'ffmpeg',
|
||||
'video_extensions': ['.mov', '.avi', '.mp4', '.webm', '.ogv', '.3gp'],
|
||||
'video_format': 'webm',
|
||||
'video_size': (480, 360),
|
||||
|
||||
@@ -61,13 +61,9 @@ def check_subprocess(cmd, source, outname):
|
||||
raise SubprocessException('Failed to process ' + source)
|
||||
|
||||
|
||||
def video_size(source, settings={}):
|
||||
"""Returns the dimensions of the video.
|
||||
:param source: path to a video
|
||||
:param settings: settings dict
|
||||
"""
|
||||
def video_size(source, converter='ffmpeg'):
|
||||
"""Returns the dimensions of the video."""
|
||||
|
||||
converter = settings.get('video_converter', 'ffmpeg')
|
||||
ret, stdout, stderr = call_subprocess([converter, '-i', source])
|
||||
pattern = re.compile(r'Stream.*Video.* ([0-9]+)x([0-9]+)')
|
||||
match = pattern.search(stderr)
|
||||
@@ -96,7 +92,8 @@ def generate_video(source, outname, settings, options=None):
|
||||
|
||||
# Don't transcode if source is in the required format and
|
||||
# has fitting datedimensions, copy instead.
|
||||
w_src, h_src = video_size(source, settings)
|
||||
converter = settings['video_converter']
|
||||
w_src, h_src = video_size(source, converter=converter)
|
||||
w_dst, h_dst = settings['video_size']
|
||||
logger.debug('Video size: %i, %i -> %i, %i', w_src, h_src, w_dst, h_dst)
|
||||
|
||||
@@ -122,7 +119,6 @@ def generate_video(source, outname, settings, options=None):
|
||||
|
||||
# Encoding options improved, thanks to
|
||||
# http://ffmpeg.org/trac/ffmpeg/wiki/vpxEncodingGuide
|
||||
converter = settings.get('video_converter', 'ffmpeg')
|
||||
cmd = [converter, '-i', source, '-y'] # -y to overwrite output files
|
||||
if options is not None:
|
||||
cmd += options
|
||||
@@ -132,22 +128,21 @@ def generate_video(source, outname, settings, options=None):
|
||||
check_subprocess(cmd, source, outname)
|
||||
|
||||
|
||||
def generate_thumbnail(source, outname, box, delay, fit=True, options=None):
|
||||
def generate_thumbnail(source, outname, box, delay, fit=True, options=None,
|
||||
converter='ffmpeg'):
|
||||
"""Create a thumbnail image for the video source, based on ffmpeg."""
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
tmpfile = outname + ".tmp.jpg"
|
||||
|
||||
# dump an image of the video
|
||||
converter = settings.get('video_converter', 'ffmpeg')
|
||||
cmd = [converter, '-i', source, '-an', '-r', '1',
|
||||
'-ss', delay, '-vframes', '1', '-y', tmpfile]
|
||||
logger.debug('Create thumbnail for video: %s', ' '.join(cmd))
|
||||
check_subprocess(cmd, source, outname)
|
||||
|
||||
# use the generate_thumbnail function from sigal.image
|
||||
image.generate_thumbnail(tmpfile, outname, box, delay, fit=fit,
|
||||
options=options)
|
||||
image.generate_thumbnail(tmpfile, outname, box, fit=fit, options=options)
|
||||
# remove the image
|
||||
os.unlink(tmpfile)
|
||||
|
||||
@@ -187,7 +182,8 @@ def process_video(filepath, outpath, settings):
|
||||
generate_thumbnail(
|
||||
outname, thumb_name, settings['thumb_size'],
|
||||
settings['thumb_video_delay'], fit=settings['thumb_fit'],
|
||||
options=settings['jpg_options'])
|
||||
options=settings['jpg_options'],
|
||||
converter=settings['video_converter'])
|
||||
except Exception:
|
||||
if logger.getEffectiveLevel() == logging.DEBUG:
|
||||
raise
|
||||
|
||||
@@ -117,15 +117,14 @@ def test_generate_thumbnail(tmpdir, image, path, wide_size, high_size):
|
||||
"Test the generate_thumbnail function."
|
||||
|
||||
dstfile = str(tmpdir.join(image))
|
||||
delay = 0
|
||||
for size in [(200, 150), (150, 200)]:
|
||||
generate_thumbnail(path, dstfile, size, delay)
|
||||
generate_thumbnail(path, dstfile, size)
|
||||
im = Image.open(dstfile)
|
||||
assert im.size == size
|
||||
|
||||
for size, thumb_size in [((200, 150), wide_size),
|
||||
((150, 200), high_size)]:
|
||||
generate_thumbnail(path, dstfile, size, delay, fit=False)
|
||||
generate_thumbnail(path, dstfile, size, fit=False)
|
||||
im = Image.open(dstfile)
|
||||
assert im.size == thumb_size
|
||||
|
||||
|
||||
Reference in New Issue
Block a user