-
Notifications
You must be signed in to change notification settings - Fork 0
/
readpdf.sh
executable file
·312 lines (262 loc) · 8.12 KB
/
readpdf.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#! /bin/bash
# readpdf - narrate a file with native osx tts utility
# written by Andrew Barlow
# https://github.com/dandrewbarlow
# https://a-barlow.com
# POSIX var that can apparently cause trouble if getopts is used multiple times in a row without resetting
OPTIND=1
# initialize empty file vars
input_file=""
output_file=false
convert=false
voice=''
# temporary file directory and files
temp_dir=""
temp_text=""
temp_audio=""
# Determine tts program
tts=""
if [ -x "$(command -v say)" ]
then
tts="say"
voice='Tom'
elif [ -x "$(command -v espeak)" ]
then
tts="espeak"
voice='english-us'
else
echo "Error: no supported text-to-speech program" && exit 1
fi
# function to clean up temp files
cleanup() {
# don't run if program exits early somewhere else
if [ ! -z "$temp_dir" ]
then
rm -rf "${temp_dir}" && \
echo "> temporary files deleted" || \
echo "Error: error deleting temporary files"
fi
}
# bind cleanup function to exit, incl. interrupt
trap cleanup EXIT
trap cleanup SIGINT
# if programs in the script aren't installed, don't run the script
requirements() {
if [ ! -x "$(command -v gs)" ] && [ ! -x "$(command -v pandoc)" ] && [ ! -x "$(command -v ffmpeg)" ] && [ ! -x "$(command -v $tts)"]
then
echo "Error: requirements not met"
echo "More info: https://github.com/dandrewbarlow/readpdf"
exit 1
fi
}
# in case I (or you) fuck up
usage() {
echo "readpdf- use OSX's native text-to-speech programs to read pdf text"
echo ""
echo "USAGE: ./readpdf.sh -i [input] -v [voice] -n [install] [-och]"
echo "options: -i -o -c -h"
echo "i (argument) input - specify input file"
echo "v (argument) voice - specify voice to use. Defaults to Tom."
echo " See list of english voices with '-v list', or use say -v '?' to get full list."
echo "n (argument) install - since i is already used, this runs a simple installation script by confirming with '-n install'"
echo "o (flag) output - if provided, outputs to input.aiff"
echo "c (flag) convert - convert to mp3 using ffmpeg"
echo ""
echo "for more information, visit https://github.com/dandrewbarlow/readpdf"
}
# check requirements before running
requirements
# options: -i [input] -v [voice] -n [install] -o -c -h
# i : input- argument, specify input file
# v : voice- specify voice, use -v list to list voices
# o : output- flag, not arg; create file with base filename, and write audio to it
# c : convert- flag, if present, convert to mp3
# h : help- display usage info
# n : install- i is already used, so this runs a simple installation script by confirming with '-n install'. Auto-creates alias in either
# parse options and arguments given using getopts
while getopts "ohci:v:n:" opt; do
case $opt in
h)
usage
exit 1
;;
i)
if [ ! -e "$OPTARG" ]
then
echo "Error: file \"${OPTARG}\" does not exist"
exit 1
else
input_file="$OPTARG"
fi
;;
o)
output_file=true
;;
c)
convert=true
;;
v)
# if argument is list, list the voices
if [ "$OPTARG" == "list" ]
then
if [ "$tts" == say ]
then
say -v '?' | grep en_US
elif [ "$tts" == espeak ]
then
espeak --voices | grep english
fi
exit 0
fi
# validate voice choice matches one of say's voices
if [ "$OPTARG" == "$(say -v '?' | grep -i ${OPTARG} | awk '{print $1}')" ]
then
voice="$OPTARG"
else
echo "Error: voice not found, defaulting to 'Tom'"
fi
;;
n)
if [ "$OPTARG" == "install" ]
then
# create a ~/.scripts dir if not present
[ ! -d "$HOME/.scripts/" ] && \
mkdir "$HOME/.scripts" && echo "$HOME/.scripts/ directory created"
# copy this script to ~/.scripts/
if [ -e "./readpdf.sh" ]
then
cp ./readpdf.sh "$HOME/.scripts/" && echo "Script copied to $HOME/.scripts/"
else
echo "Error: installation script must be run from same directory as script"
exit 1
fi
# check which shell the user is using
# bash & zsh are the only shells I rly know or use (and are both OSX defaults, depending on ur computer's age) so I'm only messing with them
# sh and fish users can figure it out themselves
if [[ "$SHELL" =~ "bash" ]]
then
# check if the user already has an alias for this script
if [ -z "$(cat $HOME/.bashrc | grep -i 'readpdf')" ]
then
# echo alias into shell config if not
echo "alias readpdf=~/.scripts/readpdf.sh" >> "$HOME/.bashrc" && \
echo "Bash alias 'readpdf' has been created to access"
echo "Restart terminal or re-source bash config to use"
fi
# same process as above, but for zsh
elif [[ "$SHELL" =~ "zsh" ]]
then
if [ -z "$(cat $HOME/.zshrc | grep -i 'readpdf')" ]
then
echo "alias readpdf=~/.scripts/readpdf.sh" >> "$HOME/.zshrc" && \
echo "Zsh alias 'readpdf' has been created to access"
echo "Restart terminal or re-source bash config to use"
fi
else
echo "Unknown shell, no alias created"
fi
exit 0
fi
echo "Error, confirm installation with '-n install'"
exit 1
;;
*)
usage
exit 1
;;
esac
done
# check if the input file is specified and extant
if [ "$input_file" != "" ] && [ -e "$input_file" ]
then
# extract the name of the file and extension
name="$(echo "$input_file" | cut -f 1 -d '.')"
extension="$(echo "$input_file" | awk -F. '{print $NF}')"
# create temporary files if they are needed inside temp directory
temp_dir="$(mktemp -d)"
temp_text="${temp_dir}/text.txt)"
touch "$temp_text"
# convert to txt
echo "> converting to txt"
if [ "$extension" == "pdf" ]
then
gs -q -dNOPROMPT -dBATCH -dNOPAUSE -sDEVICE=txtwrite -sOutputFile="$temp_text" "$input_file" ||
echo "Error: ghostscript conversion failed" || exit 1
elif [ "$extension" == "epub" ]
then
pandoc "$input_file" -f epub -t plain -o "$temp_text" ||
echo "Error: pandoc conversion failed" || exit 1
else
echo "Unsupported filetype, attempting implicit pandoc conversion"
# attempt conversion, on fail output error message and exit
pandoc ${input_file} -t plain -o ${temp_text} || \
echo "Error: pandoc implicit conversion failed" || exit 1
fi
# create an audio file
if [ "$output_file" = true ]
then
echo "> generating audio"
# narrate and then compress audio file
if [ "$convert" = true ]
then
# handle different tts programs
if [ "$tts" == "say" ]
then
# Create temp audio
touch "${temp_dir}/audio.aiff"
temp_audio="${temp_dir}/audio.aiff"
# check if temp audio actually exists
[ -z "$temp_audio" ] && \
echo "Error creating temporary file (audio)" && exit 1
say -v "$voice" -f "$temp_text" "--output-file=$temp_audio" || \
echo "Error: say returned an error" || exit 1
elif [ "$tts" == "espeak" ]
then
# Create temp audio
touch "${temp_dir}/audio.wav"
temp_audio="${temp_dir}/audio.wav"
# check if temp audio actually exists
[ -z "$temp_audio" ] && \
echo "Error creating temporary file (audio)" && exit 1
espeak -v "$voice" -f "$temp_text" -w "$temp_audio"
fi
# get the bitrate of output file and use that for equivalent conversion
bit="$(ffmpeg -i "${temp_audio}" 2>&1 | grep Audio | awk -F", " '{print $5}' | cut -d' ' -f1)"
echo "> converting audio"
ffmpeg -hide_banner -loglevel fatal -i "$temp_audio" -f mp3 -acodec libmp3lame -ab "$bit"k "./${name}.mp3" || \
echo "Error: ffmpeg mp3 conversion failed; Exiting program" || \
exit 1
# Narrate to uncompressed audio file
else
if [ "$tts" == "say" ]
then
say -v "$voice" -f "$temp_text" -o "./${name}.aiff" || \
echo "Error: say returned error code" || \
exit 1
elif [ "$tts" == "espeak" ]
then
espeak -v "$voice" -f "$temp_text" -w "./${name}.wav" || \
echo "Error: espeak returned error code" || \
exit 1
fi
fi
# narrate from command line
else
if [ "$tts" == "say" ]
then
echo "> starting narration"
say -v "$voice" -f "$temp_text" || \
echo "Error: say returned an error"|| \
exit 1
elif [ "$tts" == "espeak" ]
then
espeak -v "$voice" -f "$temp_text" || \
echo "Error: espeak returned error code" || \
exit 1
fi
fi
else
echo "Error: no input file specified"
echo ""
usage
fi