Skip to content

Commit

Permalink
Limit bitrate on rotation #338 #340
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlt8 committed Apr 20, 2022
1 parent d46d4aa commit e020559
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/rtsp_event.py
Expand Up @@ -60,7 +60,7 @@ def pub_start(self) -> None:
+ env_bool(f"RTSP_PATHS_{self.uri.upper()}_READPASS")
+ f"@{rtsp_addr}"
)
ffmpeg_cmd = f"ffmpeg -loglevel fatal -threads 1 -analyzeduration 1 -rtsp_transport tcp -i rtsp://{rtsp_addr}/{self.uri} -f:v h264 -an -f image2 -frames:v 1 -y {img_file}"
ffmpeg_cmd = f"ffmpeg -loglevel error -threads 1 -analyzeduration 50 -probesize 50 -rtsp_transport tcp -i rtsp://{rtsp_addr}/{self.uri} -an -f image2 -frames:v 1 -y {img_file}"
while True:
self.send_mqtt("state", "online")
if rtsp:
Expand Down
15 changes: 8 additions & 7 deletions app/wyze_bridge.py
Expand Up @@ -20,7 +20,7 @@

class WyzeBridge:
def __init__(self) -> None:
print("🚀 STARTING DOCKER-WYZE-BRIDGE v1.3.4 DEV 3\n")
print("🚀 STARTING DOCKER-WYZE-BRIDGE v1.3.4 DEV 4\n")
signal.signal(signal.SIGTERM, lambda n, f: self.clean_up())
self.hass: bool = bool(os.getenv("HASS"))
self.on_demand: bool = bool(os.getenv("ON_DEMAND"))
Expand Down Expand Up @@ -397,7 +397,7 @@ def start_tutk_stream(self, cam: wyzecam.WyzeCamera, stop_flag, connected) -> No
) as sess:
connected.set()
fps = check_cam_sess(sess, uri)
cmd = get_ffmpeg_cmd(uri, cam.product_model)
cmd = get_ffmpeg_cmd(uri, cam.product_model, fps)
with Popen(cmd, stdin=PIPE) as ffmpeg:
for frame in sess.recv_bridge_frame(
stop_flag, self.keep_bad_frames, self.timeout, fps
Expand Down Expand Up @@ -524,6 +524,7 @@ def check_cam_sess(sess: wyzecam.WyzeIOTCSession, uri: str) -> int:
if (force_fps := int(env_bool(f"FORCE_FPS_{uri}", 0))) and force_fps != fps:
log.info(f"Attempting to change FPS to {force_fps}")
sess.change_fps(force_fps)
fps = force_fps
if env_bool("DEBUG_LEVEL"):
log.info(f"[videoParm] {video_param}")
firmware = sess.camera.camera_info["basicInfo"].get("firmware", "NA")
Expand All @@ -537,25 +538,25 @@ def check_cam_sess(sess: wyzecam.WyzeIOTCSession, uri: str) -> int:
return fps or 15


def get_ffmpeg_cmd(uri: str, cam_model: str = None) -> list:
def get_ffmpeg_cmd(uri: str, cam_model: str = None, fps: int = None) -> list:
"""Return the ffmpeg cmd with options from the env."""
lib264 = (
["libx264", "-vf", "transpose=1"]
["libx264", "-vf", "transpose=1", "-b:v", "3000K"] # , "-r", f"{fps}"]
+ ["-tune", "zerolatency", "-preset", "ultrafast"]
+ ["-force_key_frames", "expr:gte(t,n_forced*2)"]
)
flags = "-fflags +genpts+flush_packets+nobuffer -flags +low_delay"
rotate = cam_model == "WYZEDB3" and env_bool("ROTATE_DOOR", False)
rtsp_ss = f"[select=v:f=rtsp]rtsp://0.0.0.0:8554/{uri.lower()}"
rtsp_ss = f"[select=v:f=rtsp:rtsp_transport=tcp]rtsp://0.0.0.0:8554/{uri.lower()}"
livestream = get_livestream_cmd(uri)
cmd = env_bool(f"FFMPEG_CMD_{uri}", env_bool("FFMPEG_CMD", "")).strip(
"'\"\n "
).format(cam_name=uri.lower(), CAM_NAME=uri).split() or (
["-loglevel", "verbose" if env_bool("DEBUG_FFMPEG") else "fatal"]
["-loglevel", "verbose" if env_bool("DEBUG_FFMPEG") else "error"]
+ env_bool(f"FFMPEG_FLAGS_{uri}", env_bool("FFMPEG_FLAGS", flags))
.strip("'\"\n ")
.split()
+ ["-analyzeduration", "1", "-f", "h264", "-i", "pipe:"]
+ ["-analyzeduration", "50", "-probesize", "50", "-f", "h264", "-i", "pipe:"]
+ (["-f", "lavfi", "-i", "anullsrc=cl=mono"] if livestream else [])
+ ["-c:v"]
+ (["copy"] if not rotate else lib264)
Expand Down
2 changes: 1 addition & 1 deletion app/wyzecam/iotc.py
Expand Up @@ -481,7 +481,7 @@ def update_frame_size_rate(self, bitrate: bool = False, fps: int = None) -> None
if bitrate:
param = mux.send_ioctl(K10020CheckCameraParams(3, 5)).result()
if fps and int(param.get("5", fps)) != fps:
warnings.warn(f"FPS param mismatch (framerate={param.get('5')})")
warnings.warn(f"FPS param mismatch (avRecv FPS={fps})")
if os.getenv("FPS_FIX"):
self.change_fps(fps)
return
Expand Down

0 comments on commit e020559

Please sign in to comment.