mirror of
https://github.com/koush/scrypted.git
synced 2026-03-20 16:40:24 +00:00
python-codecs: improve pyav for zero latency
This commit is contained in:
@@ -4,6 +4,8 @@ from typing import Any
|
||||
import vipsimage
|
||||
import pilimage
|
||||
from generator_common import createVideoFrame, createImageMediaObject
|
||||
import threading
|
||||
import asyncio
|
||||
|
||||
av = None
|
||||
try:
|
||||
@@ -29,21 +31,33 @@ async def generateVideoFramesLibav(mediaObject: scrypted_sdk.MediaObject, option
|
||||
|
||||
gray = options and options.get('format') == 'gray'
|
||||
|
||||
start = 0
|
||||
sampleQueue = asyncio.Queue(1)
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
def threadMain():
|
||||
try:
|
||||
for idx, frame in enumerate(container.decode(stream)):
|
||||
try:
|
||||
# non blocking put may fail if queue is not empty
|
||||
sampleQueue.put_nowait(frame)
|
||||
except:
|
||||
pass
|
||||
except:
|
||||
asyncio.run_coroutine_threadsafe(sampleQueue.put(None), loop = loop)
|
||||
|
||||
thread = threading.Thread(target=threadMain)
|
||||
thread.start()
|
||||
|
||||
try:
|
||||
vipsImage: vipsimage.VipsImage = None
|
||||
pilImage: pilimage.PILImage = None
|
||||
mo: scrypted_sdk.MediaObject = None
|
||||
|
||||
for idx, frame in enumerate(container.decode(stream)):
|
||||
now = time.time()
|
||||
if not start:
|
||||
start = now
|
||||
elapsed = now - start
|
||||
if (frame.time or 0) < elapsed - 0.500:
|
||||
# print('too slow, skipping frame')
|
||||
continue
|
||||
# print(frame)
|
||||
while True:
|
||||
frame = await sampleQueue.get()
|
||||
if not frame:
|
||||
break
|
||||
|
||||
if vipsimage.pyvips:
|
||||
if gray and frame.format.name.startswith('yuv') and frame.planes and len(frame.planes):
|
||||
vips = vipsimage.new_from_memory(memoryview(frame.planes[0]), frame.width, frame.height, 1)
|
||||
|
||||
Reference in New Issue
Block a user