def read_captions(captions, options): reader_kwargs = { 'read_invalid_positioning': options.read_invalid_positioning } scc_reader = pycaption.SCCReader(**reader_kwargs) srt_reader = pycaption.SRTReader(**reader_kwargs) sami_reader = pycaption.SAMIReader(**reader_kwargs) dfxp_reader = pycaption.DFXPReader(**reader_kwargs) vtt_reader = pycaption.WebVTTReader(**reader_kwargs) if scc_reader.detect(captions): if options.lang: return scc_reader.read(captions, lang=options.lang, offset=int(options.offset)) else: return scc_reader.read(captions, offset=int(options.offset)) elif srt_reader.detect(captions): return srt_reader.read(captions) elif sami_reader.detect(captions): return sami_reader.read(captions) elif dfxp_reader.detect(captions): return dfxp_reader.read(captions) elif vtt_reader.detect(captions): return vtt_reader.read(captions) else: raise Exception('No caption format detected :(')
def read_captions(captions, options): scc_reader = pycaption.SCCReader() srt_reader = pycaption.SRTReader() sami_reader = pycaption.SAMIReader() dfxp_reader = pycaption.DFXPReader() if scc_reader.detect(captions): if options.lang: return scc_reader.read(captions, lang=options.lang, offset=int(options.offset)) else: return scc_reader.read(captions, offset=float(options.offset)) elif srt_reader.detect(captions): return srt_reader.read(captions) elif sami_reader.detect(captions): return sami_reader.read(captions) elif dfxp_reader.detect(captions): return dfxp_reader.read(captions) else: raise Exception('No caption format detected :(')
def run(self): for input_file in self.input_files: input_file_name = os.path.basename(input_file) input_type = os.path.splitext(input_file)[1].lower()[1:] output_file = os.path.join( self.output_folder, os.path.splitext(input_file_name)[0] + '.' + self.output_type) output_file_name = os.path.basename(output_file) if os.path.exists(output_file) and not self.overwrite_on: self.log_signal.emit("{}을 건너뜁니다...".format(input_file_name)) continue self.log_signal.emit("{}을 읽습니다...".format(input_file_name)) with open(output_file, 'w', encoding=self.output_encoding) as file_out: reader = None encoding = None content = None with open(input_file, 'rb') as file_in: encoding = chardet.detect(file_in.read())['encoding'] with open(input_file, 'r', encoding=encoding) as file_in: content = file_in.read() if input_type == "smi": reader = pycaption.SAMIReader().read(content) elif input_type == "srt": reader = pycaption.SRTReader().read(content) if self.output_type == "smi": file_out.write(pycaption.SAMIWriter().write(reader)) elif self.output_type == "srt": file_out.write(pycaption.SRTWriter().write(reader)) elif self.output_type == "txt": file_out.write(TextWriter().write(reader)) elif self.output_type == "ats": open(output_file, 'wb').write(AtsWriter().write(reader)) self.log_signal.emit("{}으로 변환했습니다".format(output_file_name))