import os import pathlib import re import subprocess def build_mkvmerge_cmd(path_to_mks_file, srt_files): cmd = [ f'mkvmerge -o "{path_to_mks_file}"' ] srt_files_as_dict = {} for srt_file in srt_files: file_name = os.path.splitext( os.path.basename(srt_file) )[0].lower().replace("_", " ") srt_files_as_dict[file_name] = srt_file if "german forced" in srt_files_as_dict: p = srt_files_as_dict["german forced"] cmd.append( f'--default-track-flag 0:true --forced-display-flag 0:true --language 0:de --track-name 0:"Forced" "{p}"' ) if "german full" in srt_files_as_dict: p = srt_files_as_dict["german full"] cmd.append( f'--default-track-flag 0:false --forced-display-flag 0:false --language 0:de --track-name 0:"Full" "{p}"' ) if "english forced" in srt_files_as_dict: p = srt_files_as_dict["english forced"] cmd.append( f'--default-track-flag 0:false --forced-display-flag 0:false --language 0:en --track-name 0:"Forced" "{p}"' ) if "english full" in srt_files_as_dict: p = srt_files_as_dict["english full"] cmd.append( f'--default-track-flag 0:false --forced-display-flag 0:false --language 0:en --track-name 0:"Full" "{p}"' ) return " ".join(cmd) def get_all_episode_folders(path_to_season): return get_all_subfolders_matching_regex( path_to_season, r'^E\d\d$' ) def get_all_srt_files_in_folder(path_to_episode): regex = re.compile(r'^Staffel \d+$') srt_files = [] for file_name in os.listdir(path_to_episode): file_path = os.path.join(path_to_episode, file_name) if os.path.isdir(file_path): continue if not is_valid_srt_file(file_name): continue srt_files.append(file_path) srt_files.sort() return srt_files def get_all_season_folers(path_to_series): return get_all_subfolders_matching_regex( path_to_series, r'^Staffel \d+$' ) def get_all_subfolders_matching_regex(path_to_base_folder, pattern): regex = re.compile(pattern) subfolders = [] for folder_name in os.listdir(path_to_base_folder): folder_path = os.path.join(path_to_base_folder, folder_name) if not os.path.isdir(folder_path): continue if not regex.match(folder_name): continue subfolders.append(folder_path) subfolders.sort() return subfolders def get_episode_indicator(path_to_episode): return os.path.basename( os.path.normpath(path_to_episode) ) def get_season_indicator(path_to_season): match = re.search(r'.* (\d+)$', path_to_season) if not match: return None if len(match.group(1)) == 1: return "S0" + match.group(1) return "S" + match.group(1) def is_valid_srt_file(file_name): regex = re.compile(r'^((G|g)erman|(E|e)nglish)[\_ ]((F|f)orced|(F|f)ull)\.srt$') return regex.match(file_name) != None if __name__ == "__main__": path_to_series = pathlib.Path(__file__).parent.resolve() for path_to_season in get_all_season_folers(path_to_series): path_to_mks_folder = os.path.join(path_to_season, "__mks__") season_indicator = get_season_indicator(path_to_season) for path_to_episode in get_all_episode_folders(path_to_season): srt_files = get_all_srt_files_in_folder(path_to_episode) if len(srt_files) == 0: continue os.makedirs(path_to_mks_folder, exist_ok=True) episode_indicator = get_episode_indicator(path_to_episode) filename_mks = f"{season_indicator}{episode_indicator}.mks" path_to_mks_file = os.path.join(path_to_mks_folder, filename_mks) print(f"Creating: {path_to_mks_file}") subprocess.run( build_mkvmerge_cmd(path_to_mks_file, srt_files), shell=True, check=True, capture_output=True, text=True )