-
Notifications
You must be signed in to change notification settings - Fork 923
Add examples: split_silence
+ transcribe
#52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f818cff
Add `compile` operator
kkroening 4311e33
Add `split_silence` example
kkroening ad58a38
Finalize split_silence
kkroening f5f7ee2
Improve logging in split_silence; add transcribe example
kkroening 3a818cc
Update requirements.txt
kkroening File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improve logging in split_silence; add transcribe example
- Loading branch information
commit f5f7ee20730f2beca5db80933f11d880e1f22a0c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python | ||
from __future__ import unicode_literals | ||
|
||
from google.cloud import speech | ||
from google.cloud.speech import enums | ||
from google.cloud.speech import types | ||
import argparse | ||
import ffmpeg | ||
import logging | ||
import subprocess | ||
import sys | ||
|
||
|
||
logging.basicConfig(level=logging.INFO, format='%(message)s') | ||
logger = logging.getLogger(__file__) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
parser = argparse.ArgumentParser(description='Convert speech audio to text using Google Speech API') | ||
parser.add_argument('in_filename', help='Input filename (`-` for stdin)') | ||
|
||
|
||
def decode_audio(in_filename, **input_kwargs): | ||
p = subprocess.Popen( | ||
(ffmpeg | ||
.input(in_filename, **input_kwargs) | ||
.output('-', format='s16le', acodec='pcm_s16le', ac=1, ar='16k') | ||
.overwrite_output() | ||
.compile() | ||
), | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE | ||
) | ||
out = p.communicate() | ||
if p.returncode != 0: | ||
sys.stderr.write(out[1]) | ||
sys.exit(1) | ||
return out[0] | ||
|
||
|
||
def get_transcripts(audio_data): | ||
client = speech.SpeechClient() | ||
audio = types.RecognitionAudio(content=audio_data) | ||
config = types.RecognitionConfig( | ||
encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, | ||
sample_rate_hertz=16000, | ||
language_code='en-US' | ||
) | ||
response = client.recognize(config, audio) | ||
return [result.alternatives[0].transcript for result in response.results] | ||
|
||
|
||
def transcribe(in_filename): | ||
audio_data = decode_audio(in_filename) | ||
transcripts = get_transcripts(audio_data) | ||
for transcript in transcripts: | ||
print(repr(transcript.encode('utf-8'))) | ||
|
||
|
||
if __name__ == '_ 3D27 _main__': | ||
args = parser.parse_args() | ||
transcribe(args.in_filename) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.