Skip to content

Instantly share code, notes, and snippets.

@xavierfiechter
Forked from jooray/yt-whisper
Created March 18, 2023 22:52
Show Gist options
  • Save xavierfiechter/4ea89b8932f766e651cd04b3f1f9b179 to your computer and use it in GitHub Desktop.
Save xavierfiechter/4ea89b8932f766e651cd04b3f1f9b179 to your computer and use it in GitHub Desktop.
A script to download an audio from a video from a streaming platform such as youtube and transcribe it to text using whisper.cpp
#!/bin/bash
# Usage: yt-whisper URL [OUTPUT_FILENAME_TEMPLATE [LANGUAGE]]
# If OUTPUT_FILENAME_TEMPLATE is empty, output is yt-whisper-video
# If LANGAUGE is empty, it is set to "auto"
# General settings (paths) for whisper.cpp
# Note - this uses whisper.cpp, not official whisper. Get it at
# https://github.com/ggerganov/whisper.cpp
# You will have to adjust these
WHISPER_MODEL=/Users/test/whisper.cpp/models/ggml-large.bin
WHISPER_BIN=/Users/test/whisper.cpp/main
# Don't forget to install yt-dlp:
# brew install yt-dlp
YT_DOWNLOAD=yt-dlp
OUTPUT_BASENAME="yt-whisper-video"
if [ -n "${2}" ]; then OUTPUT_BASENAME="${2}"; fi
RECOGNITION_LANGUAGE="auto"
if [ -n "${3}" ]; then RECOGNITION_LANGUAGE="${3}"; fi
WAV_DOWNLOADED_FILENAME="${OUTPUT_BASENAME}-downloaded.wav"
WAV_RECODED_FILENAME="${OUTPUT_BASENAME}.wav"
if [ -e "${WAV_DOWNLOADED_FILENAME}" ]
then
echo "${WAV_DOWNLOADED_FILENAME} already exists"
exit 1
fi
if [ -e "${WAV_RECODED_FILENAME}" ]
then
echo "${WAV_RECODED_FILENAME} already exists"
exit 1
fi
${YT_DOWNLOAD} -x --audio-format wav -o "${WAV_DOWNLOADED_FILENAME}" "${1}"
if [ -not -f "${WAV_DOWNLOADED_FILENAME}" ]
then
echo "Download failed"
exit 1
fi
ffmpeg -i "${WAV_DOWNLOADED_FILENAME}" -ar 16000 -ac 1 -c:a pcm_s16le "${WAV_RECODED_FILENAME}" && rm -f "${WAV_DOWNLOADED_FILENAME}"
${WHISPER_BIN} -m ${WHISPER_MODEL} -f "${WAV_RECODED_FILENAME}" -otxt -ovtt -pp -l "${RECOGNITION_LANGUAGE}" && rm -f "${WAV_RECODED_FILENAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment