/
rtsp_monitor.py
executable file
·130 lines (117 loc) · 4.74 KB
/
rtsp_monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/opt/pro/python279/bin/python
#encoding = utf-8
import log_helper, date_helper, datetime
import os, ConfigParser, time, subprocess, json
import mes
global __log__path
cf = ConfigParser.ConfigParser()
cf.read('/opt/scripts/Streamer_Monitor/config.ini')
__log__path = cf.get('global','rtsp_log_path')
rtsp_channel_list = cf.options("RTSP_Channel")
#print rtsp_channel_list
def logger(mes):
log_helper.get_logger(__log__path).info(mes)
def rtsp_monitor_url(channelurl):
urloption = "?ip=127.0.0.1&uid=00000000000000000000&cid=9800000000000000000000000000006&cdntoken=api_20160112"
rtsp_url = cf.get('RTSP_Channel', channelurl) + urloption
#print rtsp_url
return rtsp_url
def probe_stream(rtsp_url):
start = datetime.datetime.now()
cmd = "/usr/local/ffmpeg2/bin/ffprobe -v quiet -print_format json -show_streams -i " + '"' + rtsp_url + '"'
p = subprocess.Popen(cmd, bufsize=10000, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
#print cmd
while p.poll() is None:
time.sleep(0.1)
now = datetime.datetime.now()
if (now - start).seconds > 60:
#print (now - start).seconds
try:
p.terminate()
except Exception,e:
return None
#return None
return p.stdout.read()
#out = process.communicate()[0]
#if process.stdin:
# process.stdin.close()
#if process.stdout:
# process.stdout.close()
#if process.stderr:
# process.stderr.close()
#try:
# process.kill()
#except OSError:
# pass
#return out
#return p.stdout.read()
def stram_info_json_format(stream_info):
#print stream_info_json
#stream_info = json.loads(stream_info_json)
video_codec_type = stream_info['streams'][0]['codec_type']
video_codec_name = stream_info['streams'][0]['codec_name']
width = stream_info['streams'][0]["width"]
height = stream_info['streams'][0]['height']
video_profile = stream_info['streams'][0]['profile']
video_Resolution = str(height) + "x" + str(height)
profile_level = stream_info['streams'][0]['level']
video_start_time = stream_info['streams'][0]['start_time']
audio_codec_type = stream_info['streams'][1]['codec_type']
audio_codec_name = stream_info['streams'][1]['codec_name']
audio_channels = stream_info['streams'][1]['channels']
sample_rate = stream_info['streams'][1]['sample_rate']
audio_start_time = stream_info['streams'][1]['start_time']
m = {
'video_codec_type':video_codec_type,
'video_codec_name':video_codec_name,
'video_profile':video_profile,
'video_Resolution':video_Resolution,
'profile_level':profile_level,
'video_start_time':str(video_start_time),
'audio_codec_type':audio_codec_type,
'audio_codec_name':audio_codec_name,
'audio_channels':audio_channels,
'sample_rate':sample_rate,
'audio_start_time':str(audio_start_time),
}
return m
def main():
for rtsp_channel in rtsp_channel_list:
rtsp_url = rtsp_monitor_url(rtsp_channel)
#print rtsp_url
channel_info_json = probe_stream(rtsp_url)
#print channel_info_json
if channel_info_json:
channel_info = json.loads(channel_info_json)
#print channel_info
if channel_info:
print "%s normal" %rtsp_channel
#logger(channel_info_json)
m = stram_info_json_format(channel_info)
'''
print "video_codec_type: %(video_codec_type)s \n" \
"video_codec_name: %(video_codec_name)s \n" \
"video_profile: %(video_profile)s \n" \
"video_Resolution: %(video_Resolution)s \n" \
"profile_level: %(profile_level)s \n" \
"video_start_time: %(video_start_time)s \n" \
"audio_codec_type: %(audio_codec_type)s \n" \
"audio_codec_name: %(audio_codec_name)s \n" \
"audio_channels: %(audio_channels)s \n" \
"sample_rate: %(sample_rate)s \n" \
"audio_start_time:%(audio_start_time)s" % m
'''
else:
#print "%s breakdown" %rtsp_channel
rtsp = cf.get('RTSP_Channel', rtsp_channel)
mes.redis_process(rtsp_channel,'RTSP',rtsp=rtsp)
logger("RTSP %s breakdown" %rtsp_channel)
logger("%s json is %s" %(rtsp_channel,channel_info_json))
else:
#print "%s ***breakdown***" %rtsp_channel
rtsp = cf.get('RTSP_Channel', rtsp_channel)
#mes.redis_process(rtsp_channel,'RTSP',rtsp=rtsp)
logger("RTSP %s ***breakdown***" %rtsp_channel)
logger("%s json is %s" %(rtsp_channel,channel_info_json))
if __name__ == "__main__":
main()