def handbrake_mkv(srcpath, basepath, logfile, job): """process all mkv files in a directory.\n srcpath = Path to source for HB (dvd or files)\n basepath = Path where HB will save trancoded files\n logfile = Logfile for HB to redirect output to\n job = Disc object\n Returns nothing """ # Added to limit number of transcodes job.status = "waiting_transcode" db.session.commit() utils.sleep_check_process("HandBrakeCLI", int(cfg["MAX_CONCURRENT_TRANSCODES"])) job.status = "transcoding" db.session.commit() if job.disctype == "dvd": hb_args = cfg["HB_ARGS_DVD"] hb_preset = cfg["HB_PRESET_DVD"] elif job.disctype == "bluray": hb_args = cfg["HB_ARGS_BD"] hb_preset = cfg["HB_PRESET_BD"] # This will fail if the directory raw gets deleted # sorted makes sure we transcode each title in order for f in sorted(srcpath, key=lambda x: int(x.replace('title_t', ''))): srcpathname = os.path.join(srcpath, f) destfile = os.path.splitext(f)[0] filename = os.path.join(basepath, destfile + "." + cfg["DEST_EXT"]) filepathname = os.path.join(basepath, filename) logging.info(f"Transcoding file {shlex.quote(f)} to {shlex.quote(filepathname)}") cmd = 'nice {0} -i {1} -o {2} --preset "{3}" {4}>> {5} 2>&1'.format( cfg["HANDBRAKE_CLI"], shlex.quote(srcpathname), shlex.quote(filepathname), hb_preset, hb_args, logfile ) logging.debug(f"Sending command: {cmd}") try: hb = subprocess.check_output( cmd, shell=True ).decode("utf-8") logging.debug(f"Handbrake exit code: {hb}") except subprocess.CalledProcessError as hb_error: err = f"Handbrake encoding of file {shlex.quote(f)} failed with code: {hb_error.returncode}" \ f"({hb_error.output})" logging.error(err) # job.errors.append(f) logging.info(PROCESS_COMPLETE) logging.debug("\n\r" + job.pretty_table())
def handbrake_mkv(srcpath, basepath, logfile, job): """process all mkv files in a directory.\n srcpath = Path to source for HB (dvd or files)\n basepath = Path where HB will save trancoded files\n logfile = Logfile for HB to redirect output to\n job = Disc object\n Returns nothing """ # Added to limit number of transcodes job.status = "waiting_transcode" db.session.commit() utils.sleep_check_process("HandBrakeCLI", int(job.config.MAX_CONCURRENT_TRANSCODES)) job.status = "transcoding" db.session.commit() if job.disctype == "dvd": hb_args = job.config.HB_ARGS_DVD hb_preset = job.config.HB_PRESET_DVD elif job.disctype == "bluray": hb_args = job.config.HB_ARGS_BD hb_preset = job.config.HB_PRESET_BD # This will fail if the directory raw gets deleted for f in os.listdir(srcpath): srcpathname = os.path.join(srcpath, f) destfile = os.path.splitext(f)[0] filename = os.path.join(basepath, destfile + "." + job.config.DEST_EXT) filepathname = os.path.join(basepath, filename) logging.info("Transcoding file " + shlex.quote(f) + " to " + shlex.quote(filepathname)) cmd = 'nice {0} -i {1} -o {2} --preset "{3}" {4}>> {5} 2>&1'.format( job.config.HANDBRAKE_CLI, shlex.quote(srcpathname), shlex.quote(filepathname), hb_preset, hb_args, logfile) logging.debug("Sending command: %s", cmd) try: hb = subprocess.check_output(cmd, shell=True).decode("utf-8") logging.debug("Handbrake exit code: " + hb) except subprocess.CalledProcessError as hb_error: err = "Handbrake encoding of file " + shlex.quote( f) + " failed with code: " + str( hb_error.returncode) + "(" + str(hb_error.output) + ")" logging.error(err) # job.errors.append(f) logging.info("Handbrake processing complete") logging.debug("\n\r" + job.pretty_table()) return
def handbrake_all(srcpath, basepath, logfile, job): """Process all titles on the dvd\n srcpath = Path to source for HB (dvd or files)\n basepath = Path where HB will save trancoded files\n logfile = Logfile for HB to redirect output to\n job = Disc object\n Returns nothing """ # Wait until there is a spot to transcode job.status = "waiting_transcode" db.session.commit() utils.sleep_check_process("HandBrakeCLI", int(cfg["MAX_CONCURRENT_TRANSCODES"])) job.status = "transcoding" db.session.commit() logging.info("Starting BluRay/DVD transcoding - All titles") if job.disctype == "dvd": hb_args = cfg["HB_ARGS_DVD"] hb_preset = cfg["HB_PRESET_DVD"] elif job.disctype == "bluray": hb_args = cfg["HB_ARGS_BD"] hb_preset = cfg["HB_PRESET_BD"] get_track_info(srcpath, job) logging.debug(f"Total number of tracks is {job.no_of_titles}") for track in job.tracks: if track.length < int(cfg["MINLENGTH"]): # too short logging.info(f"Track #{track.track_number} of {job.no_of_titles}. " f"Length ({track.length}) is less than minimum length ({cfg['MINLENGTH']}). Skipping") elif track.length > int(cfg["MAXLENGTH"]): # too long logging.info(f"Track #{track.track_number} of {job.no_of_titles}. " f"Length ({track.length}) is greater than maximum length ({cfg['MAXLENGTH']}). Skipping") else: # just right logging.info(f"Processing track #{track.track_number} of {job.no_of_titles}. " f"Length is {track.length} seconds.") filename = "title_" + str.zfill(str(track.track_number), 2) + "." + cfg["DEST_EXT"] filepathname = os.path.join(basepath, filename) logging.info(f"Transcoding title {track.track_number} to {shlex.quote(filepathname)}") track.filename = track.orig_filename = filename db.session.commit() cmd = 'nice {0} -i {1} -o {2} --preset "{3}" -t {4} {5}>> {6} 2>&1'.format( cfg["HANDBRAKE_CLI"], shlex.quote(srcpath), shlex.quote(filepathname), hb_preset, str(track.track_number), hb_args, logfile ) logging.debug(f"Sending command: {cmd}") try: hb = subprocess.check_output( cmd, shell=True ).decode("utf-8") logging.debug(f"Handbrake exit code: {hb}") track.status = "success" except subprocess.CalledProcessError as hb_error: err = f"Handbrake encoding of title {track.track_number} failed with code: {hb_error.returncode}" \ f"({hb_error.output})" logging.error(err) track.status = "fail" track.error = err track.ripped = True db.session.commit() logging.info(PROCESS_COMPLETE) logging.debug("\n\r" + job.pretty_table())
def handbrake_mainfeature(srcpath, basepath, logfile, job): """process dvd with mainfeature enabled.\n srcpath = Path to source for HB (dvd or files)\n basepath = Path where HB will save trancoded files\n logfile = Logfile for HB to redirect output to\n job = Job object\n Returns nothing """ logging.info("Starting DVD Movie Mainfeature processing") logging.debug("Handbrake starting: ") logging.debug("\n\r" + job.pretty_table()) utils.database_updater({'status': "waiting_transcode"}, job) # TODO: send a notification that jobs are waiting ? utils.sleep_check_process("HandBrakeCLI", int(cfg["MAX_CONCURRENT_TRANSCODES"])) logging.debug("Setting job status to 'transcoding'") utils.database_updater({'status': "transcoding"}, job) filename = os.path.join(basepath, job.title + "." + cfg["DEST_EXT"]) filepathname = os.path.join(basepath, filename) logging.info(f"Ripping title Mainfeature to {shlex.quote(filepathname)}") get_track_info(srcpath, job) track = job.tracks.filter_by(main_feature=True).first() if track is None: msg = "No main feature found by Handbrake. Turn MAINFEATURE to false in arm.yml and try again." logging.error(msg) raise RuntimeError(msg) track.filename = track.orig_filename = filename db.session.commit() if job.disctype == "dvd": hb_args = cfg["HB_ARGS_DVD"] hb_preset = cfg["HB_PRESET_DVD"] elif job.disctype == "bluray": hb_args = cfg["HB_ARGS_BD"] hb_preset = cfg["HB_PRESET_BD"] cmd = 'nice {0} -i {1} -o {2} --main-feature --preset "{3}" {4} >> {5} 2>&1'.format( cfg["HANDBRAKE_CLI"], shlex.quote(srcpath), shlex.quote(filepathname), hb_preset, hb_args, logfile ) logging.debug(f"Sending command: {cmd}") try: subprocess.check_output(cmd, shell=True).decode("utf-8") logging.info("Handbrake call successful") track.status = "success" except subprocess.CalledProcessError as hb_error: err = f"Call to handbrake failed with code: {hb_error.returncode}({hb_error.output})" logging.error(err) track.status = "fail" track.error = err job.status = "fail" db.session.commit() sys.exit(err) logging.info(PROCESS_COMPLETE) logging.debug("\n\r" + job.pretty_table()) track.ripped = True db.session.commit()
def handbrake_all(srcpath, basepath, logfile, job): """Process all titles on the dvd\n srcpath = Path to source for HB (dvd or files)\n basepath = Path where HB will save trancoded files\n logfile = Logfile for HB to redirect output to\n job = Disc object\n Returns nothing """ # Wait until there is a spot to transcode job.status = "waiting_transcode" db.session.commit() utils.sleep_check_process("HandBrakeCLI", int(job.config.MAX_CONCURRENT_TRANSCODES)) job.status = "transcoding" db.session.commit() logging.info("Starting BluRay/DVD transcoding - All titles") if job.disctype == "dvd": hb_args = job.config.HB_ARGS_DVD hb_preset = job.config.HB_PRESET_DVD elif job.disctype == "bluray": hb_args = job.config.HB_ARGS_BD hb_preset = job.config.HB_PRESET_BD get_track_info(srcpath, job) logging.debug("Total number of tracks is " + str(job.no_of_titles)) for track in job.tracks: if track.length < int(job.config.MINLENGTH): # too short logging.info("Track #" + str(track.track_number) + " of " + str(job.no_of_titles) + ". Length (" + str(track.length) + ") is less than minimum length (" + job.config.MINLENGTH + "). Skipping") elif track.length > int(job.config.MAXLENGTH): # too long logging.info("Track #" + str(track.track_number) + " of " + str(job.no_of_titles) + ". Length (" + str(track.length) + ") is greater than maximum length (" + job.config.MAXLENGTH + "). Skipping") else: # just right logging.info("Processing track #" + str(track.track_number) + " of " + str(job.no_of_titles) + ". Length is " + str(track.length) + " seconds.") filename = "title_" + str.zfill(str(track.track_number), 2) + "." + job.config.DEST_EXT filepathname = os.path.join(basepath, filename) logging.info("Transcoding title " + str(track.track_number) + " to " + shlex.quote(filepathname)) track.filename = track.orig_filename = filename db.session.commit() cmd = 'nice {0} -i {1} -o {2} --preset "{3}" -t {4} {5}>> {6} 2>&1'.format( job.config.HANDBRAKE_CLI, shlex.quote(srcpath), shlex.quote(filepathname), hb_preset, str(track.track_number), hb_args, logfile) logging.debug("Sending command: %s", cmd) try: hb = subprocess.check_output(cmd, shell=True).decode("utf-8") logging.debug("Handbrake exit code: " + hb) track.status = "success" except subprocess.CalledProcessError as hb_error: err = "Handbrake encoding of title " + str( track.track_number) + " failed with code: " + str( hb_error.returncode) + "(" + str( hb_error.output) + ")" # noqa E501 logging.error(err) track.status = "fail" track.error = err # return # sys.exit(err) track.ripped = True db.session.commit() logging.info("Handbrake processing complete") logging.debug("\n\r" + job.pretty_table()) return