Exemplo n.º 1
0
    def read(self, content, lang='en-US'):
        caption_set = CaptionSet()
        lines = content.splitlines()
        start_line = 0
        captions = []

        while start_line < len(lines):
            if not lines[start_line].isdigit():
                break

            caption = Caption()

            end_line = self._find_text_line(start_line, lines)

            timing = lines[start_line + 1].split('-->')
            caption.start = self._srttomicro(timing[0].strip(' \r\n'))
            caption.end = self._srttomicro(timing[1].strip(' \r\n'))

            for line in lines[start_line + 2:end_line - 1]:
                # skip extra blank lines
                if not caption.nodes or line != '':
                  caption.nodes.append(CaptionNode.create_text(line))
                  caption.nodes.append(CaptionNode.create_break())

            # remove last line break from end of caption list
            caption.nodes.pop()

            captions.append(caption)
            start_line = end_line

        caption_set.set_captions(lang, captions)
        return caption_set
Exemplo n.º 2
0
def run_pipeline(url=None, hmm=None, lm=None, dict=None, caption_format="webvtt", out_file=None):
    if url is None:
        raise Exception("No URL specified!")
    pipeline = Gst.parse_launch(
        "uridecodebin name=source ! audioconvert !" + " audioresample ! pocketsphinx name=asr !" + " fakesink"
    )
    source = pipeline.get_by_name("source")
    source.set_property("uri", url)
    pocketsphinx = pipeline.get_by_name("asr")
    if hmm:
        pocketsphinx.set_property("hmm", hmm)
    if lm:
        pocketsphinx.set_property("lm", lm)
    if dict:
        pocketsphinx.set_property("dict", dict)

    bus = pipeline.get_bus()

    # Start playing
    pipeline.set_state(Gst.State.PLAYING)

    cap_set = CaptionSet()
    captions = []

    # Wait until error or EOS
    while True:
        try:
            msg = bus.timed_pop(Gst.CLOCK_TIME_NONE)
            if msg:
                # if msg.get_structure():
                #    print(msg.get_structure().to_string())

                if msg.type == Gst.MessageType.EOS:
                    break
                struct = msg.get_structure()
                if struct and struct.get_name() == "pocketsphinx":
                    if struct["final"]:
                        c = Caption()
                        c.start = struct["start_time"] / Gst.USECOND
                        c.end = struct["end_time"] / Gst.USECOND
                        c.nodes.append(CaptionNode.create_text(struct["hypothesis"]))
                        captions.append(c)
        except KeyboardInterrupt:
            pipeline.send_event(Gst.Event.new_eos())

    # Free resources
    pipeline.set_state(Gst.State.NULL)

    cap_set.set_captions("en-US", captions)
    writer = SRTWriter() if caption_format == "srt" else WebVTTWriter()
    caption_data = writer.write(cap_set)
    if out_file is not None:
        codecs.open(out_file, "w", "utf-8").write(caption_data)
    else:
        print(caption_data)
Exemplo n.º 3
0
    def read(self, content, lang='en', simulate_roll_up=False, offset=0):
        self.simulate_roll_up = simulate_roll_up
        self.offset = offset * 1000000
        # split lines
        inlines = content.splitlines()

        # loop through each line except the first
        for line in inlines[1:]:
            self._translate_line(line)

        # after converting lines, see if anything is left in paint_buffer
        if self.paint_buffer:
            self._roll_up()

        captions = CaptionSet()
        captions.set_captions(lang, self.scc)
        return captions
Exemplo n.º 4
0
    def read(self, content):
        dfxp_soup = BeautifulSoup(content)
        captions = CaptionSet()

        # Each div represents all the captions for a single language.
        for div in dfxp_soup.find_all('div'):
            lang = div.attrs.get('xml:lang', 'en')
            captions.set_captions(lang, self._translate_div(div))

        for style in dfxp_soup.find_all('style'):
            id = style.attrs.get('id')
            if not id:
                id = style.attrs.get('xml:id')
            captions.add_style(id, self._translate_style(style))

        captions = self._combine_matching_captions(captions)

        return captions
Exemplo n.º 5
0
    def new_caption_set_from_match(
            self, match: Dict[int, Dict[int, str]]) -> CaptionSet:
        new_captions = []
        for s, sentence in enumerate(self.sentences):
            for c, caption in enumerate(sentence.captions):
                trans = match[s][c]
                new_caption = deepcopy(caption.raw_caption)
                new_caption.nodes = [CaptionNode.create_text(trans.strip())]
                new_captions.append(new_caption)

                # print(f'"{caption.raw_text}"', f'"{trans}"')
        new_caption_set = CaptionSet({'en': new_captions})
        return new_caption_set
Exemplo n.º 6
0
    def read(self, content):
        content, doc_styles, doc_langs = SAMIParser().feed(content)
        sami_soup = BeautifulSoup(content)
        captions = CaptionSet()
        captions.set_styles(doc_styles)

        for language in doc_langs:
            lang_captions = self._translate_lang(language, sami_soup)
            captions.set_captions(language, lang_captions)

        for lang in captions.get_languages():
            if captions.get_captions(lang):
                return captions

        raise SAMIReaderError("Empty Caption File")
