-
-
Save rubasov/2efb28a28df18c17cf3199369d71af59 to your computer and use it in GitHub Desktop.
gtts-cli wrapper for ease of use in language learning
This file contains hidden or 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 | |
# 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