Skip to content

Instantly share code, notes, and snippets.

@rubasov
Created January 24, 2023 15:17
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 rubasov/2efb28a28df18c17cf3199369d71af59 to your computer and use it in GitHub Desktop.
Save rubasov/2efb28a28df18c17cf3199369d71af59 to your computer and use it in GitHub Desktop.
gtts-cli wrapper for ease of use in language learning
#! /bin/bash
# tts
# Take some words or short text in a given language,
# make Google's TTS pronounce it and play the pronunciation.
#
# usage
# tts LANG-CODE TEXT TO SPEECH
#
# supported languages
# gtts-cli --all
#
# dependencies
# python3 -m pip install gtts
# apt install mpv
#
# links
# https://gtts.readthedocs.io/en/latest/index.html
# non-caching variant
# gtts-cli --output - --lang "lang" "$*" | mpv -
cache_dir="$HOME/.cache/tts"
lang="$1"
shift
mkdir -p "$cache_dir/$lang" 2>/dev/null
#cname=$( echo -n "$*" | sha1sum | cut -d ' ' -f1 )
cname=$( echo -n "$*" | tr -s '[:space:][:punct:]/' '---' )
if [ ! -f "$cache_dir/$lang/$cname" ]
then
gtts-cli --lang "$lang" --output "$cache_dir/$lang/$cname" "$*" || exit 1
fi
mpv "$cache_dir/$lang/$cname"
find "$cache_dir" -type f -atime +7 -exec rm {} +
find "$cache_dir" -type d -empty -exec rmdir {} +
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment