Skip to content

Instantly share code, notes, and snippets.

@jooray
Forked from mmmaly/transcribe.sh
Last active July 25, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jooray/70c328968f7690a9489c1cbb7dce73e8 to your computer and use it in GitHub Desktop.
Save jooray/70c328968f7690a9489c1cbb7dce73e8 to your computer and use it in GitHub Desktop.
Call Google speech recognize
#!/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