title: Subtitles/captions with Microsoft Azure Speech-to-text in Python
body: <p>I've been trying to figure out how to make subtitles with Microsoft Azure Speech Recognition service in Python, but can't figure it out. I've followed the tips someone else has answered here on getting the individual words, but even formatting those to .srt or .vtt seems convoluted.
  Here's the code:</p>
  <pre><code>import azure.cognitiveservices.speech as speechsdk
  
  
  def speech_recognize_continuous_from_file():
      &quot;&quot;&quot;performs continuous speech recognition with input from an audio file&quot;&quot;&quot;
      # &lt;SpeechContinuousRecognitionWithFile&gt;
      speech_key, service_region = &quot;{api-key}&quot;, &quot;{serive-region}&quot;
      speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
      
      audio_filename = &quot;{for example: video.wav}&quot;
      audio_config = speechsdk.audio.AudioConfig(filename=audio_filename)
      
      speech_config.speech_recognition_language=&quot;en-US&quot;
      speech_config.request_word_level_timestamps()
  
      speech_config.enable_dictation()
      speech_config.output_format = speechsdk.OutputFormat(1)
      
      speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
  
      done = False
      
      results = []
      
      transcript = []
      words = []
      
      def handle_final_result(evt):
          import json
          results = json.loads(evt.result.json)
          transcript.append(results['DisplayText'])
      confidence_list_temp = [item.get('Confidence') for item in results['NBest']]
      max_confidence_index = confidence_list_temp.index(max(confidence_list_temp))
          words.extend(results['NBest'][max_confidence_index]['Words'])
      def stop_cb(evt):
          &quot;&quot;&quot;callback that stops continuous recognition upon receiving an event `evt`&quot;&quot;&quot;
          print('CLOSING on {}'.format(evt))
          speech_recognizer.stop_continuous_recognition()
          nonlocal done
          done = True
          print(&quot;Transcript display list:\n&quot;)
          print(transcript)
          print(&quot;\nWords\n&quot;)
          print(words)
          print(&quot;\n&quot;)
  
  
      speech_recognizer.recognized.connect(handle_final_result)
      # Connect callbacks to the events fired by the speech recognizer
      speech_recognizer.recognizing.connect(lambda evt: format(evt))
      speech_recognizer.recognized.connect(lambda evt: format(evt))
      speech_recognizer.session_started.connect(lambda evt: format(evt))
      speech_recognizer.session_stopped.connect(lambda evt: format(evt))
      speech_recognizer.canceled.connect(lambda evt: format(evt))
      # stop continuous recognition on either session stopped or canceled events
      speech_recognizer.session_stopped.connect(stop_cb)
      speech_recognizer.canceled.connect(stop_cb)
  
      # Start continuous speech recognition
      speech_recognizer.start_continuous_recognition()
      while not done:
          time.sleep(.5)
  
      
      with open('Azure_Raw.txt','w') as f:
          f.write('\n'.join(results))
  
  sample_long_running_recognize(storage_uri)
  </code></pre>
  <p>Only other &quot;tutorial&quot; I found on subtitles was a Google Cloud one, and that gives the results I am looking for (Yes, I've tested it myself), but Azure obviously doesn't work at all like G-cloud: <a href="https://medium.com/searce/generate-srt-file-subtitles-using-google-clouds-speech-to-text-api-402b2f1da3bd" rel="nofollow noreferrer">https://medium.com/searce/generate-srt-file-subtitles-using-google-clouds-speech-to-text-api-402b2f1da3bd</a></p>
  <p>So basically: How do I get like 3 seconds of speech-text into an .srt format, like this:</p>
  <pre><code>1
  00:00:00,000 --&gt; 00:00:03,000
  This is the first sentence that
  
  2
  00:00:03,000 --&gt; 00:00:06,000
  continues after 3 seconds or so
  </code></pre>