Skip to content

Commit

Permalink
python-codecs: improve pyav for zero latency
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Oct 23, 2023
1 parent 672f01f commit 0e797c6
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions plugins/python-codecs/src/libav.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import vipsimage
import pilimage
from generator_common import createVideoFrame, createImageMediaObject
import threading
import asyncio

av = None
try:
Expand All @@ -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)
Expand Down

0 comments on commit 0e797c6

Please sign in to comment.