|
10 | 10 | import logging
|
11 | 11 | import subprocess
|
12 | 12 | import sys
|
13 |
| -import IPython |
14 | 13 |
|
15 | 14 |
|
16 | 15 | logging.basicConfig(level=logging.INFO, format='%(message)s')
|
|
20 | 19 |
|
21 | 20 | parser = argparse.ArgumentParser(description='Convert speech audio to text using Google Speech API')
|
22 | 21 | parser.add_argument('in_filename', help='Input filename (`-` for stdin)')
|
| 22 | +parser.add_argument('--out-file', type=argparse.FileType('w'), default='-', |
| 23 | + help='Output filename (defaults to stdout)') |
23 | 24 | parser.add_argument('--timing', action='store_true', help='Include timing info')
|
24 | 25 |
|
25 | 26 |
|
@@ -53,12 +54,12 @@ def get_transcripts(audio_data, include_timing_info=False):
|
53 | 54 | return client.recognize(config, audio)
|
54 | 55 |
|
55 | 56 |
|
56 |
| -def transcribe(in_filename, include_timing_info=False): |
| 57 | +def transcribe(in_filename, out_file=sys.stdout, include_timing_info=False): |
57 | 58 | audio_data = decode_audio(in_filename)
|
58 | 59 | response = get_transcripts(audio_data, include_timing_info)
|
59 |
| - print(MessageToJson(response, sort_keys=True)) |
| 60 | + out_file.write(MessageToJson(response, sort_keys=True).encode('utf-8')) |
60 | 61 |
|
61 | 62 |
|
62 | 63 | if __name__ == '__main__':
|
63 | 64 | args = parser.parse_args()
|
64 |
| - transcribe(args.in_filename, args.timing) |
| 65 | + transcribe(args.in_filename, args.out_file, args.timing) |
0 commit comments