def get_frame_attribute(fileOne, attribute, default=None, audio=False): ffmpegcommand = get_ffprobe_tool() results = [] errors = _run_command([ffmpegcommand, '-loglevel', 'error', '-show_entries', 'stream={},codec_type'.format(attribute), fileOne], outputCollector=results) if len(results) > 0: streams = [] for result in results: if result.find('[STREAM]') >= 0: streams.append(dict()) continue parts = result.split('=') if len(parts) < 2: continue streams[-1][parts[0]] = parts[1] for stream in streams: if (audio and getValue(stream,'codec_type','na') == 'audio') or \ (not audio and getValue(stream,'codec_type','na') != 'audio'): return stream[attribute] return default
def process_meta_from_streams(stream, errorstream): streams = [] meta = {} video_stream = None while True: line = stream.readline() if line is None or len(line) == 0: break if '[STREAM]' in line or '[FORMAT]' in line: while True: line = stream.readline() if '[/FORMAT]' in line: bit_rate = getValue(meta,'bit_rate',None) if bit_rate is not None and video_stream is not None: streams[video_stream]['bit_rate'] = bit_rate break if '[/STREAM]' in line: index = __add_meta_to_list(streams, meta, index_id ='index') if getValue(meta,'codec_type','na') == 'video' and video_stream is None: video_stream = index meta = {} break elif '=' not in line: continue else: setting = line.split('=') if len(setting) < 2 or len(setting[1]) == 0: continue if setting[0] not in meta: meta[setting[0]] = ''.join(setting[1:]).strip() return streams
def get_video_frame_rate_from_meta(meta, frames): index = get_stream_indices_of_type(meta, 'video')[0] r = getValue(meta[index],'r_frame_rate','30/1') avg = getValue(meta[index],'avg_frame_rate',r) parts = avg.split('/') if parts[0] == 'N': parts = r.split('/') if parts[0] != 'N': return float(parts[0]) / int(parts[1]) if len(parts) > 0 and int(parts[1]) != 0 else float(parts[0]) return len(frames[index])/float(getValue(meta[index],'duration',1)) if len(index) < len(frames) else \ float(getValue(meta[index], 'nb_frames', 30))/float(getValue(meta[index],'duration',1))
def process_meta_from_streams(stream, errorstream): streams = [] meta = {} bit_rate = None video_stream = None try: while True: line = errorstream.readline() if line is None or len(line) == 0: break pos = line.find('bitrate:') if pos > 0: bit_rate = line[pos+9:].strip() pos = bit_rate.find(' ') if pos > 0: bit_rate = str(int(bit_rate[0:pos]) * 1024) break except: pass while True: line = stream.readline() if line is None or len(line) == 0: break if '[STREAM]' in line or '[FORMAT]' in line: while True: line = stream.readline() if '[/STREAM]' in line: index = __add_meta_to_list(streams, meta, index_id ='index') if getValue(meta,'codec_type','na') == 'video' and video_stream is None: video_stream = index meta = {} break elif '=' not in line: continue else: setting = line.split('=') if len(setting) < 2 or len(setting[1]) == 0: continue meta[setting[0]] = '='.join(setting[1:]).strip() if len(meta) > 0: index = __add_meta_to_list(streams, meta, index_id='index') if getValue(meta, 'codec_type', 'na') == 'video' and video_stream is None: video_stream = index if bit_rate is not None and video_stream is not None and \ ('bit_rate' is not streams[video_stream] or \ streams[video_stream]['bit_rate'] == 'N/A'): streams[video_stream]['bit_rate'] = bit_rate return streams
def is_vfr(meta, frames=[]): """ :param meta: :return: based on meta data for video, is the stream variable frame rate @rtype: bool """ if getValue(meta,'codec_type','na') != 'video': return False prior_check = getValue(meta,'is_vfr',None) if prior_check is not None: return prior_check nb = getValue(meta,'nb_frames','N/A').lower() avg = getValue(meta,'avg_frame_rate','N/A').lower() r = getValue(meta,'r_frame_rate','N/A').lower() if (r[0] != 'n' and r != avg) or r[0] in ['n','0'] or nb[0] in ['n','0']: # approach requires frames which is more expensive to gather but more accurate frame_durations = set([frame['pkt_duration_time'] for frame in frames[0:100]]) if len(frames) > 0 else [] if len(frame_durations) != 1 or len(frames) == 0: return True return False
def get_stream_indices_of_type(stream_data, stream_type): """ Get indexes of the streams that are of a given type :param stream_data: metadata with stream information. :param stream_type: codec_type to look for. :return: list of indexes in string form. @rtype: list of str """ indicies = [] for pos in range(len(stream_data)): if getValue(stream_data[pos],'codec_type','na') == stream_type: indicies.append(pos) return indicies
def __get_channel_data(source_data, codec_type): for data in source_data: if getValue(data,'codec_type','na') == codec_type: return data
def strip(meta,frames,media_types): return [item for item in meta if getValue(item,'codec_type','na') in media_types],\ [frames[pos] for pos in range(len(frames)) if getValue(meta[pos],'codec_type','na') in media_types] \ if len(frames) > 0 else frames
def get_duraton_from_meta(meta, media_type='video'): indices = get_stream_indices_of_type(meta, media_type) if len(indices) == 0: return None return getValue(meta[indices[0]], 'duration')
def get_audio_frame_rate_from_meta(meta): index = get_stream_indices_of_type(meta, 'audio')[0] return int(getValue(meta[index],'sample_rate',None))