Python Script to conver Speech/Audio to Text

This is the place for queries that don't fit in any of the other categories.

Python Script to conver Speech/Audio to Text

Postby lprocks99 » Mon Mar 31, 2014 12:40 am

I want to create a script that will take an audio file and transcribe it to text. I have searched for code that will help me do this. Majority of the time it errors or something is wrong in the coding or modules that I install and I am not quite sure how to fix the issue. If someone could create a script that is fluid I would truly appreciate it. I am currently using Python 2.7.6 on Windows 8.1. Below I inserted a code that I have found but none seem to work.

Code: Select all
 1 #!/usr/bin/env python2
 2 # -*- coding: utf-8 -*-
 4 import httplib
 5 import json
 6 import sys
 8 def speech_to_text(audio):
 9     url = ""
10     path = "/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en"
11     headers = { "Content-type": "audio/x-flac; rate=16000" };
12     params = {"xjerr": "1", "client": "chromium"}
13     conn = httplib.HTTPSConnection(url)
14     conn.request("POST", path, audio, headers)
15     response = conn.getresponse()
16     data =
17     jsdata = json.loads(data)
18     return jsdata["hypotheses"][0]["utterance"]
20 if __name__ == "__main__":
21     if len(sys.argv) != 2 or "--help" in sys.argv:
22         print "Usage: <flac-audio-file>"
23         sys.exit(-1)
24     else:
25         with open(sys.argv[1], "r") as f:
26             speech =
27         text = speech_to_text(speech)
28         print text
Last edited by stranac on Mon Mar 31, 2014 9:37 am, edited 1 time in total.
Reason: Removed color. First post lock.
Posts: 1
Joined: Mon Mar 31, 2014 12:06 am

Re: Python Script to conver Speech/Audio to Text

Postby stranac » Mon Mar 31, 2014 9:37 am

  1. Saying your code doesn't work without showing the error message is useless most of the time. Always post the entire error traceback, or explain how the result is different from what you expected
  2. Line numbers are not a part of the script, you should remove those
  3. Posting in red is annoying
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1595
Joined: Thu Feb 07, 2013 3:42 pm

Return to General Coding Help

Who is online

Users browsing this forum: buran and 3 guests