Compare commits
3 Commits
71eb252b8d
...
fdbf9b2610
Author | SHA1 | Date | |
---|---|---|---|
fdbf9b2610 | |||
8437c1f60e | |||
02c5e484b5 |
|
@ -10,6 +10,7 @@ from wsgiref.handlers import format_date_time
|
|||
import av
|
||||
import av.datasets
|
||||
import fitz # PyMuPDF
|
||||
from av.streams import SideData
|
||||
from PIL import Image
|
||||
from sanic import Blueprint, empty, raw
|
||||
from sanic.exceptions import NotFound
|
||||
|
@ -97,24 +98,20 @@ def process_pdf(path, *, maxsize, maxzoom, quality, page_number=0):
|
|||
def process_video(path, *, maxsize, quality):
|
||||
with av.open(str(path)) as container:
|
||||
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.threads = 1
|
||||
rot = stream.side_data and stream.side_data.get(SideData.DISPLAYMATRIX) or 0
|
||||
container.seek(container.duration // 8)
|
||||
frame = next(container.decode(stream))
|
||||
img = frame.to_image()
|
||||
del frame, stream
|
||||
|
||||
img.thumbnail((maxsize, maxsize))
|
||||
imgdata = io.BytesIO()
|
||||
if rotation:
|
||||
img = img.rotate(rotation, expand=True)
|
||||
img.save(imgdata, format="webp", quality=quality, method=4)
|
||||
del img
|
||||
img = next(container.decode(stream)).to_image()
|
||||
del stream
|
||||
|
||||
img.thumbnail((maxsize, maxsize))
|
||||
imgdata = io.BytesIO()
|
||||
if rot:
|
||||
img = img.rotate(rot, expand=True)
|
||||
img.save(imgdata, format="webp", quality=quality, method=4)
|
||||
del img
|
||||
ret = imgdata.getvalue()
|
||||
del imgdata
|
||||
gc.collect()
|
||||
return ret
|
||||
|
|
Loading…
Reference in New Issue
Block a user