def log(message, level, process_name=None): '''Conditionally write a message to stdout based on command line options and level.''' global DEBUG global DEBUG_PROTOCOL global QUIET global INFO if QUIET and level != LEVEL_ERROR: return if not DEBUG_PROTOCOL and level == LEVEL_PROTOCOL: return if not DEBUG and level == LEVEL_DEBUG: return if not INFO and level == LEVEL_INFO: return if not process_name: log_file = settings.log_file else: log_file = os.path.join(settings.DATA_DIR, 'logs', "%s.log" % process_name) ensureDir(log_file) logger = get_logger(log_file, maxbytes=2 * 1024 * 1024) # maxbytes = 2MB if level == LEVEL_ERROR: logger.error(message) elif level == LEVEL_DEBUG or level == LEVEL_PROTOCOL: logger.debug(message) else: logger.info(message)
def process_file(self, vid_file_path): # Parse input path srcFile = os.path.basename(vid_file_path) srcPath = os.path.abspath(vid_file_path) srcFolder = os.path.dirname(srcPath) # Parse an output cache path outFile = "{}.{}".format(os.path.splitext(srcFile)[0], self.settings.OUT_CONTAINER) outPath = os.path.join(self.settings.CACHE_PATH,outFile) # Create output path if not exists common.ensureDir(outPath) # Reset all info self.set_info_defaults() # Fetch file info try: self.file_in = self.file_probe(vid_file_path) except Exception as e: self._log("Exception - process_file: {}".format(e), level='exception') return False # Convert file success = False ffmpeg_args = self.generate_ffmpeg_args() if ffmpeg_args: success = self.convert_file_and_fetch_progress(srcPath,outPath,ffmpeg_args) if success: # Move file back to original folder and remove source success = self.post_process_file(outPath) if success: destPath = os.path.join(srcFolder,outFile) self._log("Moving file {} --> {}".format(outPath,destPath)) shutil.move(outPath, destPath) try: self.post_process_file(destPath) except FFMPEGHandlePostProcessError: success = False if success: # If successful move, remove source #TODO: Add env variable option to keep src if srcPath != destPath: self._log("Removing source: {}".format(srcPath)) os.remove(srcPath) else: self._log("Copy / Replace failed during post processing '{}'".format(outPath), level='warning') return False else: self._log("Encoded file failed post processing test '{}'".format(outPath), level='warning') return False else: self._log("Failed processing file '{}'".format(srcPath), level='warning') return False # If file conversion was successful, we will get here self._log("Successfully processed file '{}'".format(srcPath)) return True
def convert_single_file(self,infile,outfile,test_for_failure=False): if not os.path.exists(infile): self._log("No such file: {}".format(infile)) sys.exit(1) # Ensure the directory exists common.ensureDir(outfile) # Remove the ouput file if it already exists if os.path.exists(outfile): os.remove(outfile) # Setup ffmpeg args built_args = self.build_ffmpeg_args(test_for_failure) # Run conversion process self._log("Converting {} -> {}".format(infile, outfile)) assert self.ffmpeg.convert_file_and_fetch_progress(infile, outfile, built_args) if not test_for_failure: assert self.ffmpeg.post_process_file(outfile) elif test_for_failure: import pytest with pytest.raises(FFMPEGHandlePostProcessError): self.ffmpeg.post_process_file(outfile)