def patch_file(input_file, target_length=TARGET_LENGTH, output_file=None, verbose=True): patched = False if target_length < 0: logger.error(u"Bad length ({0}), not patching file".format(target_length)) return with open(input_file, "rb") as infile: bitstreams = list(get_bitstreams(infile)) for bitstream in bitstreams: length = bitstream.get_length() if verbose: logger.info(u"Current file length: {0}".format(pprint_time(length))) logger.info(u"Target file length: {0}".format(pprint_time(target_length))) if length > target_length: patched = True bitstream.patch_length(target_length, verbose=verbose) if patched: if output_file is None: output_file = input_file if verbose: logger.info(u"Writing patched file to {0}".format(output_file)) with open(output_file, "wb") as outfile: for bitstream in bitstreams: bitstream.write_to_file(outfile) elif verbose: logger.info(u"Not patching file; file already appears to be {0} or shorter.".format( pprint_time(target_length)))
def copy_songs(input_path, target_dir, verbose=False): logger.info(u"INPUT DIR: {0}".format(repr(input_path))) all_files = [os.path.join(input_path, f) for f in os.listdir(input_path)] dirs = [f for f in all_files if os.path.isdir(f)] oddballs = [f for f in all_files if (not os.path.isdir(f)) and (not os.path.isfile(f))] if len(oddballs) > 0: logger.info(u"ODDBALLS: {0}".format(repr(oddballs))) # If directories present: recurse into them. if len(dirs) > 0: for d in dirs: copy_songs(d, target_dir, verbose=verbose) # Check whether this is a song directory. files = [f for f in all_files if os.path.isfile(f)] stepfile_exists = any( (f.endswith(".sm") or f.endswith(".dwi")) for f in all_files) if not stepfile_exists: return # This is a song directory. Are we compatible? # Currently we must have a .sm and .ogg file. .dwi and .mp3 are # not supported. sm_exists = any(f.endswith(".sm") for f in all_files) ogg_exists = any(f.endswith(".ogg") for f in all_files) mp3_exists = any(f.endswith(".mp3") for f in all_files) if not sm_exists: logger.error(u"Directory {0}: Could not find .sm; only .dwi was found. Skipping.".format(input_path)) return if not ogg_exists: if any(f.endswith(".mp3") for f in all_files): logger.error(u"Directory {0}: Could not find .ogg; only .mp3 was found. Skipping.".format(input_path)) else: logger.error(u"Directory {0}: Could not find .ogg. Skipping.".format(input_path)) return # We are compatible. Check for destination directory; complain # LOUDLY if not able to create it. song_dir_name = os.path.split(input_path)[-1] target_song_dir = os.path.join( target_dir, u"In The Groove 2", u"Songs", song_dir_name) if os.path.exists(target_song_dir): logger.error(u"ERROR: {0} already exists; not copying files from {1}.".format(target_song_dir, input_path)) return os.makedirs(target_song_dir) for ext in ".sm", ".ogg": for src_file in (f for f in all_files if f.endswith(ext)): dest_file = os.path.join( target_song_dir, os.path.basename(src_file)) if verbose: logger.info(u"Copying: {0}\n to: {1}".format(src_file, dest_file)) shutil.copyfile(src_file, dest_file)
def check_file(input_file, target_length, verbose=True): if target_length < 0: logger.error(u"Bad length ({0}), not patching file".format(target_length)) return with open(input_file, "rb") as infile: bitstreams = list(get_bitstreams(infile)) for bitstream in bitstreams: length = bitstream.get_length() if verbose: logger.info(u"Current file length: {0}".format(pprint_time(length))) logger.info(u"Target file length: {0}".format(pprint_time(target_length))) if length > target_length: logger.error(u"File exceeds {0}. Length: {1}".format( pprint_time(target_length), pprint_time(length))) return False else: if verbose: logger.info(u"File passes length check.") continue return True
def get_packets(pages): """Generates packets from pages until a last page marker is found.""" data = [] for i, page in enumerate(pages): #print "{0:4d}".format(i), page if page.continued_packet and len(data) == 0: raise UnexpectedContinuedPacket() for j in xrange(page.segments): segment = page.get_segment(j) data.append(segment) if page.seg_table[j] < 255: yield "".join(data) data = [] if page.last_page: break if len(data) > 0: # I *think* the patch should still work - the patch # operates on the page level, but this error is at the # underlying packet; it shouldn't matter. logger.error(u"WARNING: Unterminated packet detected, ignoring.")
def run(target_dir, input_paths, length_patch=True, verbose=False, ext_logger=None): global logger try: if logger is not None: logger = ext_logger oggpatch.set_logger(logger) create_target_dir_structure(target_dir, verbose=verbose) for input_path in input_paths: copy_songs(input_path, target_dir, verbose=verbose) # *NOTE:* If no input paths are specified, this tool can be used # to patch the length on existing ogg files in the target dir. if length_patch: patch_length(target_dir, verbose=verbose) except: msg = traceback.format_exc() try: enc_msg = msg.decode() except UnicodeDecodeError: enc_msg = repr(msg).decode() logger.error(enc_msg)