def test_simple_write(): subs = SSAFile() e1 = SSAEvent() e1.start = 0 e1.end = 60000 e1.text = "ten--chars" e2 = SSAEvent() e2.start = 60000 e2.end = 120000 e2.text = "ten--chars-ten-chars" e3 = SSAEvent() e3.start = 60000 e3.end = 120000 e3.text = "Invisible subtitle." e3.is_comment = True subs.append(e1) subs.append(e2) subs.append(e3) ref = dedent("""\ 00:00:00:ten--chars 00:01:00:ten--chars-ten-chars """) text = subs.to_string("tmp") assert text.strip() == ref.strip()
def test_write_drawing(): # test for 7bde9a6c3a250cf0880a8a9fe31d1b6a69ff21a0 subs = SSAFile() e1 = SSAEvent() e1.start = 0 e1.end = 60000 e1.text = r"{\p1}m 0 0 l 100 0 100 100 0 100{\p0}test" e2 = SSAEvent() e2.start = 60000 e2.end = 120000 e2.text = "Subtitle number\\Ntwo." subs.append(e1) subs.append(e2) ref = dedent("""\ 1 00:01:00,000 --> 00:02:00,000 Subtitle number two. """) text = subs.to_string("srt") assert text.strip() == ref.strip()
def test_simple_write(): subs = SSAFile() e1 = SSAEvent() e1.start = 0 e1.end = 60000 e1.text = "An example subtitle." e2 = SSAEvent() e2.start = 60000 e2.end = 120000 e2.text = "Subtitle number\\Ntwo." e3 = SSAEvent() e3.start = 60000 e3.end = 120000 e3.text = "Invisible subtitle." e3.is_comment = True subs.append(e1) subs.append(e2) subs.append(e3) ref = dedent("""\ WEBVTT 1 00:00:00,000 --> 00:01:00,000 An example subtitle. 2 00:01:00,000 --> 00:02:00,000 Subtitle number two. """) text = subs.to_string("vtt") assert text.strip() == ref.strip()
def test_simple_write(): subs = SSAFile() e1 = SSAEvent() e1.start = 0 e1.end = 60000 e1.text = "An example subtitle." e2 = SSAEvent() e2.start = 60000 e2.end = 120000 e2.text = "Subtitle number\\Ntwo." e3 = SSAEvent() e3.start = 60000 e3.end = 120000 e3.text = "Invisible subtitle." e3.is_comment = True subs.append(e1) subs.append(e2) subs.append(e3) ref = dedent("""\ 1 00:00:00,000 --> 00:01:00,000 An example subtitle. 2 00:01:00,000 --> 00:02:00,000 Subtitle number two. """) text = subs.to_string("srt") assert text.strip() == ref.strip()
def compress(subs, max_chars=30, max_stretch_time=3, max_oldest_time=10): new_subs = SSAFile() # Phase 1 based on character count so that we dont overflow the screen # Phase 2 if the end of the last subtitle is close to the start of the next we want to stretch out the end # Phase 3 is to make sure that the oldest word on the screen has not been there for too long char_count = 0 current_text = '' current_event = None oldest_start_time = 0 for sub in subs: last_event = current_event current_event = SSAEvent() current_event.start = sub.start current_event.end = sub.end char_count += len(sub.text) # Check the character count and reset if needed if char_count > max_chars: current_text = sub.text char_count = len(sub.text) else: current_text = current_text + ' ' + sub.text # Check the stretch of subtitles make last one longer if last_event and current_event.start - last_event.end < max_stretch_time * 1000: last_event.end = current_event.start else: current_text = sub.text char_count = len(sub.text) # Make sure that the oldest subtitle on the screen is not too old if current_event.start - oldest_start_time > max_oldest_time * 1000: current_text = sub.text char_count = len(sub.text) oldest_start_time = sub.start current_event.text = current_text new_subs.append(current_event) logger.info( f'Compressed {len(subs)} subtitles into {len(new_subs)} subtitles') return new_subs