Exemplo n.º 7
0
    def replace_unknown_language(self, lang_code):
        """
        This essentially sets the "unknown" language in the caption set, by replacing the key
        with this new language code

        :param lang_code: A string with the language code to replace the unknown language with
        """
        caption_set = self.get_caption_set()

        captions = {}
        for lang in caption_set.get_languages():
            set_lang = lang_code if lang == LANGUAGE_CODE_UNKNOWN else lang
            captions[set_lang] = caption_set.get_captions(lang)

        # Replace caption_set with new version, having replaced unknown language
        self.caption_set = CaptionSet(captions,
                                      styles=dict(caption_set.get_styles()),
                                      layout_info=caption_set.layout_info)
Exemplo n.º 8
0
    def read(self, content):
        content = self.force_byte_string(content)
        content, doc_styles, doc_langs = SAMIParser().feed(content)
        sami_soup = BeautifulSoup(content)
        captions = CaptionSet()
        captions.set_styles(doc_styles)

        for language in doc_langs:
            lang_captions = self._translate_lang(language, sami_soup)
            captions.set_captions(language, lang_captions)

        if not captions.is_empty():
            return captions
        else:
            raise SAMIReaderError("Empty Caption File")
Exemplo n.º 9
0
    def convert(self, lang_code):
        """
        Converts the caption set to the VTT format

        :param lang_code: A string with one of the languages to output the captions for
        :type: lang_code: str
        :return: A string with the converted caption contents
        :rtype: str
        """
        caption_set = self.get_caption_set()
        captions = caption_set.get_captions(lang_code)

        if not captions:
            raise InvalidSubtitleLanguageError(
                "Language '{}' is not present in caption set".format(
                    lang_code))

        styles = caption_set.get_styles()
        layout_info = caption_set.get_layout_info(lang_code)
        lang_caption_set = CaptionSet({lang_code: captions},
                                      styles=dict(styles),
                                      layout_info=layout_info)
        return self.writer.write(lang_caption_set)
Exemplo n.º 10
0
def run_pipeline(url=None,
                 hmm=None,
                 lm=None,
                 dict=None,
                 caption_format='webvtt',
                 out_file=None):
    if url is None:
        raise Exception('No URL specified!')
    pipeline = Gst.parse_launch('uridecodebin name=source ! audioconvert !' +
                                ' audioresample ! pocketsphinx name=asr !' +
                                ' fakesink')
    source = pipeline.get_by_name('source')
    source.set_property('uri', url)
    pocketsphinx = pipeline.get_by_name('asr')
    if hmm:
        pocketsphinx.set_property('hmm', hmm)
    if lm:
        pocketsphinx.set_property('lm', lm)
    if dict:
        pocketsphinx.set_property('dict', dict)

    bus = pipeline.get_bus()

    # Start playing
    pipeline.set_state(Gst.State.PLAYING)

    cap_set = CaptionSet()
    captions = []

    # Wait until error or EOS
    while True:
        try:
            msg = bus.timed_pop(Gst.CLOCK_TIME_NONE)
            if msg:
                #if msg.get_structure():
                #    print(msg.get_structure().to_string())

                if msg.type == Gst.MessageType.EOS:
                    break
                struct = msg.get_structure()
                if struct and struct.get_name() == 'pocketsphinx':
                    if struct['final']:
                        c = Caption()
                        c.start = struct['start_time'] / Gst.USECOND
                        c.end = struct['end_time'] / Gst.USECOND
                        c.nodes.append(
                            CaptionNode.create_text(struct['hypothesis']))
                        captions.append(c)
        except KeyboardInterrupt:
            pipeline.send_event(Gst.Event.new_eos())

    # Free resources
    pipeline.set_state(Gst.State.NULL)

    cap_set.set_captions('en-US', captions)
    writer = SRTWriter() if caption_format == 'srt' else WebVTTWriter()
    caption_data = writer.write(cap_set)
    if out_file is not None:
        codecs.open(out_file, 'w', 'utf-8').write(caption_data)
    else:
        print(caption_data)
Exemplo n.º 11
0
stories = codecs.open('story.txt', 'r', 'utf-8').readlines()


def microsec(t):
    return t * 1000000


offset = 0.0
captions = []
for line in sys.stdin:
    if line.startswith(' '):
        continue
    tokens = line.split()
    if len(tokens) != 3:
        continue
    dirname = tokens[0]
    index = int(dirname.split('/')[-1]) - 1
    duration = float(tokens[2])
    print duration
    text = stories[index]
    cap = Caption(microsec(offset), microsec(offset + duration),
                  [CaptionNode.create_text(text)])
    offset += duration
    captions.append(cap)

caps = CaptionSet({'en': captions})

srt = codecs.open('output.srt', 'w', 'utf-8')
srt.write(SRTWriter().write(caps))
srt.close()
Exemplo n.º 12
-1
    def read(self, content, lang="en"):
        captions = CaptionSet()
        inlines = content.splitlines()
        start_line = 0
        subdata = []

        while start_line < len(inlines):
            if not inlines[start_line].isdigit():
                break

            caption = Caption()

            end_line = self._find_text_line(start_line, inlines)

            timing = inlines[start_line + 1].split("-->")
            caption.start = self._srttomicro(timing[0].strip(" \r\n"))
            caption.end = self._srttomicro(timing[1].strip(" \r\n"))

            for line in inlines[start_line + 2 : end_line - 1]:
                caption.nodes.append(CaptionData.create_text(line))
                caption.nodes.append(CaptionData.create_break())
            caption.nodes.pop()  # remove last line break from end of caption list

            subdata.append(caption)
            start_line = end_line

        captions.set_captions(lang, subdata)
        return captions