Example #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
Example #2
0
    def _translate_lang(self, language, sami_soup):
        captions = []
        milliseconds = 0

        for p in sami_soup.select('p[lang|=%s]' % language):
            milliseconds = int(float(p.parent['start']))
            start = milliseconds * 1000
            end = 0

            if captions != [] and captions[-1].end == 0:
                captions[-1].end = milliseconds * 1000

            if p.get_text().strip():
                self.line = []
                self._translate_tag(p)
                text = self.line
                styles = self._translate_attrs(p)
                caption = Caption()
                caption.start = start
                caption.end = end
                caption.nodes = text
                caption.style = styles
                captions.append(caption)

        if captions and captions[-1].end == 0:
            # Arbitrarily make this last 4 seconds. Not ideal...
            captions[-1].end = (milliseconds + 4000) * 1000

        return captions
Example #3
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)
Example #4
0
    def _translate_p_tag(self, p_tag):
        start, end = self._find_times(p_tag)
        self.nodes = []
        self._translate_tag(p_tag)
        styles = self._translate_style(p_tag)

        caption = Caption()
        caption.start = start
        caption.end = end
        caption.nodes = self.nodes
        caption.style = styles
        return caption
Example #5
0
    def _convert_to_caption(self, buffer, start):
        # check to see if previous caption needs an end-time
        if self.scc and self.scc[-1].end == 0:
            self.scc[-1].end = start

        # initial variables
        caption = Caption()
        caption.start = start
        caption.end = 0 # Not yet known; filled in later
        self.open_italic = False
        self.first_element = True

        # split into elements (e.g. break, italics, text)
        for element in buffer.split('<$>'):
            # skip empty elements
            if element.strip() == '':
                continue

            # handle line breaks
            elif element == '{break}':
                self._translate_break(caption)

            # handle open italics
            elif element == '{italic}':
                # add italics
                caption.nodes.append(CaptionData.create_style(True, {'italics': True}))
                # open italics, no longer first element
                self.open_italic = True
                self.first_element = False

            # handle clone italics
            elif element == '{end-italic}' and self.open_italic:
                caption.nodes.append(CaptionData.create_style(False, {'italics': True}))
                self.open_italic = False

            # handle text
            else:
                # add text
                caption.nodes.append(CaptionData.create_text(' '.join(element.decode("utf-8").split())))
                # no longer first element
                self.first_element = False

        # close any open italics left over
        if self.open_italic == True:
            caption.nodes.append(CaptionData.create_style(False, {'italics': True}))

        # remove extraneous italics tags in the same caption
        self._remove_italics(caption)

        # only add captions to list if content inside exists
        if caption.nodes:
            self.scc.append(caption)
Example #6
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)
Example #7
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()
Example #8
0
 def __init__(self, raw_caption: Caption):
     self.raw_caption = raw_caption
     self.raw_text = raw_caption.get_text()
     self.lines = self.raw_text.split('\n')
     assert len(self.lines) > 0
Example #9
-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