def package(self): prev_dir = os.getcwd() os.chdir(self.args.scratch_dir) # Make sure there is no 'output.mp4' in the scratch directory # MP4Box has a tendency to add tracks output_file = os.path.join(os.getcwd(), "output.mp4") if os.path.isfile(output_file): os.unlink(output_file) run_attempts = 1 while run_attempts <= self.args.mp4box_retries: cmd = [self.args.tool_paths["mp4box"], "output.mp4", # Always create new file with mp4box/GPAC # https://github.com/jbillo/xenonmkv/issues/2 "-new", "-add", self.video_path, "-fps", self.video_fps, "-par", "1=" + self.video_pixel_ar, "-add", self.audio_path, "-tmp", self.args.scratch_dir, "-itags", "name=" + self.args.name] ph = ProcessHandler(self.args, self.log) process = ph.start_output(cmd) if process != 0: # Destroy temporary file # so it does not have multiple tracks imported os.unlink(output_file) self.log.warning("An error occurred while creating " "an MP4 file with MP4Box; {0} retries left".format( self.args.mp4box_retries - run_attempts)) run_attempts += 1 # Continue retrying to create the file else: # File was created successfully; exit retry loop break if run_attempts > self.args.mp4box_retries: # Delete the temporary file so that nobody gets tempted to use it if os.path.isfile(output_file): try: os.unlink(output_file) except: # Don't really care, just as long as the file is gone. pass raise Exception("MP4Box could not create file after {0} retries; " "giving up.".format(self.args.mp4box_retries)) self.log.debug("MP4Box process complete") # When complete, change back to original directory os.chdir(prev_dir)
def encode_faac(self): # Start encoding self.log.debug("Using FAAC to encode AAC audio file") if self.args.resume_previous and os.path.isfile("audiodump.aac"): self.log.debug("audiodump.aac already exists in scratch " "directory; cancelling encode") return True cmd = [self.args.tool_paths["faac"], "-q", str(self.args.faac_quality), self.file_path] ph = ProcessHandler(self.args, self.log) return ph.start_output(cmd)
def encode_faac(self): # Start encoding self.log.debug("Using FAAC to encode AAC audio file") if self.args.resume_previous and os.path.isfile("audiodump.aac"): self.log.debug("audiodump.aac already exists in scratch " "directory; cancelling encode") return True cmd = [ self.args.tool_paths["faac"], "-q", str(self.args.faac_quality), self.file_path ] ph = ProcessHandler(self.args, self.log) return ph.start_output(cmd)
def decode_mplayer(self): self.log.debug("Starting decoding file to WAV with mplayer") # Check for existing audiodump.wav (already changed to temp directory) audiodump_file = os.path.join(os.getcwd(), "audiodump.wav") if os.path.isfile(audiodump_file): if self.args.resume_previous: self.log.debug("audiodump.wav already exists in scratch " "directory; cancelling decode") return True self.log.debug("Deleting temporary mplayer output file {0}".format( audiodump_file)) os.unlink(audiodump_file) cmd = [self.args.tool_paths["mplayer"], self.file_path, "-benchmark", "-vc", "null", "-vo", "null", "-channels", "2", "-noautosub", "-ao", "pcm:fast"] ph = ProcessHandler(self.args, self.log) ph.start_output(cmd) self.log.debug("mplayer decoding to PCM WAV file complete")
def decode_mplayer(self): self.log.debug("Starting decoding file to WAV with mplayer") # Check for existing audiodump.wav (already changed to temp directory) audiodump_file = os.path.join(os.getcwd(), "audiodump.wav") if os.path.isfile(audiodump_file): if self.args.resume_previous: self.log.debug("audiodump.wav already exists in scratch " "directory; cancelling decode") return True self.log.debug("Deleting temporary mplayer output file {0}".format( audiodump_file)) os.unlink(audiodump_file) cmd = [ self.args.tool_paths["mplayer"], self.file_path, "-benchmark", "-vc", "null", "-vo", "null", "-channels", "2", "-noautosub", "-ao", "pcm:fast" ] ph = ProcessHandler(self.args, self.log) ph.start_output(cmd) self.log.debug("mplayer decoding to PCM WAV file complete")
def extract_mkv(self): self.log.debug("Executing mkvextract on '{0}'".format(self.get_path())) prev_dir = os.getcwd() if self.args.scratch_dir != ".": self.log.debug("Using {0} as scratch directory for " "MKV extraction".format(self.args.scratch_dir)) os.chdir(self.args.scratch_dir) mkvtoolnix_video_id = self.tracks[self.video_track_id].mkvtoolnix_id mkvtoolnix_audio_id = self.tracks[self.audio_track_id].mkvtoolnix_id self.log.debug( "Using video track from MKV file with ID {0} " "(mkvtoolnix ID {1})".format(self.video_track_id, mkvtoolnix_video_id) ) self.log.debug( "Using audio track from MKV file with ID {0} " "(mkvtoolnix ID {1})".format(self.audio_track_id, mkvtoolnix_audio_id) ) try: temp_video_file = "temp_video" + self.tracks[self.video_track_id].get_filename_extension() temp_audio_file = "temp_audio" + self.tracks[self.audio_track_id].get_filename_extension() except UnsupportedCodecError: # Send back to main application raise if self.args.resume_previous and os.path.isfile(temp_video_file) and os.path.isfile(temp_audio_file): self.log.debug("Temporary video and audio files already exist; " "cancelling extract") temp_video_file = os.path.join(os.getcwd(), temp_video_file) temp_audio_file = os.path.join(os.getcwd(), temp_audio_file) os.chdir(prev_dir) return (temp_video_file, temp_audio_file) # Remove any existing files with the same names if os.path.isfile(temp_video_file): self.log.debug("Deleting temporary video file {0}".format(os.path.join(os.getcwd(), temp_video_file))) os.unlink(temp_video_file) if os.path.isfile(temp_audio_file): self.log.debug("Deleting temporary audio file {0}".format(os.path.join(os.getcwd(), temp_audio_file))) os.unlink(temp_audio_file) video_output = str(mkvtoolnix_video_id) + ":" + temp_video_file audio_output = str(mkvtoolnix_audio_id) + ":" + temp_audio_file cmd = [self.args.tool_paths["mkvextract"], "tracks", self.get_path(), video_output, audio_output] ph = ProcessHandler(self.args, self.log) process = ph.start_output(cmd) if process != 0: raise Exception( "An error occurred while extracting tracks from {0}" " - please make sure this file exists and is readable".format(self.get_path()) ) temp_video_file = os.path.join(os.getcwd(), temp_video_file) temp_audio_file = os.path.join(os.getcwd(), temp_audio_file) os.chdir(prev_dir) self.log.debug("mkvextract finished; attempting to parse output") if not temp_video_file or not temp_audio_file: raise Exception("Audio or video file missing from " "mkvextract output") return (temp_video_file, temp_audio_file)
def extract_mkv(self): self.log.debug("Executing mkvextract on '{0}'".format(self.get_path())) prev_dir = os.getcwd() if self.args.scratch_dir != ".": self.log.debug("Using {0} as scratch directory for " "MKV extraction".format(self.args.scratch_dir)) os.chdir(self.args.scratch_dir) mkvtoolnix_video_id = self.tracks[self.video_track_id].mkvtoolnix_id mkvtoolnix_audio_id = self.tracks[self.audio_track_id].mkvtoolnix_id self.log.debug("Using video track from MKV file with ID {0} " "(mkvtoolnix ID {1})".format(self.video_track_id, mkvtoolnix_video_id)) self.log.debug("Using audio track from MKV file with ID {0} " "(mkvtoolnix ID {1})".format(self.audio_track_id, mkvtoolnix_audio_id)) try: temp_video_file = ( "temp_video" + self.tracks[self.video_track_id].get_filename_extension()) temp_audio_file = ( "temp_audio" + self.tracks[self.audio_track_id].get_filename_extension()) except UnsupportedCodecError: # Send back to main application raise if (self.args.resume_previous and os.path.isfile(temp_video_file) and os.path.isfile(temp_audio_file)): self.log.debug("Temporary video and audio files already exist; " "cancelling extract") temp_video_file = os.path.join(os.getcwd(), temp_video_file) temp_audio_file = os.path.join(os.getcwd(), temp_audio_file) os.chdir(prev_dir) return (temp_video_file, temp_audio_file) # Remove any existing files with the same names if os.path.isfile(temp_video_file): self.log.debug("Deleting temporary video file {0}".format( os.path.join(os.getcwd(), temp_video_file))) os.unlink(temp_video_file) if os.path.isfile(temp_audio_file): self.log.debug("Deleting temporary audio file {0}".format( os.path.join(os.getcwd(), temp_audio_file))) os.unlink(temp_audio_file) video_output = str(mkvtoolnix_video_id) + ":" + temp_video_file audio_output = str(mkvtoolnix_audio_id) + ":" + temp_audio_file cmd = [ self.args.tool_paths["mkvextract"], "tracks", self.get_path(), video_output, audio_output ] ph = ProcessHandler(self.args, self.log) process = ph.start_output(cmd) if process != 0: raise Exception( "An error occurred while extracting tracks from {0}" " - please make sure this file exists and is readable".format( self.get_path())) temp_video_file = os.path.join(os.getcwd(), temp_video_file) temp_audio_file = os.path.join(os.getcwd(), temp_audio_file) os.chdir(prev_dir) self.log.debug("mkvextract finished; attempting to parse output") if not temp_video_file or not temp_audio_file: raise Exception("Audio or video file missing from " "mkvextract output") return (temp_video_file, temp_audio_file)