def shift(source_sub_path, target_sub_path, timedelta=None, timemap_=None, diff_delta_handler=SHIFT_APART, source_media_path=None, target_media_path=None, playertype=None): """平移一个字幕的时间轴 source_sub_path 源字幕路径 target_sub_path 目标字幕路径 source_media_path 源媒体路径 target_media_path 目标媒体路径 timedelta 时间偏移表 timemap_ 时间映射表 当 timedelta 为 None 时会使用 timemap_ 生成 timedelta diff_delta_handler 开始时间与结束时间偏移量不同时的处理方式 """ if timedelta is None: timedelta = timemap.normalize(timemap_) player_name = player.getplayer(playertype).name if playertype else None subs = Subtitle(source_sub_path) subs.shift(timedelta, diff_delta_handler) subs.set_resync_info(source_media_path, target_media_path, player_name) subs.save_as(target_sub_path)
def start_resync(self): # 文件列表 types, trees = zip(*self.ct_trees) if min([x.topLevelItemCount() for x in trees]) < 1: QMessageBox.critical(self, "错误", "文件列表中没有文件") return videos = [x.topLevelItem(0).filename for x in trees] # 获取时间映射表 try: self.hide() timemapper = FormTimeMapper(types, videos) timemapper.exec() finally: self.show() self.activateWindow() # 准备文件名 videos_src = [] videos_dst = [] timelists_src = [] timelists_dst = [] subtitless_src = [] for type_, tree, video, timelist in \ zip(types, trees, videos, timemapper.timemap): if type_ == 'src': videos_src.append(video) timelists_src.append(timelist) subtitless_src.append([x.filename for x in tree.topLevelItem(0).children()]) else: videos_dst.append(video) timelists_dst.append(timelist) tree.takeTopLevelItem(0) for video_dst, timelist_dst in zip(videos_dst, timelists_dst): for video_src, timelist_src, subtitles_src in \ zip(videos_src, timelists_src, subtitless_src): timedelta = timemap.normalize(zip(timelist_src, timelist_dst)) for sub_src in subtitles_src: # 生成字幕文件名 video_src_mainname = os.path.splitext(os.path.split(video_src)[1])[0] sub_src_name = os.path.split(sub_src)[1] video_dst_mainname = os.path.splitext(os.path.split(video_dst)[1])[0] if sub_src_name.startswith(video_src_mainname): sub_dst_name = video_dst_mainname + sub_src_name[len(video_src_mainname):] else: sub_dst_name = sub_src_name sub_dst = os.path.join(os.path.dirname(video_dst), sub_dst_name) # 调整字幕 subtitle.shift(sub_src, sub_dst, timedelta, None, None, video_src, video_dst, config.playertype) self.statusBar().showMessage("调整成功")
def _test(self, input_, output): input_ = [[to_time(v) for v in item] for item in input_] output = [dict(zip(('delta', 'until'), map(to_time, item))) for item in output] self.assertEqual(normalize(input_), output)