def __init__(self, index=0, start=None, end=None, text='', position=''): try: self.index = int(index) except (TypeError, ValueError): # try to cast as int, but it's not mandatory self.index = index self.start = SubRipTime.coerce(start or 0) self.end = SubRipTime.coerce(end or 0) self.position = str(position) self.text = str(text)
def __init__(self, index=0, start=None, end=None, text=u'', position=u''): try: self.index = int(index) except ValueError: raise InvalidIndex(repr(index)) self.start = SubRipTime.coerce(start or 0) self.end = SubRipTime.coerce(end or 0) self.position = unicode(position) self.text = unicode(text)
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 from_string(cls, source): match = cls.RE_ITEM.match(source) if not match: raise InvalidItem(source) data = dict(match.groupdict()) for group in ('start', 'end'): data[group] = SubRipTime.from_string(data[group]) return cls(**data)
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
def text_at_time(self, time): """ text_at_time() Get text that, according to subtitles, should appear on screen exactly at given time. """ time = SubRipTime.coerce(time) for item in self: if item.start <= time and time <= item.end: return item.text
def get_video_length(path): try: process = subprocess.Popen(['avconv', '-i', path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = process.communicate() matches = re.search(r"Duration:\s{1}(?P<hours>\d+?):(?P<minutes>\d+?):(?P<seconds>\d+\.\d+?),", stdout, re.DOTALL).groupdict() seconds = Decimal(matches['seconds']).quantize(Decimal('1.000')) secsAndMilli = str(seconds).partition('.') time = matches['hours'].zfill(2)+':'+matches['minutes'].zfill(2)+':'+secsAndMilli[0].zfill(2)+','+secsAndMilli[2].zfill(3) return SubRipTime.from_string(time) except AttributeError: print "No such file: ",path sys.exit(1)
def __init__(self, index=0, start=None, end=None, text=u'', position=u''): self.index = int(index) self.start = SubRipTime.coerce(start or 0) self.end = SubRipTime.coerce(end or 0) self.position = unicode(position) self.text = unicode(text)
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")
parser.add_argument('output', help='The file where the fixed subs should be saved') parser.add_argument('-i', '--inputVideo1', nargs=1, help='The first video file of the two to combine.') parser.add_argument('-o', '--offset2', nargs=1, help='Offset of second input file from end of first in format HH:MM:SS,mmm') parser.add_argument('-e', '--encoding', nargs=1, help='Encoding of the subtitle files, must be the same. Default is utf-8.') args = parser.parse_args() if not args.inputVideo1 and not args.offset2: parser.error('One of -i/--inputVideo1 or -o/--offset2 are required') sys.exit(1) zeroTime = "00:00:00,000" if args.inputVideo1: length1Time = get_video_length(args.inputVideo1[0]) offset2Time = SubRipTime.from_string(zeroTime) inVid1 = args.inputVideo1[0] if args.offset2: offset2Time = SubRipTime.from_string(args.offset2[0]) length1Time = SubRipTime.from_string(zeroTime) offset2 = args.offset2[0] inSubName1 = args.input1 inSubName2 = args.input2 outSubName = args.output if args.encoding: encoding = args.encoding[0] else: encoding = args.encoding try: inSub1 = SubRipFile.open(inSubName1,encoding) except AttributeError:
print('Converting and validating subtitles...') with open('subs.srt', 'w') as srt: index = 0 for caption in WebVTT().read("subs.vtt"): index += 1 for find, replace in unicode_fixes.items(): caption.text = re.sub(find, replace, caption.text, flags=re.MULTILINE) if caption.text.find('�') > -1: raise Exception( 'FIXME: Found bad Unicode character at time index ' + caption.start + ': ' + caption.text) start = SubRipTime.from_ordinal(round(caption.start_in_seconds * 1000)) end = SubRipTime.from_ordinal(round(caption.end_in_seconds * 1000)) srt.write( SubRipItem(index, start, end, html.unescape( caption.text)).__str__() + "\n") print('Downloading poster...') poster_url = master_json['poster'] with urlopen(poster_url) as posterurl: data = posterurl.read() with open('cover.jpg', 'wb') as posterjpg: posterjpg.write(data) print('Downloading master playlist...') master_playlist = m3u8.load(master_json['sources'][1]['src'])