-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchGenerateSpeechFromText.sh
executable file
·102 lines (88 loc) · 3.06 KB
/
BatchGenerateSpeechFromText.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/zsh
#================================================================================
# AUTHOR
# Clint Box
# https://www.youtube.com/bearcatjamboree
#
# FUNCTION
# Generate speech from text using subtitle file
#
# DETAILS
# This script will scan recursively for .SRT files and generate a .wav
# file in the same directory, containing the .SRT content converted to
# speech. If a .wav file already exist then the script will skip the file
# and go on to the next .SRT file.
#
# USAGE
# ${SCRIPT_NAME} "srt_path"
#
# NOTE
# change the voices array to the language codes and voices you will be using
#================================================================================
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
echo "${machine}"
input_folder="$1"
declare -A voices=(
["ar"]="maged"
["en"]="alex"
["hi"]="neel"
["id"]="damayanti"
["zh-Hans"]="tingting"
["es"]="carlos"
["de"]="markus"
)
# Sort languages to alphabetical order
joined=$(printf ", \"%s\"" $(echo "${(@k)voices}" | tr " " "\n" | sort | tr "\n" " "))
echo "${joined:2}"
##############################################################################
# Prompt for input folder
###############################################################################
if ! [ -d "$input_folder" ]; then
if [[ "$machine" == "Mac" ]]; then
input_folder=$(osascript -e 'tell application (path to frontmost application as text)
set input_folder to choose folder with prompt "Please choose an input folder"
POSIX path of input_folder
end')
elif [[ "$machine" == "Linux" ]]; then
input_folder=$(dialog --title "Choose a folder" --stdout --title "Please choose an input folder" --fselect ~/ 14 48)
elif [[ "$machine" == "Cygwin" ]]; then
input_folder=$(dialog --title "Choose a folder" --stdout --title "Please choose an input folder" --fselect ~/ 14 48)
else
echo "Usage: $0 input_file input_folder format"
exit 1
fi
fi
if ! [ -d "$input_folder" ]; then
echo "Usage: $0 input_folder"
exit 1
fi
# Remove trailing slash for path
input_folder="${input_folder%/}"
###################################
# Process each input folder file
###################################
IFS=$'\n'
for filename in $(find $input_folder -name '*.srt'); do
file="${filename##*/}"
file="${file%.*}"
language="${filename##*_}"
language="${language%.*}"
# Construct output file name
output_file="${filename%.*}.wav"
voice=${voices[$language]}
if ! [ -f "$output_file" ]; then
echo python GenerateSpeechFromText.py --input_file "$filename" --output_file "$output_file" --voice "$voice"
python GenerateSpeechFromText.py --input_file "$filename" --output_file "$output_file" --voice "$voice"
# Kill to prevent hangs from mullitple calls
killall com.apple.speech.speechsynthesisd
else
echo "Skipping $output_file"
fi
done