tweak input params and gracefully kill ffmpeg

This commit is contained in:
blakeblackshear 2019-08-10 21:05:15 -05:00 committed by Blake Blackshear
parent 2ec45cd1b6
commit ebaa8fac01
2 changed files with 28 additions and 14 deletions

View File

@ -47,6 +47,12 @@ cameras:
# - -hwaccel_output_format # - -hwaccel_output_format
# - yuv420p # - 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: regions:
- size: 350 - size: 350
x_offset: 0 x_offset: 0

View File

@ -122,6 +122,17 @@ class Camera:
self.rtsp_url = get_rtsp_url(self.config['rtsp']) self.rtsp_url = get_rtsp_url(self.config['rtsp'])
self.take_frame = self.config.get('take_frame', 1) self.take_frame = self.config.get('take_frame', 1)
self.ffmpeg_hwaccel_args = self.config.get('ffmpeg_hwaccel_args', []) 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.regions = self.config['regions']
self.frame_shape = get_frame_shape(self.rtsp_url) self.frame_shape = get_frame_shape(self.rtsp_url)
self.frame_size = self.frame_shape[0] * self.frame_shape[1] * self.frame_shape[2] 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): def start_or_restart_capture(self):
if not self.ffmpeg_process is None: if not self.ffmpeg_process is None:
print("Killing the existing ffmpeg process...") 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.kill()
self.ffmpeg_process.wait() self.ffmpeg_process.wait()
print("Waiting for the capture thread to exit...") print("Waiting for the capture thread to exit...")
self.capture_thread.join() self.capture_thread.join()
self.ffmpeg_process = None self.ffmpeg_process = None
@ -215,21 +233,11 @@ class Camera:
ffmpeg_global_args = [ ffmpeg_global_args = [
'-hide_banner', '-loglevel', 'panic' '-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_cmd = (['ffmpeg'] +
ffmpeg_global_args + ffmpeg_global_args +
self.ffmpeg_hwaccel_args + self.ffmpeg_hwaccel_args +
ffmpeg_input_args + self.ffmpeg_input_args +
['-i', self.rtsp_url, ['-i', self.rtsp_url,
'-f', 'rawvideo', '-f', 'rawvideo',
'-pix_fmt', 'rgb24', '-pix_fmt', 'rgb24',