further advanced state api and stream checks
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import io
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import ffmpeg
|
||||
@@ -8,6 +9,8 @@ from PIL import Image
|
||||
from pydub import AudioSegment
|
||||
from pydub.playback import play
|
||||
|
||||
logger = logging.getLogger("lrc.tools.recorder_streams_sanity_checks")
|
||||
|
||||
|
||||
def is_single_color_image(image):
|
||||
single_color_image = True
|
||||
@@ -15,9 +18,9 @@ def is_single_color_image(image):
|
||||
count = 0
|
||||
color_channels = image.split()
|
||||
for c in color_channels: # r, g, b
|
||||
|
||||
hist = c.histogram()
|
||||
num_of_non_zero_values = len([v for v in hist if v != 0])
|
||||
logger.debug("color_channel: {}, num_of_non_zero_values (NON-BLACK): {}".format(c, num_of_non_zero_values))
|
||||
if num_of_non_zero_values > 1:
|
||||
single_color_image = False
|
||||
break
|
||||
@@ -48,6 +51,7 @@ def check_frame_is_valid(stream_url, raise_errors=True):
|
||||
':'.join([str(x) for x in color.values()]))
|
||||
except ffmpeg.Error as e:
|
||||
msg = "Could not connect to stream URL or other error!"
|
||||
logger.error(msg)
|
||||
try:
|
||||
msg += " ({})".format(e.stderr.decode('utf-8').strip().split("\n")[-1])
|
||||
except IndexError:
|
||||
@@ -58,8 +62,6 @@ def check_frame_is_valid(stream_url, raise_errors=True):
|
||||
return False, msg
|
||||
|
||||
|
||||
# print(check_frame_is_valid('rtsp://172.22.246.207/extron2'))
|
||||
|
||||
def check_if_audio_is_valid(stream_url, sample_length_sec=3, lower_alert_limit_dBFS=-40.0, raise_errors=True):
|
||||
file_name = tempfile.NamedTemporaryFile(suffix='.aac').name
|
||||
if os.path.exists(file_name):
|
||||
@@ -74,7 +76,7 @@ def check_if_audio_is_valid(stream_url, sample_length_sec=3, lower_alert_limit_d
|
||||
|
||||
sound = AudioSegment.from_file(file_name, "aac")
|
||||
# print(sound.dBFS)
|
||||
#play(sound)
|
||||
# play(sound)
|
||||
if sound.max_dBFS == -float('inf'):
|
||||
return False, "No active audio signal detected!"
|
||||
elif sound.max_dBFS < lower_alert_limit_dBFS:
|
||||
@@ -83,6 +85,7 @@ def check_if_audio_is_valid(stream_url, sample_length_sec=3, lower_alert_limit_d
|
||||
return True, "good audio signal detected! ({} max dBFS in {}s sample)".format(sound.max_dBFS, sample_length_sec)
|
||||
except ffmpeg.Error as e:
|
||||
msg = "Could not connect to stream URL or other error!"
|
||||
logger.error(msg)
|
||||
try:
|
||||
msg += " ({})".format(e.stderr.decode('utf-8').strip().split("\n")[-1])
|
||||
except IndexError:
|
||||
@@ -93,9 +96,6 @@ def check_if_audio_is_valid(stream_url, sample_length_sec=3, lower_alert_limit_d
|
||||
return False, msg
|
||||
|
||||
|
||||
print(check_if_audio_is_valid('rtsp://172.22.246.207/extron1'))
|
||||
|
||||
|
||||
"""
|
||||
Following code is not working correctly - ffmpeg parameters are wrong.
|
||||
"""
|
||||
@@ -113,3 +113,7 @@ def check_if_audio_is_valid_stream(stream_url, raise_errors=True):
|
||||
|
||||
# check_if_audio_is_valid('rtsp://172.22.246.207/extron1')
|
||||
"""
|
||||
|
||||
if __name__ == '__main__':
|
||||
# print(check_frame_is_valid('rtsp://172.22.246.207/extron2'))
|
||||
print(check_if_audio_is_valid('rtsp://172.22.246.207/extron1'))
|
||||
|
||||
Reference in New Issue
Block a user