From 90044eacd2bcf1ed02b4cf9f8a0e760c50eb29f2 Mon Sep 17 00:00:00 2001 From: ReAnzu Date: Sat, 24 Feb 2018 01:06:11 -0600 Subject: [PATCH] Added auto conversion option for gif to mp4,webm * webm conversion is slow, but better quality than mp4 conversion and with a typically smaller filesize --- config.yaml.dist | 6 +++ server/szurubooru/api/post_api.py | 1 + server/szurubooru/func/images.py | 66 +++++++++++++++++++++++++++++++ server/szurubooru/func/mime.py | 46 ++++++++++++--------- server/szurubooru/func/posts.py | 36 ++++++++++++++++- server/szurubooru/func/util.py | 10 +++++ 6 files changed, 145 insertions(+), 20 deletions(-) diff --git a/config.yaml.dist b/config.yaml.dist index a53009c3..18eb8611 100644 --- a/config.yaml.dist +++ b/config.yaml.dist @@ -27,6 +27,12 @@ thumbnails: post_height: 300 +convert: + gif: + generate_webm: false + generate_mp4: false + + # used to send password reset e-mails smtp: host: # example: localhost diff --git a/server/szurubooru/api/post_api.py b/server/szurubooru/api/post_api.py index 27f10c16..1ae62af7 100644 --- a/server/szurubooru/api/post_api.py +++ b/server/szurubooru/api/post_api.py @@ -69,6 +69,7 @@ def create_post( posts.update_post_thumbnail(post, ctx.get_file('thumbnail')) ctx.session.add(post) ctx.session.flush() + posts.generate_alternate_formats(post, content) snapshots.create(post, None if anonymous else ctx.user) for tag in new_tags: snapshots.create(tag, None if anonymous else ctx.user) diff --git a/server/szurubooru/func/images.py b/server/szurubooru/func/images.py index 16587ed5..2611c97e 100644 --- a/server/szurubooru/func/images.py +++ b/server/szurubooru/func/images.py @@ -79,6 +79,72 @@ class Image: '-', ]) + def to_webm(self) -> bytes: + with util.create_temp_file_path(suffix='.log') as phase_log_path: + # Pass 1 + self._execute([ + '-i', '{path}', + '-pass', '1', + '-passlogfile', phase_log_path, + '-vcodec', 'libvpx-vp9', + '-crf', '4', + '-b:v', '2500K', + '-acodec', 'libvorbis', + '-f', 'webm', + '-y', '/dev/null' + ]) + + # Pass 2 + return self._execute([ + '-i', '{path}', + '-pass', '2', + '-passlogfile', phase_log_path, + '-vcodec', 'libvpx-vp9', + '-crf', '4', + '-b:v', '2500K', + '-acodec', 'libvorbis', + '-f', 'webm', + '-' + ]) + + def to_mp4(self) -> bytes: + + with util.create_temp_file_path(suffix='.dat') as mp4_temp_path: + + width = self.width + height = self.height + altered_dimensions = False + + if self.width % 2 != 0: + width = self.width - 1 + altered_dimensions = True + + if self.height % 2 != 0: + height = self.height - 1 + altered_dimensions = True + + args = [ + '-i', '{path}', + '-vcodec', 'libx264', + '-preset', 'slow', + '-crf', '22', + '-b:v', '200K', + '-profile:v', 'main', + '-pix_fmt', 'yuv420p', + '-acodec', 'aac', + '-f', 'mp4' + ] + + if altered_dimensions: + args = args + [ + '-filter:v', 'scale=\'%d:%d\'' % (width, height) + ] + + self._execute(args + ['-y', mp4_temp_path]) + + with open(mp4_temp_path, 'rb') as mp4_temp: + return mp4_temp.read() + def _execute(self, cli: List[str], program: str = 'ffmpeg') -> bytes: extension = mime.get_extension(mime.get_mime_type(self.content)) assert extension diff --git a/server/szurubooru/func/mime.py b/server/szurubooru/func/mime.py index c83f744e..12e358c0 100644 --- a/server/szurubooru/func/mime.py +++ b/server/szurubooru/func/mime.py @@ -1,58 +1,66 @@ import re from typing import Optional +APPLICATION_SWF = 'application/x-shockwave-flash' +IMAGE_JPEG = 'image/jpeg' +IMAGE_PNG = 'image/png' +IMAGE_GIF = 'image/gif' +VIDEO_WEBM = 'video/webm' +VIDEO_MP4 = 'video/mp4' +APPLICATION_OCTET_STREAM = 'application/octet-stream' + def get_mime_type(content: bytes) -> str: if not content: - return 'application/octet-stream' + return APPLICATION_OCTET_STREAM if content[0:3] in (b'CWS', b'FWS', b'ZWS'): - return 'application/x-shockwave-flash' + return APPLICATION_SWF if content[0:3] == b'\xFF\xD8\xFF': - return 'image/jpeg' + return IMAGE_JPEG if content[0:6] == b'\x89PNG\x0D\x0A': - return 'image/png' + return IMAGE_PNG if content[0:6] in (b'GIF87a', b'GIF89a'): - return 'image/gif' + return IMAGE_GIF if content[0:4] == b'\x1A\x45\xDF\xA3': - return 'video/webm' + return VIDEO_WEBM if content[4:12] in (b'ftypisom', b'ftypmp42'): - return 'video/mp4' + return VIDEO_MP4 - return 'application/octet-stream' + return APPLICATION_OCTET_STREAM def get_extension(mime_type: str) -> Optional[str]: extension_map = { - 'application/x-shockwave-flash': 'swf', - 'image/gif': 'gif', - 'image/jpeg': 'jpg', - 'image/png': 'png', - 'video/mp4': 'mp4', - 'video/webm': 'webm', - 'application/octet-stream': 'dat', + APPLICATION_SWF: 'swf', + IMAGE_GIF: 'gif', + IMAGE_JPEG: 'jpg', + IMAGE_PNG: 'png', + VIDEO_MP4: 'mp4', + VIDEO_WEBM: 'webm', + APPLICATION_OCTET_STREAM: 'dat', } return extension_map.get((mime_type or '').strip().lower(), None) def is_flash(mime_type: str) -> bool: - return mime_type.lower() == 'application/x-shockwave-flash' + return mime_type.lower() == APPLICATION_SWF def is_video(mime_type: str) -> bool: - return mime_type.lower() in ('application/ogg', 'video/mp4', 'video/webm') + return mime_type.lower() in ('application/ogg', VIDEO_MP4, VIDEO_WEBM) def is_image(mime_type: str) -> bool: - return mime_type.lower() in ('image/jpeg', 'image/png', 'image/gif') + return mime_type.lower() in (IMAGE_JPEG, IMAGE_PNG, IMAGE_GIF) def is_animated_gif(content: bytes) -> bool: pattern = b'\x21\xF9\x04[\x00-\xFF]{4}\x00[\x2C\x21]' - return get_mime_type(content) == 'image/gif' \ + return get_mime_type(content) == IMAGE_GIF \ and len(re.findall(pattern, content)) > 1 diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 406f6e18..580a0d42 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -5,7 +5,7 @@ import sqlalchemy as sa from szurubooru import config, db, model, errors, rest from szurubooru.func import ( users, scores, comments, tags, util, - mime, images, files, image_hash, serialization) + mime, images, files, image_hash, serialization, snapshots) EMPTY_PIXEL = ( @@ -429,6 +429,40 @@ def _sync_post_content(post: model.Post) -> None: generate_post_thumbnail(post) +def generate_alternate_formats(post: model.Post, content: bytes) -> None: + assert post + assert content + if mime.is_animated_gif(content): + tag_names = [tag_name.name for tag_name in [tag.names for tag in post.tags]] + new_posts = [] + + if config.config['convert']['gif']['generate_mp4']: + mp4_post, new_tags = create_post(images.Image(content).to_mp4(), tag_names, post.user) + update_post_flags(mp4_post, ['loop']) + update_post_safety(mp4_post, post.safety) + update_post_source(mp4_post, post.source) + new_posts += [(mp4_post, new_tags)] + + if config.config['convert']['gif']['generate_webm']: + webm_post, new_tags = create_post(images.Image(content).to_webm(), tag_names, post.user) + update_post_flags(webm_post, ['loop']) + update_post_safety(webm_post, post.safety) + update_post_source(webm_post, post.source) + new_posts += [(webm_post, new_tags)] + + db.session.flush() + + new_posts = list(filter(lambda i: i[0] is not None, new_posts)) + + for new_post, new_tags in new_posts: + snapshots.create(new_post, post.user) + for tag in new_tags: + snapshots.create(tag, post.user) + + new_relations = [p[0].post_id for p in new_posts] + update_post_relations(post, new_relations) if len(new_relations) > 0 else None + + def update_post_content(post: model.Post, content: Optional[bytes]) -> None: assert post if not content: diff --git a/server/szurubooru/func/util.py b/server/szurubooru/func/util.py index 61e975b6..ba2d4dc9 100644 --- a/server/szurubooru/func/util.py +++ b/server/szurubooru/func/util.py @@ -41,6 +41,16 @@ def create_temp_file(**kwargs: Any) -> Generator: os.remove(path) +@contextmanager +def create_temp_file_path(**kwargs: Any) -> Generator: + (descriptor, path) = tempfile.mkstemp(**kwargs) + os.close(descriptor) + try: + yield path + finally: + os.remove(path) + + def unalias_dict(source: List[Tuple[List[str], T]]) -> Dict[str, T]: output_dict = {} # type: Dict[str, T] for aliases, value in source: