コード例 #1
0
ファイル: test_tmp.py プロジェクト: utylee/pysubs2
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()
コード例 #2
0
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()
コード例 #3
0
def ssaevent_trim(event: ps2.SSAEvent, ir: List[int]):
    r"""
    Given an event that overlaps with ir
    if subtitle is only on one side of IR
        trim subtitle to not overlap IR
    if subtitle is on both sides of IR
        split subtitle into two and trim each
    if subtitle is entirely inside IR
        drop it
    :param event: SSAEvent to trim/split+trim/drop
    :param ir: Ignore range, two integers representing milliseconds
    :return: List of trimmed events. There may be zero, one, or two events returned
    """

    assert len(ir) == 2
    trimmed = []
    if ir[0] <= event.start < ir[1] and ir[0] < event.end <= ir[1]:
        # event falls completely inside an ignore range
        return trimmed
    if ir[0] <= event.start < ir[1] and ir[1] < event.end:
        # event starts inside ignore range, ends outside of it
        event.start = ir[1]
        trimmed.append(event)
        return trimmed
    if event.start < ir[0] and ir[0] < event.end <= ir[1]:
        # event starts outside of ignore range, ends inside of it
        event.end = ir[0]
        trimmed.append(event)
        return trimmed
    if event.start < ir[0] and ir[1] < event.end:
        # split into two pieces
        event2 = copy.deepcopy(event)
        event.end = ir[0]
        trimmed.append(event)
        event2.start = ir[1]
        trimmed.append(event2)
        return trimmed
    # should never get here
    assert False
コード例 #4
0
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()
コード例 #5
0
ファイル: test_subrip.py プロジェクト: hpsbranco/pysubs2
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()
コード例 #6
0
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