-
-
Save jooray/70c328968f7690a9489c1cbb7dce73e8 to your computer and use it in GitHub Desktop.
Call Google speech recognize
This file contains 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
#!/bin/bash | |
# How to set it up: | |
# Create a project in Google Cloud admin console | |
# Install google cloud sdk (you need gcloud and gsutil commands) | |
# Install ffmpeg and jq | |
# Run gcloud init, login, answer the questions | |
# Under the project you created, create a globally unique bucket | |
# here: https://console.cloud.google.com/storage/browser | |
# set the name of the globally unique bucket name prefixed by gs:// | |
# here | |
GSBUCKET=gs://my_project_transcribe | |
# Then run this command with a parameter with the file name of an | |
# audio or video file understood by ffmpeg | |
# Forked from: | |
# https://gist.github.com/mmmaly/e78fbc3d05cb232e38e6cee5cda6aa2d | |
if [[ $# -eq 0 ]] ; then | |
echo 'Usage: transcribe.sh FILENAME [language_code]' | |
echo 'And please read the source code, because you need to configure the bucket' | |
echo 'and install some tools' | |
exit 0 | |
fi | |
name=$1 | |
file=$name.flac | |
language=${2:-sk-SK} | |
echo "Transcoding to flac..." | |
ffmpeg -i $name -ar 16000 -ac 1 $file 2>/dev/null | |
echo "Copying to Google cloud, bucket ${GSBUCKET}..." | |
gsutil cp "$file" "${GSBUCKET}" | |
echo "Recognizing language ${language}..." | |
gcloud ml speech recognize-long-running "${GSBUCKET}"/"${file}" --sample-rate 16000 --encoding flac --language-code="${language}" --async > $$.id | |
id=`jq -r .name < $$.id ` | |
gcloud ml speech operations wait $id > $name.json | |
jq -r 'reduce .results[] as $item (""; . + $item.alternatives[].transcript + " ")' < ${name}.json | fmt | tee ${name}.txt | |
rm $$.id | |
rm $file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment