Skip to content

Instantly share code, notes, and snippets.

@korakot
Last active April 12, 2021 17:46
Show Gist options
  • Save korakot/bb96432d7d7933c27aea85c3d9d5a4f8 to your computer and use it in GitHub Desktop.
Save korakot/bb96432d7d7933c27aea85c3d9d5a4f8 to your computer and use it in GitHub Desktop.
Transcribe YouTube with Google Cloud Speech API (on Colab)
import json
from subprocess import check_output
from time import sleep
!pip -q install youtube-dl
from google.colab import auth
auth.authenticate_user()
!gcloud config set project kora-id
gcs = 'gs://co-lab/dhamma' # use your own project, gcs
# main function
def convert_youtube(vid):
download_audio(vid)
oid = transcribe(vid)
show_progress(oid)
save_result(oid, vid)
""" download, convert to flac, then upload """
def download_audio(vid):
!youtube-dl youtu.be/$vid --id --format m4a -q
!ffmpeg -loglevel panic -y -i {vid}.m4a -ac 1 -ar 16000 {vid}.flac
!gsutil -q cp {vid}.flac {gcs}/{vid}.flac # upload to transcribe
def transcribe(vid):
!gcloud ml speech recognize-long-running {gcs}/{vid}.flac \
--language-code th --sample-rate 16000 \
--include-word-time-offsets --async > {vid}.id
return json.load(open(vid+'.id'))['name']
def show_progress(oid):
cmd = 'gcloud ml speech operations describe '+oid
while True:
res = check_output(cmd.split()).decode()
data = json.loads(res)
pc = data['metadata'].get('progressPercent',0)
if pc==100: break
print('\r', pc, '% ', end='')
for _ in range(60):
print('.', end='')
sleep(1)
print('\r','100% finished')
def save_result(oid, vid):
!gcloud ml speech operations wait {oid} > {vid}.json
!gsutil -q rm {gcs}/{vid}.flac
#!rm {vid}.m4a {vid}.flac {vid}.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment