Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ffprobe():
- def __init__(self,source,timeout=None,tcp=False):
- if timeout:
- if tcp:
- com =['timeout',str(timeout),'ffprobe', '-v', 'quiet', '-rtsp_transport', 'tcp', '-print_format', 'json', '-show_format', '-show_streams', source]
- else:
- com = ['timeout',str(timeout),'ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', source]
- else:
- if tcp:
- com =['ffprobe', '-v', 'quiet', '-rtsp_transport', 'tcp', '-print_format', 'json', '-show_format', '-show_streams', source]
- else:
- com = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', source]
- out,err,rc = execute(com)
- try:
- self.metadata = json.loads(out)
- except:
- self.metadata = None
- self.rc = rc
- self.video_streams = []
- self.audio_streams = []
- self.format = None
- if self.is_sane():
- for stream in self.metadata['streams']:
- if stream['codec_type'] == 'video':
- self.video_streams.append(stream)
- elif stream['codec_type'] == 'audio':
- self.audio_streams.append(stream)
- self.format = self.metadata['format']
- def is_sane(self):
- if not self.metadata or self.rc != 0 or len(self.metadata['streams'])<1:
- return False
- else:
- return True
- def get_resolution(self,num_video_stream = 0):
- if self.is_sane() and len(self.video_streams) >= num_video_stream:
- w = self.video_streams[num_video_stream]['width']
- h = self.video_streams[num_video_stream]['height']
- return w,h
- else:
- return None
- def get_bitrate(self,codec='video',num_stream=0):
- stream = None
- if codec == 'video':
- if len(self.video_streams) > num_stream:
- stream = self.video_streams[num_stream]
- elif codec == 'audio':
- if len(self.audio_streams) > num_stream:
- stream = self.audio_streams[num_stream]
- if stream and 'bit_rate' in stream.keys():
- return int(stream['bit_rate'])/1024
- else:
- return None
- def get_video_codec(self,num_stream=0):
- stream = None
- if len(self.video_streams) > num_stream:
- stream = self.video_streams[num_stream]
- if stream and 'codec_name' in stream.keys():
- return stream['codec_name']
- else:
- return None
- def get_audio_codec(self,num_stream=0):
- stream = None
- if len(self.audio_streams) > num_stream:
- stream = self.audio_streams[num_stream]
- if stream and 'codec_name' in stream.keys():
- return stream['codec_name']
- else:
- return None
- def get_format_name(self):
- if self.format and 'format_name' in self.format.keys():
- return self.format['format_name']
- else:
- return None
- def get_duration(self):
- if self.format and 'duration' in self.format.keys():
- return float(self.format['duration'])
- def get_start_time(self):
- if self.format and 'start_time' in self.format.keys():
- return float(self.format['start_time'])
- def get_fps(self,num_stream=0):
- if len(self.video_streams) > num_stream:
- stream = self.video_streams[num_stream]
- if stream and 'nb_frames' in stream.keys() and 'duration' in self.format.keys():
- return float(stream['nb_frames'])/float(self.format['duration'])
- else:
- return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement