def _validate(self): if self.vid_fps is None: raise errors.MMFError( "Failed to parse video file '%s' - no FPS found" % self.input_file_name) if self.vid_width is None: raise errors.MMFError( "Failed to parse video file '%s' - no video width found" % self.input_file_name) if self.vid_height is None: raise errors.MMFError( "Failed to parse video file '%s' - no video height found" % self.input_file_name) if self.vid_interlaced is None: raise errors.MMFError( "Failed to parse video file '%s' - no scan info found" % self.input_file_name) if self.vid_codec is None: raise errors.MMFError( "Failed to parse video file '%s' - no video codec found" % self.input_file_name) if self.audio_codec_id is None: raise errors.MMFError( "Failed to parse video file '%s' - no audio codec found" % self.input_file_name) if self.audio_channels is None: raise errors.MMFError( "Failed to parse video file '%s' - no audio channels found" % self.input_file_name) if self.audio_samplerate is None: raise errors.MMFError( "Failed to parse video file '%s' - no audio sample rate found" % self.input_file_name)
def _bit_rate_convert(bit_rate_tokenized): if bit_rate_tokenized[1] == "Kbps": return _float_to_str_trunc(float(bit_rate_tokenized[0])) elif bit_rate_tokenized[1] == "Mbps": return _float_to_str_trunc(float(bit_rate_tokenized[0]) * 1024) else: raise errors.MMFError("Unknown bit rate string")
def _validate(self): """ Validate that required fields are present""" if self.video_max_width is None: raise errors.MMFError( "Incomplete target file %s - no video width specified" % self.target_file) if self.video_max_height is None: raise errors.MMFError( "Incomplete target file %s - no video height specified" % self.target_file) if self.video_max_bitrate is None: raise errors.MMFError( "Incomplete target file %s - no video bitrate specified" % self.target_file) if self.codec_h264_profile is None: raise errors.MMFError( "Incomplete target file %s - no video codec specified" % self.target_file) if self.audio_max_bitrate is None: raise errors.MMFError( "Incomplete target file %s - no audio bitrate specified" % self.target_file) if self.audio_sample_rate is None: raise errors.MMFError( "Incomplete target file %s - no audio sample rate specified" % self.target_file) if self.audio_channel_count is None: raise errors.MMFError( "Incomplete target file %s - no audio channel count specified" % self.target_file)
def __init__(self, target_string): """Loads target configuration from file for given target string""" # Initialize all values - file could be missing fields self.target_name = None self.video_max_width = None self.video_max_height = None self.video_max_bitrate = None self.video_interlaced = False self.codec_h264_profile = None self.codec_h264_same = False self.codec_h264_level = None self.audio_max_bitrate = None self.audio_max_samplerate = None self.audio_channel_count = None target_file = find_target_file(target_string) if target_file is not None: self.target_file = target_file else: raise errors.MMFError("Target file for '%s' not found." % target_string) cur_file = open(self.target_file, 'r') for line in cur_file: if line.startswith("TARGET_NAME_STRING"): self.target_name = _get_string_field(line) elif line.startswith("VIDEO_MAX_WIDTH"): self.video_max_width = _get_int_field(line) elif line.startswith("VIDEO_MAX_HEIGHT"): self.video_max_height = _get_int_field(line) elif line.startswith("VIDEO_MAX_BITRATE"): self.video_max_bitrate = _get_int_field(line) elif line.startswith("VIDEO_SCAN"): scan_str = _get_string_field(line) if scan_str.lower() == "interlaced": self.video_interlaced = True elif line.startswith("CODEC_H264_PROFILE"): self.codec_h264_profile = _get_string_field(line) elif line.startswith("CODEC_H264_LEVEL"): self.codec_h264_level = _get_string_field(line) elif line.startswith("AUDIO_MAX_BITRATE"): self.audio_max_bitrate = _get_int_field(line) elif line.startswith("AUDIO_SAMPLE_RATE"): self.audio_sample_rate = _get_int_field(line) elif line.startswith("AUDIO_CHANNEL_COUNT"): self.audio_channel_count = _get_int_field(line) try: self._validate() except errors.MMFError: raise if (self.codec_h264_level.lower() == "same" or self.codec_h264_profile.lower() == "same"): self.codec_h264_same = True
def write_all(self): """ Read all the files in the input list and write them to the output set with set_output() """ if self._output_fd is None: raise errors.MMFError( "No output descriptor set for %s" % self) buf = self._read() while buf != "": try: self._output_fd.write(buf) except IOError: # Output file descriptor closed, reader process is gone... raise buf = self._read() self._output_fd.close()
def _validate_list(self, file_list): """Validate the input file list to make sure all files are similar""" print "Validating multiple file compatibility..." for cur_file in file_list: if self.parser is None: try: self.parser = vidparse.VidParser(cur_file) except errors.MMFError: raise else: try: cur_parser = vidparse.VidParser(cur_file) except errors.MMFError: raise if cur_parser != self.parser: raise errors.MMFError( "Files '%s' and '%s' are incompatible:\n%s" % (file_list[0], cur_file, cur_parser.diff_str)) # TODO: Validate that the file containers are concat-able print "All files compatible!"
def __init__(self, input_file_name): self.vid_stream_id = None self.vid_format_profile = None self.vid_codec = None self.vid_interlaced = None self.vid_width = None self.vid_height = None self.vid_bitrate = None self.vid_fps = None self._vid_codec_id = None self._vid_format = None self.audio_stream_id = None self.audio_format = None self.audio_codec_id = None self.audio_channels = None self.audio_bitrate = None self.audio_samplerate = None self.diff_str = None if not os.path.isfile(input_file_name): raise errors.MMFError("Input file '%s' not found." % input_file_name) self.input_file_name = input_file_name current_section = VidParser.INVALID_SECTION mp = subprocess.Popen(['mediainfo', "".join(input_file_name)], stdout=subprocess.PIPE) mp_output = mp.communicate() mp_tokenized = mp_output[0].split("\n") for mp_line in mp_tokenized: if mp_line == "Video": current_section = VidParser.VIDEO_SECTION elif mp_line == "Audio" or mp_line == "Audio #1": current_section = VidParser.AUDIO_SECTION elif mp_line == "Text": current_section = VidParser.TEXT_SECTION elif current_section == VidParser.VIDEO_SECTION: if mp_line.startswith("Format "): self._vid_format = _get_field_value(mp_line) elif mp_line.startswith("Format profile "): self.vid_format_profile = _get_field_value(mp_line) elif mp_line.startswith("Codec ID "): self._vid_codec_id = _get_field_value(mp_line) elif mp_line.startswith("ID "): self.vid_stream_id = int( _tokenize_field(_get_field_value(mp_line))[0]) elif mp_line.startswith("Scan type "): vid_scan = _get_field_value(mp_line) if (vid_scan == "Interlaced") or (vid_scan == "MBAFF"): self.vid_interlaced = True elif (vid_scan == "Progressive"): self.vid_interlaced = False elif mp_line.startswith("Width "): vid_width_str = _get_field_value(mp_line) self.vid_width = int( _field_collapse_thousands(vid_width_str)[0]) elif mp_line.startswith("Height "): vid_height_str = _get_field_value(mp_line) self.vid_height = int( _field_collapse_thousands(vid_height_str)[0]) elif (mp_line.startswith("Bit rate ") or mp_line.startswith("Nominal bit rate")): vid_bit_rate_str = _get_field_value(mp_line) self.vid_bitrate = int( _bit_rate_convert( _field_collapse_thousands(vid_bit_rate_str))) elif mp_line.startswith("Frame rate "): vid_fps_str = _get_field_value(mp_line) self.vid_fps = float(_tokenize_field(vid_fps_str)[0]) elif current_section == VidParser.AUDIO_SECTION: if mp_line.startswith("Format "): self.audio_format = _get_field_value(mp_line) elif mp_line.startswith("Codec ID "): self.audio_codec_id = _get_field_value(mp_line) elif mp_line.startswith("ID"): self.audio_stream_id = int( _tokenize_field(_get_field_value(mp_line))[0]) elif mp_line.startswith("Bit rate "): audio_bit_rate_str = _get_field_value(mp_line) self.audio_bitrate = int( _bit_rate_convert( _field_collapse_thousands(audio_bit_rate_str))) elif mp_line.startswith("Sampling rate "): audio_sample_rate_str = _get_field_value(mp_line) self.audio_samplerate = int( _float_to_str_trunc( float(_tokenize_field(audio_sample_rate_str)[0]))) elif mp_line.startswith("Channel(s)"): audio_channel_str = _get_field_value(mp_line) self.audio_channels = int( _tokenize_field(audio_channel_str)[0]) if (self._vid_codec_id == "avc1" or self._vid_codec_id == "V_MPEG4/ISO/AVC" or self._vid_format == "AVC" or self._vid_format == "MPEG-4 Visual"): self.vid_codec = VIDEO_CODEC_H264 elif self._vid_codec_id == "WMV3": self.vid_codec = VIDEO_CODEC_WMV3 elif self._vid_codec_id == "DX40" or self._vid_codec_id == "DIVX": self.vid_codec = VIDEO_CODEC_DIVX elif self._vid_format == "MPEG Video": self.vid_codec = VIDEO_CODEC_MPEG12 elif self._vid_format == "VC-1": self.vid_codec = VIDEO_CODEC_VC1 elif self._vid_codec_id == "XVID": self.vid_codec = VIDEO_CODEC_XVID try: self._validate() except errors.MMFError: raise