How to use the srt.Subtitle function in srt

To help you get started, we’ve selected a few srt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github foone / SplitBySubs / split_by_subs.py View on Github external
args.srt = os.path.splitext(args.movie)[0] + '.srt'
		if not os.path.exists(args.srt):
			print >>sys.stderr,"Couldn't find SRT file! (guessed {})".format(args.srt)
			print >>sys.stderr,"Please specify SRT path explicitly!"

			sys.exit(1)

if not subtitles:
	with open(args.srt,'rb') as f:
		subtitles=list(srt.parse(f.read().decode(args.encoding)))

if args.shift is not None:
	shifted_subs = []
	shift_amount = datetime.timedelta(seconds=-args.shift)
	for e in subtitles:
		shifted_subs.append(srt.Subtitle(e.index,e.start+shift_amount,e.end+shift_amount,e.content,e.proprietary))
	subtitles = shifted_subs

EXTENSION = os.path.splitext(args.movie)[1]
if args.twitter:
	EXTENSION='.mp4'

quiet_mkdir(args.outdir)

match_pattern='*'
if args.match:
	match_pattern = '*{}*'.format(args.match.lower())

if args.subs:
	# We need the subs in a filename with no spaces, because ffmpeg filter parsing is terrible
	# so we save it locally and then delete at the end.
	quiet_erase(TMPFILE)
github smacke / subsync / subsync / subtitle_parsers.py View on Github external
def fit(self, subs, *_):
        offset_subs = []
        for sub in subs:
            offset_subs.append(srt.Subtitle(index=sub.index,
                                            start=sub.start + self.td_seconds,
                                            end=sub.end + self.td_seconds,
                                            content=sub.content))
        self.subs_ = SrtSubtitles(offset_subs, encoding=subs.encoding)
        return self
github HackerDom / qctf-starter-2018 / tasks / youtube-subtitles / youtube_subtitles / generation.py View on Github external
def generate_subtitles(sentences, durations_in_seconds):
    subtitles = []
    start = timedelta(seconds=0)
    for i, (sentence, duration) in enumerate(zip(sentences, durations_in_seconds)):
        end = start + timedelta(seconds=duration)
        subtitles.append(srt.Subtitle(index=i + 1, start=start, end=end, content=sentence))
        start = end
    return subtitles
github foone / SplitBySubs / split_by_subs.py View on Github external
quiet_mkdir(args.outdir)

match_pattern='*'
if args.match:
	match_pattern = '*{}*'.format(args.match.lower())

if args.subs:
	# We need the subs in a filename with no spaces, because ffmpeg filter parsing is terrible
	# so we save it locally and then delete at the end.
	quiet_erase(TMPFILE)
	if args.replace or args.regexreplace:
		modified_subs = []
		if args.replace:
			new_subtitle = args.replace.replace('{NL}','\n')
			for e in subtitles:
				modified_subs.append(srt.Subtitle(e.index,e.start,e.end,new_subtitle,e.proprietary))
		elif args.regexreplace:
			replacements = [
				re.match(r's\/(.+)\/(.+)\/', replace)
				for replace in 
				args.regexreplace
			]
			for e in subtitles:
				new_subtitle = e.content
				for m in replacements:
					new_subtitle = re.sub(m.group(1), m.group(2), new_subtitle)
				modified_subs.append(srt.Subtitle(e.index,e.start,e.end,new_subtitle,e.proprietary))
	else:
		modified_subs = subtitles
	with open(TMPFILE,'wb') as f:
		f.write(srt.compose(modified_subs).encode(args.encoding))
try:
github foone / SplitBySubs / split_by_subs.py View on Github external
modified_subs = []
		if args.replace:
			new_subtitle = args.replace.replace('{NL}','\n')
			for e in subtitles:
				modified_subs.append(srt.Subtitle(e.index,e.start,e.end,new_subtitle,e.proprietary))
		elif args.regexreplace:
			replacements = [
				re.match(r's\/(.+)\/(.+)\/', replace)
				for replace in 
				args.regexreplace
			]
			for e in subtitles:
				new_subtitle = e.content
				for m in replacements:
					new_subtitle = re.sub(m.group(1), m.group(2), new_subtitle)
				modified_subs.append(srt.Subtitle(e.index,e.start,e.end,new_subtitle,e.proprietary))
	else:
		modified_subs = subtitles
	with open(TMPFILE,'wb') as f:
		f.write(srt.compose(modified_subs).encode(args.encoding))
try:
	last_end = 0.0
	for entryi,entry in enumerate(subtitles):
		if not fnmatch.fnmatchcase(entry.content.lower(), match_pattern):
			continue
		if args.between:
			name = 'between'
		else:
			name = clean(entry.content)
		filename='clip{:04} {}{}'.format(entry.index,name,EXTENSION)
		path = os.path.join(args.outdir,filename)
		if args.between:
github cnbeining / autotimecode / kaldi_logmel / run.py View on Github external
for i in range(num_seg):
        feats_seg = normalize(fts[i * shift:i * shift + num_frames])
        p = model_predict(feats_seg.reshape((1, num_frames, num_freq, 1)).tolist())
        for j in range(i * shift, i * shift + 64):
            pred[j].extend([p[0][1]])
    predictions = np.array([np.median(pred[i]) if pred[i] != [] else 0 for i in range(fts.shape[0])])
    
    # Post-processing of posteriors
    labels = np.round(predictions)
    seg_times = frame2seg(labels)
    
    subs = []
    # Write start and end SAD timestamps
    for i, segment in enumerate(seg_times):
        if segment[1] - segment[0] > 0.05:
            subs.append(srt.Subtitle(index = i, start = datetime.timedelta(seconds = float(segment[0])),
                                     end = datetime.timedelta(seconds = float(segment[1])), content = ''))
    
    return '\n'.join([i.to_srt() for i in subs])

srt

A tiny library for parsing, modifying, and composing SRT files.

MIT
Latest version published 2 years ago

Package Health Score

52 / 100
Full package analysis

Popular srt functions

Similar packages