Write to pipe instead of encoding mpeg1

This commit is contained in:
Nick Mowen 2022-12-29 13:36:43 -07:00
parent b349fdb13f
commit d710b7dfd2
2 changed files with 48 additions and 58 deletions

View File

@ -3,6 +3,7 @@ import glob
import logging import logging
import math import math
import multiprocessing as mp import multiprocessing as mp
import os
import queue import queue
import signal import signal
import subprocess as sp import subprocess as sp
@ -28,6 +29,9 @@ logger = logging.getLogger(__name__)
class FFMpegConverter: class FFMpegConverter:
BIRDSEYE_PIPE = "/tmp/birdseye"
def __init__( def __init__(
self, self,
in_width: int, in_width: int,
@ -38,41 +42,17 @@ class FFMpegConverter:
birdseye_rtsp: bool = False, birdseye_rtsp: bool = False,
): ):
if birdseye_rtsp: if birdseye_rtsp:
ffmpeg_cmd = [ try:
"ffmpeg", os.mkfifo(self.BIRDSEYE_PIPE, mode=0o777)
"-f", stdin = os.open(self.BIRDSEYE_PIPE, os.O_RDONLY | os.O_NONBLOCK)
"rawvideo", self.bd_pipe = os.open(self.BIRDSEYE_PIPE, os.O_WRONLY)
"-pix_fmt", os.close(stdin)
"yuv420p", except Exception as e:
"-video_size", print(f"The exception is {e}")
f"{in_width}x{in_height}", self.bd_pipe = None
"-i",
"pipe:",
"-an",
"-f",
"rtp_mpegts",
"-s",
f"{out_width}x{out_height}",
"-codec:v",
"mpeg1video",
"-q",
f"{quality}",
"-bf",
"0",
"rtp://127.0.0.1:1998",
"-f",
"mpegts",
"-s",
f"{out_width}x{out_height}",
"-codec:v",
"mpeg1video",
"-q",
f"{quality}",
"-bf",
"0",
"pipe:",
]
else: else:
self.bd_pipe = None
ffmpeg_cmd = [ ffmpeg_cmd = [
"ffmpeg", "ffmpeg",
"-f", "-f",
@ -104,9 +84,16 @@ class FFMpegConverter:
start_new_session=True, start_new_session=True,
) )
def write(self, b): def write(self, b) -> None:
self.process.stdin.write(b) self.process.stdin.write(b)
if self.bd_pipe:
try:
os.write(self.bd_pipe, b)
except BrokenPipeError:
# catch error when no one is listening
return
def read(self, length): def read(self, length):
try: try:
return self.process.stdout.read1(length) return self.process.stdout.read1(length)
@ -114,6 +101,9 @@ class FFMpegConverter:
return False return False
def exit(self): def exit(self):
if self.bd_pipe:
os.close(self.bd_pipe)
self.process.terminate() self.process.terminate()
try: try:
self.process.communicate(timeout=30) self.process.communicate(timeout=30)

View File

@ -45,7 +45,7 @@ class RestreamApi:
if self.config.restream.birdseye: if self.config.restream.birdseye:
self.relays[ self.relays[
"birdseye" "birdseye"
] = "ffmpeg:rtp://127.0.0.1:1998#video=h264#audio=none" ] = f"exec:ffmpeg -hide_banner -f rawvideo -pix_fmt yuv420p -s {self.config.birdseye.width}x{self.config.birdseye.height} -i /tmp/birdseye -tune zerolatency -preset ultrafast -c:v libx264 -rtsp_transport tcp -f rtsp {{output}}"
for name, path in self.relays.items(): for name, path in self.relays.items():
params = {"src": path, "name": name} params = {"src": path, "name": name}