def cleanup_temp_files(): global args, log if not args.preserve_temp_files: t = MKVTrack(log) f_utils = FileUtils(log, args) f_utils.delete_temp_files(args.scratch_dir, t.get_possible_extensions())
def parse_mkvinfo(self, result): track_detect_string = "| + A track" if not track_detect_string in result: raise Exception("mkvinfo: output did not contain any tracks") track_info = result self.duration = self.parse_audio_duration(track_info) # Multiple track support: # Extract mediainfo profile for all tracks in file, # then cross-reference them with the output from mkvinfo. # This prevents running mediainfo multiple times. mediainfo_video_output = self.get_mediainfo("video") mediainfo_audio_output = self.get_mediainfo("audio") # For ease of use, throw these values into a dictionary # with the key being the track ID. mediainfo = {} for mediainfo_track in mediainfo_video_output: mediainfo[int(mediainfo_track[0])] = mediainfo_track[1:] for mediainfo_track in mediainfo_audio_output: mediainfo[int(mediainfo_track[0])] = mediainfo_track[1:] # Create a new parser that can be used for all tracks info_parser = MKVInfoParser(self.log) has_audio = has_video = False while track_detect_string in track_info: track = MKVTrack(self.log) if track_detect_string in track_info: track_info = track_info[track_info.index(track_detect_string) + len(track_detect_string) :] else: break # Get track type and number out of this block track_type = info_parser.parse_track_type(track_info) track_number, track_mkvtoolnix_id = info_parser.parse_track_number(track_info) self.log.debug( "Track {0} will use ID {1} when taking actions " "with the mkvtoolnix suite".format(track_number, track_mkvtoolnix_id) ) # Set individual track properties for the object by track ID if track_type in ("video", "audio"): mediainfo_track = mediainfo[track_number] if track_type == "video": has_video = True track = VideoTrack(self.log) track.number = track_number track.default = info_parser.parse_track_is_default(track_info) track.height = int(mediainfo_track[0]) track.width = int(mediainfo_track[1]) try: # Possible condition: no reference frames detected # If so, just set to zero and log a debug message track.reference_frames = int(mediainfo_track[2]) except ValueError: track.reference_frames = 0 self.log.debug( "Reference frame value '{0}' in track {1} " "could not be parsed; assuming 0 reference " "frames".format(mediainfo_track[2], track.number) ) track.language = mediainfo_track[3].lower() if mediainfo_track[4]: track.frame_rate = float(mediainfo_track[4]) elif mediainfo_track[7]: self.log.debug("Using original track frame rate as " "container frame rate was not set") track.frame_rate = float(mediainfo_track[7]) else: raise ValueError("Could not read FPS information " "from mediainfo output") track.codec_id = mediainfo_track[5] track.display_ar = self.parse_display_aspect_ratio(mediainfo_track[6]) track.pixel_ar = self.calc_pixel_aspect_ratio(track) self.log.debug( "Video track {0} has dimensions {1}x{2} with " "{3} reference frames".format(track.number, track.width, track.height, track.reference_frames) ) self.log.debug( "Video track {0} has {1:f} FPS and codec {2}".format(track.number, track.frame_rate, track.codec_id) ) self.log.debug("Video track {0} has display aspect ratio {1:f}".format(track.number, track.display_ar)) if self.reference_frames_exceeded(track): self.log.warning( "Video track {0} contains too many " "reference frames to play properly on low-powered " "devices. See {1} for details".format(track.number, xenonmkv.text.REFERENCE_FRAMES_INFO) ) if not self.args.ignore_reference_frames: raise Exception("Video track {0} has too many " "reference frames".format(track.number)) else: self.log.debug( "Video track {0} has a reasonable number " "of reference frames, and should be compatible with " "low-powered devices".format(track.number) ) elif track_type == "audio": has_audio = True track = AudioTrack(self.log) track.number = track_number track.default = info_parser.parse_track_is_default(track_info) track.codec_id = mediainfo_track[0] track.language = mediainfo_track[1].lower() track.channels = int(mediainfo_track[2]) # Indicate if the audio track needs a recode. # By default, it does. # Check that the audio type is AAC, and if the number of # channels in the file is less than or equal to what was # specified on the command line, no recode is necessary. if track.codec_id == "A_AAC": # Check on the number of channels in the file # versus the argument passed. if track.channels <= self.args.channels: # Reasonable message to log at info level self.log.info( "Audio track {0} will not need to be " "re-encoded ({1} channels specified, {2} channels " "in file)".format(track.number, self.args.channels, track.channels) ) track.needs_recode = False self.log.debug( "Audio track {0} has codec {1} and language {2}".format( track.number, track.codec_id, track.language ) ) self.log.debug("Audio track {0} has {1} channel(s)".format(track.number, track.channels)) else: # Unrecognized track type. Don't completely abort processing, # but do log it. # Do not proceed to add this to the global tracks list. self.log.debug("Unrecognized track type '{0}' in {1}; skipping".format(track_type, track_number)) continue # Add general properties to track track.mkvtoolnix_id = track_mkvtoolnix_id self.log.debug("All properties set for {0} track {1}".format(track_type, track.number)) track.track_type = track_type self.tracks[track.number] = track # All tracks detected here self.log.debug("All tracks detected from mkvinfo output; " "total number is {0}".format(len(self.tracks))) # Make sure that there is at least one audio and one video track # and throw an exception if not if not has_video: raise Exception("No video track found in MKV file {0}".format(self.path)) elif not has_audio: raise Exception("No audio track found in MKV file {0}".format(self.path))
def parse_mkvinfo(self, result): track_detect_string = "| + A track" if not track_detect_string in result: raise Exception("mkvinfo: output did not contain any tracks") track_info = result self.duration = self.parse_audio_duration(track_info) # Multiple track support: # Extract mediainfo profile for all tracks in file, # then cross-reference them with the output from mkvinfo. # This prevents running mediainfo multiple times. mediainfo_video_output = self.get_mediainfo("video") mediainfo_audio_output = self.get_mediainfo("audio") # For ease of use, throw these values into a dictionary # with the key being the track ID. mediainfo = {} for mediainfo_track in mediainfo_video_output: mediainfo[int(mediainfo_track[0])] = mediainfo_track[1:] for mediainfo_track in mediainfo_audio_output: mediainfo[int(mediainfo_track[0])] = mediainfo_track[1:] # Create a new parser that can be used for all tracks info_parser = MKVInfoParser(self.log) has_audio = has_video = False while track_detect_string in track_info: track = MKVTrack(self.log) if track_detect_string in track_info: track_info = track_info[track_info.index(track_detect_string) + len(track_detect_string):] else: break # Get track type and number out of this block track_type = info_parser.parse_track_type(track_info) track_number, track_mkvtoolnix_id = ( info_parser.parse_track_number(track_info)) self.log.debug("Track {0} will use ID {1} when taking actions " "with the mkvtoolnix suite".format( track_number, track_mkvtoolnix_id)) # Set individual track properties for the object by track ID if track_type in ("video", "audio"): mediainfo_track = mediainfo[track_number] if track_type == "video": has_video = True track = VideoTrack(self.log) track.number = track_number track.default = info_parser.parse_track_is_default(track_info) track.height = int(mediainfo_track[0]) track.width = int(mediainfo_track[1]) try: # Possible condition: no reference frames detected # If so, just set to zero and log a debug message track.reference_frames = int(mediainfo_track[2]) except ValueError: track.reference_frames = 0 self.log.debug("Reference frame value '{0}' in track {1} " "could not be parsed; assuming 0 reference " "frames".format(mediainfo_track[2], track.number)) track.language = mediainfo_track[3].lower() if mediainfo_track[4]: track.frame_rate = float(mediainfo_track[4]) elif mediainfo_track[7]: self.log.debug("Using original track frame rate as " "container frame rate was not set") track.frame_rate = float(mediainfo_track[7]) else: raise ValueError("Could not read FPS information " "from mediainfo output") track.codec_id = mediainfo_track[5] track.display_ar = self.parse_display_aspect_ratio( mediainfo_track[6]) track.pixel_ar = self.calc_pixel_aspect_ratio(track) self.log.debug("Video track {0} has dimensions {1}x{2} with " "{3} reference frames".format( track.number, track.width, track.height, track.reference_frames)) self.log.debug( "Video track {0} has {1:f} FPS and codec {2}".format( track.number, track.frame_rate, track.codec_id)) self.log.debug( "Video track {0} has display aspect ratio {1:f}".format( track.number, track.display_ar)) if self.reference_frames_exceeded(track): self.log.warning( "Video track {0} contains too many " "reference frames to play properly on low-powered " "devices".format(track.number)) if not self.args.ignore_reference_frames: raise Exception("Video track {0} has too many " "reference frames".format( track.number)) else: self.log.debug( "Video track {0} has a reasonable number " "of reference frames, and should be compatible with " "low-powered devices".format(track.number)) elif track_type == "audio": has_audio = True track = AudioTrack(self.log) track.number = track_number track.default = info_parser.parse_track_is_default(track_info) track.codec_id = mediainfo_track[0] track.language = mediainfo_track[1].lower() track.channels = int(mediainfo_track[2]) # Indicate if the audio track needs a recode. # By default, it does. # Check that the audio type is AAC, and if the number of # channels in the file is less than or equal to what was # specified on the command line, no recode is necessary. if track.codec_id == "A_AAC": # Check on the number of channels in the file # versus the argument passed. if track.channels <= self.args.channels: # Reasonable message to log at info level self.log.info( "Audio track {0} will not need to be " "re-encoded ({1} channels specified, {2} channels " "in file)".format(track.number, self.args.channels, track.channels)) track.needs_recode = False self.log.debug( "Audio track {0} has codec {1} and language {2}".format( track.number, track.codec_id, track.language)) self.log.debug("Audio track {0} has {1} channel(s)".format( track.number, track.channels)) else: # Unrecognized track type. Don't completely abort processing, # but do log it. # Do not proceed to add this to the global tracks list. self.log.debug( "Unrecognized track type '{0}' in {1}; skipping".format( track_type, track_number)) continue # Add general properties to track track.mkvtoolnix_id = track_mkvtoolnix_id self.log.debug("All properties set for {0} track {1}".format( track_type, track.number)) track.track_type = track_type self.tracks[track.number] = track # All tracks detected here self.log.debug("All tracks detected from mkvinfo output; " "total number is {0}".format(len(self.tracks))) # Make sure that there is at least one audio and one video track # and throw an exception if not if not has_video: raise Exception("No video track found in MKV file {0}".format( self.path)) elif not has_audio: raise Exception("No audio track found in MKV file {0}".format( self.path))