How to use the pysubs2.SSAFile.from_string function in pysubs2

To help you get started, we’ve selected a few pysubs2 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 tkarabela / pysubs2 / tests / test_substation.py View on Github external
def test_aegisub_project_garbage():
    subs = SSAFile.from_string(AEGISUB_PROJECT_GARBAGE_FILE)
    garbage_section = dedent("""
        [Aegisub Project Garbage]
        Last Style Storage: Default
        Video File: ?dummy:23.976000:40000:640:480:47:163:254:
        Video AR Value: 1.333333
        Video Zoom Percent: 0.500000
        Active Line: 2""")

    assert garbage_section in subs.to_string("ass")
github tkarabela / pysubs2 / tests / test_mpl2.py View on Github external
def test_simple_parsing():
    test_input1 = "[123][456] Line 1"
    subs1 = SSAFile.from_string(test_input1)
    assert len(subs1) == 1
    assert subs1[0] == SSAEvent(start=make_time(ms=12300), end=make_time(ms=45600), text="Line 1")

    test_input2 = "[123][456] / Line 1|   Line 2/2"
    subs2 = SSAFile.from_string(test_input2)
    assert len(subs2) == 1
    assert subs2[0] == SSAEvent(start=make_time(ms=12300), end=make_time(ms=45600), text="{\i1}Line 1{\i0}\\NLine2/2")

    test_input3 = dedent("""
    [123][456] Line 1
    [321][456] / Line 2|   Line 3
    (123)(456)This line should not be parsed
    This line should also not be parsed
    
    [789][1234] /Line 4""")
github tkarabela / pysubs2 / tests / test_subrip.py View on Github external
text = 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.
    """)

    ref = SSAFile()
    ref.append(SSAEvent(start=0, end=make_time(m=1), text="An example subtitle."))
    ref.append(SSAEvent(start=make_time(m=1), end=make_time(m=2), text="Subtitle number\\Ntwo."))

    subs = SSAFile.from_string(text)
    assert subs.equals(ref)
github tkarabela / pysubs2 / tests / test_subrip.py View on Github external
def test_read_bad_tags():
    """missing opening/closing tag, bad nesting, extra whitespace"""

    text = dedent("""\
    1
    00:00:10,500 --> 00:00:13,000
    &lt; u&gt;<i><font color="red">Elephant's &lt; s&gt;Dream&lt; /  i &gt; Is Long And Badly Nested

    """)

    ref = SSAFile()
    ref.append(SSAEvent(start=make_time(s=10.5), end=make_time(s=13), text="{\\u1}{\\i1}Elephant's {\\s1}Dream{\\i0} Is Long{\\s0} And Badly Nested"))

    subs = SSAFile.from_string(text)
    assert subs.equals(ref)
</font></i>
github morpheus65535 / bazarr / libs / subliminal_patch / subtitle.py View on Github external
# something else, try to return srt
        try:
            logger.debug("Trying parsing with PySubs2")
            try:
                # in case of microdvd, try parsing the fps from the subtitle
                subs = pysubs2.SSAFile.from_string(text)
                if subs.format == "microdvd":
                    logger.info("Got FPS from MicroDVD subtitle: %s", subs.fps)
                else:
                    logger.info("Got format: %s", subs.format)
            except pysubs2.UnknownFPSError:
                # if parsing failed, suggest our media file's fps
                logger.info("No FPS info in subtitle. Using our own media FPS for the MicroDVD subtitle: %s",
                            self.plex_media_fps)
                subs = pysubs2.SSAFile.from_string(text, fps=self.plex_media_fps)

            unicontent = self.pysubs2_to_unicode(subs)
            self.content = unicontent.encode(self._guessed_encoding)
        except:
            logger.exception("Couldn't convert subtitle %s to .srt format: %s", self, traceback.format_exc())
            return False

        self._is_valid = True
        return True
github kaegi / alass / statistics-helpers / generate_database_from_videolist.py View on Github external
print("error occured")

    if data == None:
        print("Error getting data - skipping subtitle!", file=sys.stderr)
        return None

    print("Done!", file=sys.stderr)

    ssa_styling_pattern = re.compile(r"\s*#?{[^}]*}#?\s*")  # remove SSA-styling info
    newline_whitespace = re.compile(
        r"\s*\n\s*"
    )  # remove unnecessary trailing space around newlines

    line_data = []

    decoded_sub_data = pysubs2.SSAFile.from_string(
        data[sub_id], encoding=opensubtitles_metadata["SubEncoding"]
    )
    for line in decoded_sub_data:
        if "www.opensubtitles.org" in line.text.lower():
            continue  # remove ad as this throws of pairing/statistics (same text in different places)

        text = line.text.replace("\n", "").replace("\r", "")
        text = ssa_styling_pattern.sub("", text)
        text = re.sub(r"[\x00-\x1f\x7f-\x9f]", "", text)
        text = text.replace(r"\N", "\n")
        text = text.strip()
        text = newline_whitespace.sub("\n", text)

        if line.start &lt; line.end:
            line_data.append({"start_ms": line.start, "end_ms": line.end, "text": text})
        elif line.start &gt; line.end:
github morpheus65535 / bazarr / libs / subliminal_patch / subtitle.py View on Github external
# valid srt
        try:
            pysrt.from_string(text, error_handling=pysrt.ERROR_RAISE)
        except Exception:
            logger.error("PySRT-parsing failed, trying pysubs2")
        else:
            self._is_valid = True
            return True

        # something else, try to return srt
        try:
            logger.debug("Trying parsing with PySubs2")
            try:
                # in case of microdvd, try parsing the fps from the subtitle
                subs = pysubs2.SSAFile.from_string(text)
                if subs.format == "microdvd":
                    logger.info("Got FPS from MicroDVD subtitle: %s", subs.fps)
                else:
                    logger.info("Got format: %s", subs.format)
            except pysubs2.UnknownFPSError:
                # if parsing failed, suggest our media file's fps
                logger.info("No FPS info in subtitle. Using our own media FPS for the MicroDVD subtitle: %s",
                            self.plex_media_fps)
                subs = pysubs2.SSAFile.from_string(text, fps=self.plex_media_fps)

            unicontent = self.pysubs2_to_unicode(subs)
            self.content = unicontent.encode(self._guessed_encoding)
        except:
            logger.exception("Couldn't convert subtitle %s to .srt format: %s", self, traceback.format_exc())
            return False