Compare commits
No commits in common. "fdbf9b26108f813be8ac7f18a033be35aa3211c8" and "71eb252b8d9ba5cd607b368e3f55a2d2b6bdbf3e" have entirely different histories.
fdbf9b2610
...
71eb252b8d
@ -10,7 +10,6 @@ from wsgiref.handlers import format_date_time
|
|||||||
import av
|
import av
|
||||||
import av.datasets
|
import av.datasets
|
||||||
import fitz # PyMuPDF
|
import fitz # PyMuPDF
|
||||||
from av.streams import SideData
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from sanic import Blueprint, empty, raw
|
from sanic import Blueprint, empty, raw
|
||||||
from sanic.exceptions import NotFound
|
from sanic.exceptions import NotFound
|
||||||
@ -98,20 +97,24 @@ def process_pdf(path, *, maxsize, maxzoom, quality, page_number=0):
|
|||||||
def process_video(path, *, maxsize, quality):
|
def process_video(path, *, maxsize, quality):
|
||||||
with av.open(str(path)) as container:
|
with av.open(str(path)) as container:
|
||||||
stream = container.streams.video[0]
|
stream = container.streams.video[0]
|
||||||
|
rotation = (
|
||||||
|
stream.side_data
|
||||||
|
and stream.side_data.get(av.stream.SideData.DISPLAYMATRIX)
|
||||||
|
or 0
|
||||||
|
)
|
||||||
stream.codec_context.skip_frame = "NONKEY"
|
stream.codec_context.skip_frame = "NONKEY"
|
||||||
stream.codec_context.threads = 1
|
|
||||||
rot = stream.side_data and stream.side_data.get(SideData.DISPLAYMATRIX) or 0
|
|
||||||
container.seek(container.duration // 8)
|
container.seek(container.duration // 8)
|
||||||
img = next(container.decode(stream)).to_image()
|
frame = next(container.decode(stream))
|
||||||
del stream
|
img = frame.to_image()
|
||||||
|
del frame, stream
|
||||||
|
|
||||||
img.thumbnail((maxsize, maxsize))
|
img.thumbnail((maxsize, maxsize))
|
||||||
imgdata = io.BytesIO()
|
imgdata = io.BytesIO()
|
||||||
if rot:
|
if rotation:
|
||||||
img = img.rotate(rot, expand=True)
|
img = img.rotate(rotation, expand=True)
|
||||||
img.save(imgdata, format="webp", quality=quality, method=4)
|
img.save(imgdata, format="webp", quality=quality, method=4)
|
||||||
del img
|
del img
|
||||||
|
|
||||||
ret = imgdata.getvalue()
|
ret = imgdata.getvalue()
|
||||||
del imgdata
|
|
||||||
gc.collect()
|
gc.collect()
|
||||||
return ret
|
return ret
|
||||||
|
Loading…
x
Reference in New Issue
Block a user