def __build_sentence_list(id: int, caption_list: list, f_name: str, source_film: str = None, target_audio: str = None): s_list = [] for caption in caption_list: try: id += 1 level = CaptionFactory.__judge_level(caption.english) if source_film: try: audio_file = film_spliter.spliter.Spliter.split_to_mp3( source_film, target_audio, caption) except ValueError as e: LogWriter.write_warning(e, "视频裁切模块错误") audio_file = "" except IOError as e: LogWriter.write_warning(e, "读取视频文件或写入音频文件失败") audio_file = "" s_list.append( data_connector.model_sentence.ModelSentence( id, caption, f_name, level, target_audio + "\\" + audio_file)) else: s_list.append( data_connector.model_sentence.ModelSentence( id, caption, f_name, level)) except ValueError as e: LogWriter.write_warning(e, "字符串分割错误:原句为" + caption.english) return s_list
def load_dir(path: str, id: int, audio_path: str): sentence_list = [] files = os.listdir(path) for file in files: if file.endswith(".srt"): is_have_film = CaptionFactory.__find_film(path, file) try: caption_list = CaptionFactory.load_srt_file(path + '\\' + file) if not is_have_film: sentence_list += CaptionFactory.__build_sentence_list( id, caption_list, file[:-4]) else: film_source = path + '\\' + file[:-4] + ".mp4" sentence_list += CaptionFactory.__build_sentence_list( id, caption_list, file[:-4], film_source, audio_path) id += len(caption_list) except IOError as e: LogWriter.write_warning(e, "字幕文件读取失败,文件名:" + str(file)) except ValueError as e: LogWriter.write_warning(e, "字幕文件解析失败,文件名:" + str(file)) return sentence_list
def __init__(self, start_id: int = 0): try: self.__db_setting = self.__decode_xml() print('分解字幕文件并分割视频....') self.__sentence_list = analyser.caption_factory.CaptionFactory.load_dir( self.__caption_path, start_id, self.__audio_path) self.__word_list = [] try: dm = data_connector.data_manager.DataManager(self.__db_setting) print('分析例句并上传....') for sentence in self.__sentence_list: self.__split_word(sentence) dm.execute_sql(sentence.to_sql()) print('上传分析结果....') for word in self.__word_list: dm.execute_sql(word.to_sql()) dm.close_connection() except Exception as e: LogWriter.write_warning(e, "连接数据库失败") raise e except Exception as e: LogWriter.write_warning(e, "读取配置文件失败") raise e print('作业结束。')