From 97e1f881d0117102e436b7d85ad71ad20e16128c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 28 Apr 2018 20:36:33 -0700 Subject: [PATCH] Move video thumbnailing code to helpers. --- etiquette/helpers.py | 30 ++++++++++++++++++++++++++++++ etiquette/objects.py | 35 +++++++++-------------------------- 2 files changed, 39 insertions(+), 26 deletions(-) diff --git a/etiquette/helpers.py b/etiquette/helpers.py index 4a21b3b..4439443 100644 --- a/etiquette/helpers.py +++ b/etiquette/helpers.py @@ -178,6 +178,36 @@ def generate_image_thumbnail(filepath, width, height): image = image.convert('RGB') return image +def generate_video_thumbnail(filepath, outfile, width, height, **special): + probe = constants.ffmpeg.probe(filepath) + if not probe.video: + return False + + size = fit_into_bounds( + image_width=probe.video.video_width, + image_height=probe.video.video_height, + frame_width=width, + frame_height=height, + ) + size = '%dx%d' % size + duration = probe.video.duration + + if 'timestamp' in special: + timestamp = special['timestamp'] + elif duration < 3: + timestamp = 0 + else: + timestamp = 2 + + constants.ffmpeg.thumbnail( + filepath, + outfile=outfile, + quality=2, + size=size, + time=timestamp, + ) + return True + def get_mimetype(filepath): ''' Extension to mimetypes.guess_type which uses my diff --git a/etiquette/objects.py b/etiquette/objects.py index 0531416..917010c 100644 --- a/etiquette/objects.py +++ b/etiquette/objects.py @@ -802,37 +802,20 @@ class Photo(ObjectBase): return_filepath = hopeful_filepath elif self.simple_mimetype == 'video' and constants.ffmpeg: - #print('video') self.photodb.log.debug('Thumbnailing %s', self.real_path.absolute_path) - probe = constants.ffmpeg.probe(self.real_path.absolute_path) try: - if probe.video: - size = helpers.fit_into_bounds( - image_width=probe.video.video_width, - image_height=probe.video.video_height, - frame_width=self.photodb.config['thumbnail_width'], - frame_height=self.photodb.config['thumbnail_height'], - ) - size = '%dx%d' % size - duration = probe.video.duration - if 'timestamp' in special: - timestamp = special['timestamp'] - else: - if duration < 3: - timestamp = 0 - else: - timestamp = 2 - constants.ffmpeg.thumbnail( - self.real_path.absolute_path, - outfile=hopeful_filepath.absolute_path, - quality=2, - size=size, - time=timestamp, - ) + success = helpers.generate_video_thumbnail( + self.real_path.absolute_path, + outfile=hopeful_filepath.absolute_path, + width=self.photodb.config['thumbnail_width'], + height=self.photodb.config['thumbnail_height'], + **special + ) except Exception: traceback.print_exc() else: - return_filepath = hopeful_filepath + if success: + return_filepath = hopeful_filepath if return_filepath != self.thumbnail: data = {