def duration_seconds(self): """ Returns the runtime duration of the video stream as a floating point number of seconds. Returns 0.0 if not a video stream. """ duration = 0.0 if self.is_video() or self.is_audio(): if self.__dict__['duration'] and self.__dict__['duration'] != 'N/A': try: duration = float(self.__dict__['duration']) except ValueError: raise FFProbeError('None numeric duration') elif self.__dict__[ 'TAG:DURATION'] and self.__dict__['TAG:DURATION'] != 'N/A': try: hours, minutes, seconds = self.__dict__[ 'TAG:DURATION'].split(":") duration = (int(hours) * 3600) + (int(minutes) * 60) + float(seconds) except ValueError: raise FFProbeError('None integer frame count') else: raise FFProbeError('Duration for stream not found') return duration
def get_r_frame_rate(self): """ Returns r_frame_rateas a str or float :param integer: bool :return: float """ # return self.__dict__['r_frame_rate'] try: if self.__dict__['r_frame_rate']: match = re.search("(\d+)(?:/(\d+))?", self.__dict__['r_frame_rate']) if match: if match.group(1): b = float(match.group(1)) if match.group(2): b = b / float(match.group(2)) # b = round(b, 4) else: raise ValueError else: b = float(self.__dict__['r_frame_rate']) except ValueError: raise FFProbeError('Nothing useful in r_frame_rate') return b
def get_avg_frame_rate(self): """ Returns avg_frame_rate :return: float """ try: if self.__dict__['avg_frame_rate']: match = re.search("(\d+)(?:/(\d+))?", self.__dict__['avg_frame_rate']) if match: if match.group(1): b = float(match.group(1)) if match.group(2): b = b / float(match.group(2)) # b = round(b, 4) else: raise ValueError else: b = float(self.__dict__['avg_frame_rate']) else: raise ValueError except ValueError: raise FFProbeError('Nothing useful in r_frame_rate') return b
def bit_rate(self): """ Returns bit_rate as an integer in bps """ b = 0 if self.__dict__['bit_rate'] and self.__dict__['bit_rate'] != 'N/A': try: b = int(self.__dict__['bit_rate']) except ValueError: raise FFProbeError('None integer bit_rate') elif self.__dict__['TAG:BPS'] and self.__dict__['TAG:BPS'] != 'N/A': try: b = int(self.__dict__['TAG:BPS']) except ValueError: raise FFProbeError('None integer bit_rate') return b
def container_bitrate(self) -> int: """ Returns container_bitrate as an integer in bps """ try: return int(self.__dict__.get('container_bitrate', '')) except ValueError: raise FFProbeError('None integer container_bitrate')
def bit_rate(self): """ Returns bit_rate as an integer in bps """ try: return int(self.__dict__.get('bit_rate', '')) except ValueError: raise FFProbeError('None integer bit_rate')
def bit_rate(self): """ Returns bit_rate as an integer in bps """ try: bit_rate = self.dstream.get('bit_rate', '') return int(bit_rate) if bit_rate else None except ValueError: raise FFProbeError('None integer bit_rate')
def channels(self): """ Returns number of channels for an audio stream """ try: channels = self.dstream.get('channels', '') return int(channels) if channels else None except ValueError: raise FFProbeError('None integer channels')
def width(self): """ If this is a video stream, return the width """ try: width = self.dstream.get('width', '') return int(width) if width else None except ValueError: raise FFProbeError('None integer width')
def height(self): """ If this is a video stream, return the height """ try: height = self.dstream.get('height', '') return int(height) if height else None except ValueError: raise FFProbeError('None integer height')
def frames(self): """ Returns the length of a video stream in frames. Returns 0 if not a video stream. """ frame_count = 0 if self.is_video() or self.is_audio(): if self.__dict__['nb_frames']: try: frame_count = int(self.__dict__['nb_frames']) except ValueError: raise FFProbeError('None integer frame count') return frame_count
def duration_seconds(self): """ Returns the runtime duration of the video stream as a floating point number of seconds. Returns 0.0 if not a video stream. """ duration = 0.0 if self.is_video() or self.is_audio(): if self.__dict__['duration']: try: duration = float(self.__dict__['duration']) except ValueError: raise FFProbeError('None numeric duration') return duration
def frames(self) -> int: """ Returns the length of a video stream in frames. Returns 0 if not a video stream. """ if self.is_video() or self.is_audio(): try: frame_count = int(self.__dict__.get('nb_frames', '')) except ValueError: raise FFProbeError('None integer frame count') else: frame_count = 0 return frame_count
def duration_seconds(self): """ Returns the runtime duration of the video stream as a floating point number of seconds. Returns 0.0 if not a video stream. """ if self.is_video() or self.is_audio(): try: duration = float(self.dstream.get('duration', '')) except ValueError: raise FFProbeError('None numeric duration') else: duration = 0.0 return duration
def frames(self): """ Returns the length of a video stream in frames. Returns 0 if not a video stream. """ frame_count = 0 if self.is_video() or self.is_audio(): if self.__dict__[ 'nb_frames'] and self.__dict__['nb_frames'] != 'N/A': try: frame_count = int(self.__dict__['nb_frames']) except ValueError: raise FFProbeError('None integer frame count') elif self.__dict__['TAG:NUMBER_OF_FRAMES'] and self.__dict__[ 'TAG:NUMBER_OF_FRAMES'] != 'N/A': try: frame_count = int(self.__dict__['TAG:NUMBER_OF_FRAMES']) except ValueError: raise FFProbeError('None integer frame count') else: raise FFProbeError('Frame count for stream not found') return frame_count
def frame_size(self): """ Returns the pixel frame size as an integer tuple (width,height) if the stream is a video stream. Returns None if it is not a video stream. """ size = None if self.is_video(): width = self.__dict__['width'] height = self.__dict__['height'] if width and height: try: size = (int(width), int(height)) except ValueError: raise FFProbeError("None integer size %s:%s" % (width, height)) return size
def duration_seconds(self): """ Returns the runtime duration of the video stream as a floating point number of seconds. Returns 0.0 if not a video stream. """ duration = 0.0 if self.is_video() or self.is_audio(): try: if self.__dict__['duration'] != "N/A": duration = float(self.__dict__['duration']) else: fps = self.get_avg_frame_rate() framenumbers = self.frames() duration = float(framenumbers / fps) except ValueError: raise FFProbeError('None numeric duration') return duration
def frames(self): """ Returns the length of a video stream in frames. Returns 0 if not a video stream. """ frame_count = 0 if self.is_video() or self.is_audio(): try: if self.__dict__['nb_frames'] != "N/A": frame_count = int(self.__dict__['nb_frames']) elif self.__dict__['TAG:DURATION']: # print(self.__dict__['TAG:DURATION']) match = re.search( "^(?:00|(\d\d)):?(?:00|(\d\d)):?(?:00|(\d\d))\.(\d+)", self.__dict__['TAG:DURATION']) if match: # 00:03:50.070000000 if match.group(1): hours = match.group(1) frame_count += int(hours) * 60 * 60 if match.group(2): mins = match.group(2) frame_count += int(mins) * 60 if match.group(3): seconds = match.group(3) frame_count += int(seconds) fps = self.get_r_frame_rate() frame_count = frame_count * fps # if match.group(4): # frame_count += int(match.group(4)) else: raise ValueError except ValueError: raise FFProbeError('None integer frame count') return frame_count
def aspect_ratio(self): """ Compute the aspect ratio as a decimal. Return the value None if this is not a video stream. """ if self.is_video(): # attempt to parse the aspect ratio used for display str_disp_aspect_ratio = self.dstream[ 'display_aspect_ratio'] if 'display_aspect_ratio' in self.dstream else None if str_disp_aspect_ratio and 2 == len( str_disp_aspect_ratio.split(':')): aspect_ratios = str_disp_aspect_ratio.split(':') width = int(aspect_ratios[0]) height = int(aspect_ratios[1]) if width and height: return width / height # if display aspect is undefined then assume the raw video height/width str_width = self.dstream['width'] str_height = self.dstream['height'] if str_width and str_height: try: width = int(str_width) if str_width else None height = int(str_height) if str_height else None if width and height: return width / height except ValueError: raise FFProbeError("None integer size {}:{}".format( str_width, str_height)) return None
def __init__(self, path_to_video): self.path_to_video = path_to_video try: with open(os.devnull, 'w') as tempf: subprocess.check_call(["ffprobe", "-h"], stdout=tempf, stderr=tempf) except FileNotFoundError: raise IOError('ffprobe not found.') if os.path.isfile(self.path_to_video): if platform.system() == 'Windows': cmd = [ "ffprobe", "-v quiet -print_format json -show_format -show_streams", self.path_to_video ] else: cmd = [ "ffprobe -v quiet -print_format json -show_format -show_streams " + pipes.quote(self.path_to_video) ] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) self.streams = [] self.video = [] self.audio = [] self.subtitle = [] self.attachment = [] jstdout = "" for line in iter(p.stdout.readline, b''): line = line.decode('UTF-8').strip() jstdout += line p.stdout.close() p.stderr.close() # read the streams jstreams = json.loads(jstdout) if 'streams' in jstreams: for dstream in jstreams['streams']: self.streams.append(FFStream(dstream)) for stream in self.streams: if stream.is_audio(): self.audio.append(stream) elif stream.is_video(): self.video.append(stream) elif stream.is_subtitle(): self.subtitle.append(stream) elif stream.is_attachment(): self.attachment.append(stream) else: raise FFProbeError('Unrecognized media file ' + self.path_to_video) else: raise IOError('No such media file ' + self.path_to_video)