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(), self.args.name + ".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"], self.args.name + ".mp4", # Always create new file with mp4box/GPAC "-add", self.video_path, "-fps", self.video_fps, "-par", "1=" + self.video_pixel_ar, "-add", self.audio_path, "-tmp", self.args.scratch_dir, "-new", "-itags", "comment=" + str(self.args.tag_comment) + ":" + "name=" + str(self.args.tag_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 if os.path.isfile(output_file): os.unlink(output_file) self.log.warning("An error occurred while creating " "an MP4 file with MP4Box") 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: if os.path.isfile(output_file): 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_aac(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 = [ "ffmpeg", "-i", os.path.join(self.file_path, "temp_audio.ac3"), '-acodec', 'aac', '-strict', 'experimental', '-b:a', '192k', '-ar', '48000', '-async', '48000', '-ac', '2', '-clev', '1.414', '-slev', '.5', os.path.join(self.file_path, 'audiodump.aac') ] ph = ProcessHandler(self.args, self.log) return ph.start_output(cmd)
def encode_aac(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 = ["ffmpeg", "-i", os.path.join(self.file_path, "temp_audio.ac3"), '-acodec', 'aac', '-strict', 'experimental', '-b:a', '192k', '-ar', '48000', '-async', '48000', '-ac', '2', '-clev', '1.414', '-slev', '.5', os.path.join(self.file_path, 'audiodump.aac')] ph = ProcessHandler(self.args, self.log) return ph.start_output(cmd)
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(), self.args.name + ".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"], self.args.name + ".mp4", # Always create new file with mp4box/GPAC "-add", self.video_path, "-fps", self.video_fps, "-par", "1=" + self.video_pixel_ar, "-add", self.audio_path, "-tmp", self.args.scratch_dir, "-new", "-itags", "comment=" + str(self.args.tag_comment) + ":" + "name=" + str(self.args.tag_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 if os.path.isfile(output_file): os.unlink(output_file) self.log.warning("An error occurred while creating " "an MP4 file with MP4Box") 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: if os.path.isfile(output_file): 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 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