Skip to content

Commit

Permalink
python-codecs: gstreamer now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Mar 23, 2024
1 parent 6182369 commit bb610f2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions plugins/python-codecs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/python-codecs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/python-codecs",
"version": "0.1.94",
"version": "0.1.95",
"description": "Python Codecs for Scrypted",
"keywords": [
"scrypted",
Expand Down
13 changes: 4 additions & 9 deletions plugins/python-codecs/src/gstreamer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import platform
from asyncio import Future
from typing import Any
from typing import Any, AsyncGenerator
from urllib.parse import urlparse

import scrypted_sdk
Expand All @@ -10,12 +9,8 @@
import vipsimage
from generator_common import createImageMediaObject, createVideoFrame
from gst_generator import Gst, createPipelineIterator
from gstreamer_postprocess import (
GstreamerPostProcess,
OpenGLPostProcess,
VaapiPostProcess,
getBands,
)
from gstreamer_postprocess import (GstreamerPostProcess, OpenGLPostProcess,
VaapiPostProcess, getBands)
from util import optional_chain


Expand Down Expand Up @@ -317,7 +312,7 @@ async def generateVideoFramesGstreamer(
h264Decoder: str = None,
h265Decoder: str = None,
postProcessPipeline: str = None,
) -> scrypted_sdk.VideoFrame:
) -> AsyncGenerator[scrypted_sdk.VideoFrame, Any]:
ffmpegInput: scrypted_sdk.FFmpegInput = (
await scrypted_sdk.mediaManager.convertMediaObjectToJSON(
mediaObject, scrypted_sdk.ScryptedMimeTypes.FFmpegInput.value
Expand Down
4 changes: 2 additions & 2 deletions plugins/python-codecs/src/libav.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time
import scrypted_sdk
from typing import Any
from typing import Any, AsyncGenerator
import vipsimage
import pilimage
from generator_common import createVideoFrame, createImageMediaObject
Expand All @@ -20,7 +20,7 @@
async def generateVideoFramesLibav(
mediaObject: scrypted_sdk.MediaObject,
options: scrypted_sdk.VideoFrameGeneratorOptions = None,
) -> scrypted_sdk.VideoFrame:
) -> AsyncGenerator[scrypted_sdk.VideoFrame, Any]:
ffmpegInput: scrypted_sdk.FFmpegInput = (
await scrypted_sdk.mediaManager.convertMediaObjectToJSON(
mediaObject, scrypted_sdk.ScryptedMimeTypes.FFmpegInput.value
Expand Down
36 changes: 23 additions & 13 deletions plugins/python-codecs/src/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import traceback
import asyncio
import time
import traceback
from typing import Any, AsyncGenerator, List, Union

import scrypted_sdk
from scrypted_sdk import Setting, SettingValue
from typing import Any, List, Union

import gstreamer
import libav
import vipsimage
import pilimage
import time
import vipsimage
import zygote
import os

Gst = None
try:
Expand Down Expand Up @@ -201,20 +202,23 @@ def multiprocess_exit():


class CodecFork:
def timeoutExit():
print("Frame yield timed out, exiting pipeline.")
multiprocess_exit()

async def generateVideoFrames(self, iter, src: str, firstFrameOnly=False):
start = time.time()
loop = asyncio.get_event_loop()

def timeoutExit():
print("Frame yield timed out, exiting pipeline.")
multiprocess_exit()

try:
while True:
self.timeout.cancel()
self.timeout = loop.call_later(10, self.timeoutExit)
data = await asyncio.wait_for(iter.__anext__(), timeout=10)
timeout = loop.call_later(10, timeoutExit)
self.timeout.cancel()
self.timeout = loop.call_later(10, self.timeoutExit)
yield data
timeout.cancel()

if firstFrameOnly:
break
except Exception:
Expand All @@ -231,7 +235,10 @@ async def generateVideoFramesGstreamer(
h264Decoder: str,
h265Decoder: str,
postProcessPipeline: str,
) -> scrypted_sdk.VideoFrame:
) -> AsyncGenerator[scrypted_sdk.VideoFrame, Any]:
loop = asyncio.get_event_loop()
self.timeout = loop.call_later(10, self.timeoutExit)

async for data in self.generateVideoFrames(
gstreamer.generateVideoFramesGstreamer(
mediaObject, options, h264Decoder, h265Decoder, postProcessPipeline
Expand All @@ -245,7 +252,10 @@ async def generateVideoFramesLibav(
self,
mediaObject: scrypted_sdk.MediaObject,
options: scrypted_sdk.VideoFrameGeneratorOptions = None,
) -> scrypted_sdk.VideoFrame:
) -> AsyncGenerator[scrypted_sdk.VideoFrame, Any]:
loop = asyncio.get_event_loop()
self.timeout = loop.call_later(10, self.timeoutExit)

async for data in self.generateVideoFrames(
libav.generateVideoFramesLibav(mediaObject, options),
"libav",
Expand Down

0 comments on commit bb610f2

Please sign in to comment.