def test_7z_archive(self): with open(path.join(assets_path, "archive.7z"), "rb") as f: data = f.read() sub_lists = get_file_list(data, ".7z") result = { "dir1/sub1.ass": zipfile.ZipFile, "dir2/sub2.ass": zipfile.ZipFile, path.join("dir3", "dir4", "sub.ass"): P7ZIP, path.join("dir3", "sub.srt"): P7ZIP, path.join("archive", "sub4.sub"): P7ZIP, } for sub, handler in sub_lists.items(): self.assertTrue(isinstance(handler, result[sub]))
def test_rar_archive(self): with open(path.join(assets_path, "archive.rar"), "rb") as f: data = f.read() sub_lists = get_file_list(data, ".rar") result = { "dir3/sub.srt": rarfile.RarFile, "dir3/dir4/sub.ass": rarfile.RarFile, "archive/sub4.sub": rarfile.RarFile, "dir1/sub1.ass": zipfile.ZipFile, "dir2/sub2.ass": zipfile.ZipFile, } for sub, handler in sub_lists.items(): self.assertTrue(isinstance(handler, result[sub]))
def process_archive( self, video, archive_data, datatype, ): """ 解压字幕包,返回解压字幕名列表 params: video: Video object archive_data: binary archive data datatype: str, archive type return: error: str, error message extract_subs: list, [<subname, subtype>, ...] """ error = "" if datatype not in ARCHIVE_TYPES: error = "unsupported file type " + datatype return error, [] sub_lists_dict = get_file_list(archive_data, datatype) if len(sub_lists_dict) == 0: error = "no subtitle in this archive" return error, [] # get subtitles to extract if not self.single: success, sub_name = guess_subtitle(list(sub_lists_dict.keys()), video.info) if not success: error = "no guess result in auto mode" return error, [] else: sub_name = choose_subtitle(list(sub_lists_dict.keys())) # build new names sub_title, sub_type = path.splitext(sub_name) extract_subs = [[sub_name, sub_type]] if self.both: another_sub_type = ".srt" if sub_type == ".ass" else ".ass" another_sub = sub_name.replace(sub_type, another_sub_type) another_sub = path.basename(another_sub) for subname in list(sub_lists_dict.keys()): if another_sub in subname: extract_subs.append([subname, another_sub_type]) break if len(extract_subs) == 1: print("no %s subtitles in this archive" % another_sub_type) # delete existed subtitles video.delete_existed_subtitles() # extract subtitles for one_sub, one_sub_type in extract_subs: sub_new_name = video.name + video.sub_identifier + one_sub_type extract_path = path.join(video.sub_store_path, sub_new_name) with open(extract_path, "wb") as sub: file_handler = sub_lists_dict[one_sub] sub.write(file_handler.read(one_sub)) return error, extract_subs