def convert(directory, filename): index = 0 vtt_filepath = f"%s\\%s.vtt" % (directory, filename) srt_filepath = f"%s\\%s.srt" % (directory, filename) srt = open(srt_filepath, "w") for caption in WebVTT().read(vtt_filepath): index += 1 start = SubRipTime(0, 0, caption.start_in_seconds) end = SubRipTime(0, 0, caption.end_in_seconds) srt.write( SubRipItem(index, start, end, html.unescape( caption.text)).__str__() + "\n")
def convert_sub(vtt_file): index = 0 file_name, file_extension = os.path.splitext(vtt_file) if not file_extension.lower() == ".vtt": sys.stderr.write("Skipping %s.\n" % vtt_file) raise Exception("VTT file could not be found.") srt = open(file_name + ".srt", "w") for caption in WebVTT().read(vtt_file): index += 1 start = SubRipTime(0, 0, caption.start_in_seconds) end = SubRipTime(0, 0, caption.end_in_seconds) srt.write( SubRipItem(index, start, end, html.unescape( caption.text)).__str__() + "\n")
def is_passed(self, position): """Checks for the ending of the current subtitle. :param int position: Position in media stream (in seconds) :returns: Achievement of the subtitle ending :rtype: boolean """ if (self.has_subtitle() and SubRipTime(seconds=int(position)) >= self._subtitle.end): return True return False
script = sys.argv[0] args = sys.argv[1:] def usage(): return "%s FILE...\n" % os.path.basename(script) if len(args) < 1: sys.stderr.write(usage()) sys.exit(1) for arg in args: index = 0 file_name, file_extension = os.path.splitext(arg) if not file_extension.lower() == ".vtt": sys.stderr.write("Skipping %s.\n" % arg) continue srt = open(file_name + ".srt", "w") for caption in WebVTT().read(arg): index += 1 start = SubRipTime(0, 0, caption.start_in_seconds) end = SubRipTime(0, 0, caption.end_in_seconds) srt.write( SubRipItem(index, start, end, html.unescape( caption.text)).__str__() + "\n")