fix: make ffmpeg support
Some checks failed
Build Worker Base and Application Images / check-base-changes (push) Successful in 7s
Build Worker Base and Application Images / build-base (push) Failing after 6m4s
Build Worker Base and Application Images / build-docker (push) Has been skipped
Build Worker Base and Application Images / deploy-stack (push) Has been skipped

This commit is contained in:
ziesorx 2025-09-25 23:23:56 +07:00
parent 0fc86fb72b
commit a45f76884f
3 changed files with 102 additions and 231 deletions

View file

@ -166,40 +166,17 @@ class RTSPReader:
logger.info(f"RTSP reader thread ended for camera {self.camera_id}")
def _initialize_capture(self) -> bool:
"""Initialize video capture with hardware acceleration (NVDEC) for 1280x720@6fps."""
"""Initialize video capture with FFmpeg hardware acceleration (CUVID/NVDEC) for 1280x720@6fps."""
try:
# Release previous capture if exists
if self.cap:
self.cap.release()
time.sleep(0.5)
logger.info(f"Initializing capture for camera {self.camera_id} with hardware acceleration")
logger.info(f"Initializing capture for camera {self.camera_id} with FFmpeg hardware acceleration")
hw_accel_success = False
# Method 1: Try GStreamer with NVDEC (most efficient on NVIDIA GPUs)
if not hw_accel_success:
try:
# Build GStreamer pipeline for NVIDIA hardware decoding
gst_pipeline = (
f"rtspsrc location={self.rtsp_url} protocols=tcp latency=100 ! "
"rtph264depay ! h264parse ! "
"nvv4l2decoder ! " # NVIDIA hardware decoder
"nvvideoconvert ! " # NVIDIA hardware color conversion
"video/x-raw,format=BGRx,width=1280,height=720 ! "
"videoconvert ! "
"video/x-raw,format=BGR ! "
"appsink max-buffers=1 drop=true sync=false"
)
logger.info(f"Attempting GStreamer NVDEC pipeline for camera {self.camera_id}")
self.cap = cv2.VideoCapture(gst_pipeline, cv2.CAP_GSTREAMER)
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Successfully using GStreamer with NVDEC hardware acceleration")
except Exception as e:
logger.debug(f"Camera {self.camera_id}: GStreamer NVDEC not available: {e}")
# Method 2: Try OpenCV CUDA VideoReader (if built with CUVID support)
# Method 1: Try OpenCV CUDA VideoReader (if built with CUVID support)
if not hw_accel_success:
try:
# Check if OpenCV was built with CUDA codec support
@ -220,7 +197,7 @@ class RTSPReader:
except Exception as e:
logger.debug(f"Camera {self.camera_id}: OpenCV CUDA not available: {e}")
# Method 3: Try FFMPEG with optimal hardware acceleration (CUVID/VAAPI)
# Method 2: Try FFmpeg with optimal hardware acceleration (CUVID/NVDEC)
if not hw_accel_success:
try:
from core.utils.ffmpeg_detector import get_optimal_rtsp_options
@ -230,7 +207,7 @@ class RTSPReader:
optimal_options = get_optimal_rtsp_options(self.rtsp_url)
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = optimal_options
logger.info(f"Attempting FFMPEG with detected hardware acceleration for camera {self.camera_id}")
logger.info(f"Attempting FFmpeg with detected hardware acceleration for camera {self.camera_id}")
logger.debug(f"Camera {self.camera_id}: Using FFmpeg options: {optimal_options}")
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG)
@ -239,45 +216,41 @@ class RTSPReader:
hw_accel_success = True
# Try to get backend info to confirm hardware acceleration
backend = self.cap.getBackendName()
logger.info(f"Camera {self.camera_id}: Using FFMPEG hardware acceleration (backend: {backend})")
logger.info(f"Camera {self.camera_id}: Using FFmpeg hardware acceleration (backend: {backend})")
except Exception as e:
logger.debug(f"Camera {self.camera_id}: FFMPEG hardware acceleration not available: {e}")
logger.debug(f"Camera {self.camera_id}: FFmpeg optimal hardware acceleration not available: {e}")
# Fallback to basic CUVID
try:
import os
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'video_codec;h264_cuvid|rtsp_transport;tcp|hwaccel;cuda'
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG)
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Using basic FFMPEG CUVID hardware acceleration")
except Exception as e2:
logger.debug(f"Camera {self.camera_id}: Basic CUVID also failed: {e2}")
# Method 4: Try VAAPI hardware acceleration (for Intel/AMD GPUs)
# Method 3: Try FFmpeg with basic NVIDIA CUVID
if not hw_accel_success:
try:
gst_pipeline = (
f"rtspsrc location={self.rtsp_url} protocols=tcp latency=100 ! "
"rtph264depay ! h264parse ! "
"vaapih264dec ! " # VAAPI hardware decoder
"vaapipostproc ! "
"video/x-raw,format=BGRx,width=1280,height=720 ! "
"videoconvert ! "
"video/x-raw,format=BGR ! "
"appsink max-buffers=1 drop=true sync=false"
)
logger.info(f"Attempting GStreamer VAAPI pipeline for camera {self.camera_id}")
self.cap = cv2.VideoCapture(gst_pipeline, cv2.CAP_GSTREAMER)
import os
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'video_codec;h264_cuvid|rtsp_transport;tcp|hwaccel;cuda|hwaccel_device;0'
logger.info(f"Attempting FFmpeg with basic CUVID for camera {self.camera_id}")
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG)
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Successfully using GStreamer with VAAPI hardware acceleration")
logger.info(f"Camera {self.camera_id}: Using FFmpeg CUVID hardware acceleration")
except Exception as e:
logger.debug(f"Camera {self.camera_id}: GStreamer VAAPI not available: {e}")
logger.debug(f"Camera {self.camera_id}: FFmpeg CUVID not available: {e}")
# Fallback: Standard FFMPEG with software decoding
# Method 4: Try FFmpeg with VAAPI (Intel/AMD GPUs)
if not hw_accel_success:
try:
import os
os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'hwaccel;vaapi|hwaccel_device;/dev/dri/renderD128|video_codec;h264|rtsp_transport;tcp'
logger.info(f"Attempting FFmpeg with VAAPI for camera {self.camera_id}")
self.cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG)
if self.cap.isOpened():
hw_accel_success = True
logger.info(f"Camera {self.camera_id}: Using FFmpeg VAAPI hardware acceleration")
except Exception as e:
logger.debug(f"Camera {self.camera_id}: FFmpeg VAAPI not available: {e}")
# Fallback: Standard FFmpeg with software decoding
if not hw_accel_success:
logger.warning(f"Camera {self.camera_id}: Hardware acceleration not available, falling back to software decoding")
import os