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)
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()