capping ffmpeg stream error written

This commit is contained in:
Alberto-Audrix committed 2026-08-27 11:19:48 +07:00
1 parent 50752911db
commit ea88e92c99
1 file changed
+29
+29
View File
@@ -7,6 +7,7 @@ Left / right rectangular zones mark the left and right sack feeders.
A sack is counted once when its centroid enters a zone.
"""
import contextlib
import numpy as np
import cv2
import csv
@@ -14,6 +15,7 @@ import json
import os
import signal
import socket
import sys
import threading
import time
from collections import deque
@@ -923,9 +925,36 @@ def start_control_socket():
return srv
@contextlib.contextmanager
def _quiet_opencv_stderr():
"""Silence OpenCV/FFmpeg stderr (DESCRIBE failed, cap.cpp WARN, etc.)."""
saved_fd = None
devnull_fd = None
try:
stderr_fd = sys.stderr.fileno()
saved_fd = os.dup(stderr_fd)
devnull_fd = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull_fd, stderr_fd)
except (AttributeError, OSError, ValueError):
yield
return
try:
yield
finally:
os.dup2(saved_fd, stderr_fd)
os.close(saved_fd)
os.close(devnull_fd)
def open_capture(source):
if source.lower().startswith(("rtsp://", "http://")):
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = RTSP_FFMPEG_OPTIONS
# When STREAM_ERROR_LOG_MAX > 0, native OpenCV/FFmpeg stderr is suppressed;
# stream_error_log emits at most that many user-facing messages per outage.
if STREAM_ERROR_LOG_MAX > 0:
with _quiet_opencv_stderr():
cap = cv2.VideoCapture(source, cv2.CAP_FFMPEG)
else:
cap = cv2.VideoCapture(source, cv2.CAP_FFMPEG)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
return cap