Allow for thumbnails to be generated even on ffmpeg warnings

This commit is contained in:
ReAnzu 2018-02-24 01:48:10 -06:00
parent 90044eacd2
commit 4f612a6f64

View file

@ -33,10 +33,14 @@ class Image:
return self.info['streams'][0]['nb_read_frames'] return self.info['streams'][0]['nb_read_frames']
def resize_fill(self, width: int, height: int) -> None: def resize_fill(self, width: int, height: int) -> None:
width_greater = self.width > self.height
scale_param = "scale='%d:%d" % ((-1, height) if width_greater else (width, -1))
cli = [ cli = [
'-i', '{path}', '-i', '{path}',
'-f', 'image2', '-f', 'image2',
'-vf', _SCALE_FIT_FMT.format(width=width, height=height), '-filter:v', scale_param,
'-map', '0:v:0', '-map', '0:v:0',
'-vframes', '1', '-vframes', '1',
'-vcodec', 'png', '-vcodec', 'png',
@ -50,7 +54,7 @@ class Image:
'-ss', '-ss',
'%d' % math.floor(duration * 0.3), '%d' % math.floor(duration * 0.3),
] + cli ] + cli
content = self._execute(cli) content = self._execute(cli, ignore_error_if_data=True)
if not content: if not content:
raise errors.ProcessingError('Error while resizing image.') raise errors.ProcessingError('Error while resizing image.')
self.content = content self.content = content
@ -145,7 +149,7 @@ class Image:
with open(mp4_temp_path, 'rb') as mp4_temp: with open(mp4_temp_path, 'rb') as mp4_temp:
return mp4_temp.read() return mp4_temp.read()
def _execute(self, cli: List[str], program: str = 'ffmpeg') -> bytes: def _execute(self, cli: List[str], program: str = 'ffmpeg', ignore_error_if_data: bool = False) -> bytes:
extension = mime.get_extension(mime.get_mime_type(self.content)) extension = mime.get_extension(mime.get_mime_type(self.content))
assert extension assert extension
with util.create_temp_file(suffix='.' + extension) as handle: with util.create_temp_file(suffix='.' + extension) as handle:
@ -164,8 +168,9 @@ class Image:
'Failed to execute ffmpeg command (cli=%r, err=%r)', 'Failed to execute ffmpeg command (cli=%r, err=%r)',
' '.join(shlex.quote(arg) for arg in cli), ' '.join(shlex.quote(arg) for arg in cli),
err) err)
raise errors.ProcessingError( if (not ignore_error_if_data and len(out) > 0) or len(out) == 0:
'Error while processing image.\n' + err.decode('utf-8')) raise errors.ProcessingError(
'Error while processing image.\n' + err.decode('utf-8'))
return out return out
def _reload_info(self) -> None: def _reload_info(self) -> None: