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:
parent
d2b9cece28
commit
c88dfd228a
2 changed files with 32 additions and 13 deletions
|
@ -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,16 +53,21 @@ class Image(object):
|
||||||
])
|
])
|
||||||
|
|
||||||
def _execute(self, cli, program='ffmpeg'):
|
def _execute(self, cli, program='ffmpeg'):
|
||||||
proc = subprocess.Popen(
|
with util.create_temp_file() as handle:
|
||||||
[program, '-loglevel', '24'] + cli,
|
handle.write(self.content)
|
||||||
stdout=subprocess.PIPE,
|
handle.flush()
|
||||||
stdin=subprocess.PIPE,
|
cli = [program, '-loglevel', '24'] + cli
|
||||||
stderr=subprocess.PIPE)
|
cli = [part.format(path=handle.name) for part in cli]
|
||||||
out, err = proc.communicate(input=self.content)
|
proc = subprocess.Popen(
|
||||||
if proc.returncode != 0:
|
cli,
|
||||||
raise errors.ProcessingError(
|
stdout=subprocess.PIPE,
|
||||||
'Error while processing image.\n' + err.decode('utf-8'))
|
stdin=subprocess.PIPE,
|
||||||
return out
|
stderr=subprocess.PIPE)
|
||||||
|
out, err = proc.communicate(input=self.content)
|
||||||
|
if proc.returncode != 0:
|
||||||
|
raise errors.ProcessingError(
|
||||||
|
'Error while processing image.\n' + err.decode('utf-8'))
|
||||||
|
return out
|
||||||
|
|
||||||
def _reload_info(self):
|
def _reload_info(self):
|
||||||
self.info = json.loads(self._execute([
|
self.info = json.loads(self._execute([
|
||||||
|
|
|
@ -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():
|
||||||
|
|
Loading…
Reference in a new issue