server/images: replace pipes with temp files

ffmpeg's GIF demuxer needs the input stream to be seekable, which rules
pipes out.
This commit is contained in:
rr- 2016-05-20 21:34:02 +02:00
parent d2b9cece28
commit c88dfd228a
2 changed files with 32 additions and 13 deletions

View file

@ -1,6 +1,7 @@
import json import json
import subprocess import subprocess
from szurubooru import errors from szurubooru import errors
from szurubooru.func import util
_SCALE_FIT_FMT = \ _SCALE_FIT_FMT = \
r'scale=iw*max({width}/iw\,{height}/ih):ih*max({width}/iw\,{height}/ih)' r'scale=iw*max({width}/iw\,{height}/ih):ih*max({width}/iw\,{height}/ih)'
@ -24,7 +25,7 @@ class Image(object):
def resize_fill(self, width, height): def resize_fill(self, width, height):
self.content = self._execute([ self.content = self._execute([
'-i', '-', '-i', '{path}',
'-f', 'image2', '-f', 'image2',
'-vf', _SCALE_FIT_FMT.format(width=width, height=height), '-vf', _SCALE_FIT_FMT.format(width=width, height=height),
'-vframes', '1', '-vframes', '1',
@ -35,7 +36,7 @@ class Image(object):
def to_png(self): def to_png(self):
return self._execute([ return self._execute([
'-i', '-', '-i', '{path}',
'-f', 'image2', '-f', 'image2',
'-vframes', '1', '-vframes', '1',
'-vcodec', 'png', '-vcodec', 'png',
@ -44,7 +45,7 @@ class Image(object):
def to_jpeg(self): def to_jpeg(self):
return self._execute([ return self._execute([
'-i', '-', '-i', '{path}',
'-f', 'image2', '-f', 'image2',
'-vframes', '1', '-vframes', '1',
'-vcodec', 'mjpeg', '-vcodec', 'mjpeg',
@ -52,8 +53,13 @@ class Image(object):
]) ])
def _execute(self, cli, program='ffmpeg'): def _execute(self, cli, program='ffmpeg'):
with util.create_temp_file() as handle:
handle.write(self.content)
handle.flush()
cli = [program, '-loglevel', '24'] + cli
cli = [part.format(path=handle.name) for part in cli]
proc = subprocess.Popen( proc = subprocess.Popen(
[program, '-loglevel', '24'] + cli, cli,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)

View file

@ -1,8 +1,21 @@
import os
import datetime import datetime
import hashlib import hashlib
import re import re
import tempfile
from contextlib import contextmanager
from szurubooru.errors import ValidationError from szurubooru.errors import ValidationError
@contextmanager
def create_temp_file(**kwargs):
(handle, path) = tempfile.mkstemp(**kwargs)
os.close(handle)
try:
with open(path, 'r+b') as handle:
yield handle
finally:
os.remove(path)
def unalias_dict(input_dict): def unalias_dict(input_dict):
output_dict = {} output_dict = {}
for key_list, value in input_dict.items(): for key_list, value in input_dict.items():