From ebaa8fac01665236e826c89dccd60914c2e2a8b5 Mon Sep 17 00:00:00 2001 From: blakeblackshear Date: Sat, 10 Aug 2019 21:05:15 -0500 Subject: [PATCH] tweak input params and gracefully kill ffmpeg --- config/config.yml | 6 ++++++ frigate/video.py | 36 ++++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/config/config.yml b/config/config.yml index 1294d0b82..ab96ca1ad 100644 --- a/config/config.yml +++ b/config/config.yml @@ -46,6 +46,12 @@ cameras: # - /dev/dri/renderD128 # - -hwaccel_output_format # - yuv420p + + ################ + # Optional custom input args. Some cameras may need custom ffmpeg params to work reliably. Specifying + # these will replace the default input params. + ################ + # ffmpeg_input_args: [] regions: - size: 350 diff --git a/frigate/video.py b/frigate/video.py index cbf7d67eb..de991245d 100644 --- a/frigate/video.py +++ b/frigate/video.py @@ -122,6 +122,17 @@ class Camera: self.rtsp_url = get_rtsp_url(self.config['rtsp']) self.take_frame = self.config.get('take_frame', 1) self.ffmpeg_hwaccel_args = self.config.get('ffmpeg_hwaccel_args', []) + self.ffmpeg_input_args = self.config.get('ffmpeg_input_args', [ + '-avoid_negative_ts', 'make_zero', + '-fflags', 'nobuffer', + '-flags', 'low_delay', + '-strict', 'experimental', + '-fflags', '+genpts+discardcorrupt', + '-vsync', 'drop', + '-rtsp_transport', 'tcp', + '-stimeout', '5000000', + '-use_wallclock_as_timestamps', '1' + ]) self.regions = self.config['regions'] self.frame_shape = get_frame_shape(self.rtsp_url) self.frame_size = self.frame_shape[0] * self.frame_shape[1] * self.frame_shape[2] @@ -194,9 +205,16 @@ class Camera: def start_or_restart_capture(self): if not self.ffmpeg_process is None: - print("Killing the existing ffmpeg process...") - self.ffmpeg_process.kill() - self.ffmpeg_process.wait() + print("Terminating the existing ffmpeg process...") + self.ffmpeg_process.terminate() + try: + print("Waiting for ffmpeg to exit gracefully...") + self.ffmpeg_process.wait(timeout=30) + except sp.TimeoutExpired: + print("FFmpeg didnt exit. Force killing...") + self.ffmpeg_process.kill() + self.ffmpeg_process.wait() + print("Waiting for the capture thread to exit...") self.capture_thread.join() self.ffmpeg_process = None @@ -215,21 +233,11 @@ class Camera: ffmpeg_global_args = [ '-hide_banner', '-loglevel', 'panic' ] - ffmpeg_input_args = [ - '-avoid_negative_ts', 'make_zero', - '-fflags', 'nobuffer', - '-flags', 'low_delay', - '-strict', 'experimental', - '-fflags', '+genpts', - '-rtsp_transport', 'tcp', - '-stimeout', '5000000', - '-use_wallclock_as_timestamps', '1' - ] ffmpeg_cmd = (['ffmpeg'] + ffmpeg_global_args + self.ffmpeg_hwaccel_args + - ffmpeg_input_args + + self.ffmpeg_input_args + ['-i', self.rtsp_url, '-f', 'rawvideo', '-pix_fmt', 'rgb24',