-
Notifications
You must be signed in to change notification settings - Fork 3
/
transcribe.sh
executable file
·337 lines (258 loc) · 6.59 KB
/
transcribe.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/bin/bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PB_PID=""
while getopts "c:" opt; do
case ${opt} in
c)
export CUDA_VISIBLE_DEVICES=${OPTARG}
echo "starting instance ${OPTARG}"
;;
\?)
echo "Usage: transcribe.sh [-c CUDA_VISIBLE_DEVICES]"
exit 1
;;
esac
done
RED='\033[0;31m'
NOCOLOR='\033[0m'
GREEN='\033[0;32m'
export LC_NUMERIC="en_US.UTF-8"
if [ -f ./fyyd.cfg ]
then
source "./fyyd.cfg"
if [ ${CUDA_VISIBLE_DEVICES} ]
then
PIDFILE=${PIDFILE}_${CUDA_VISIBLE_DEVICES}
fi
else
echo "no config found. please run setup.sh first."
exit 0
fi
if [ -z $ATOKEN ]
then
echo "Please get an accesstoken first. Visit https://fyyd.de/dev/app/ to create one."
exit 0;
fi
trap ctrl_c INT
trap stop EXIT
function ETASTRING {
TETA="$1"
if [ "$(uname -s)" == "Darwin" ]
then
echo -n `date -u -r ${TETA#-} +%T`
else
echo -n `date -d@${TETA} -u +%T`
fi
}
function stop {
rm -f $PIDFILE
if [ ! -z $PB_PID ]
then
kill $PB_PID
wait $PB_PID 2>/dev/null
fi
}
function ctrl_c() {
if [ ! -z $PB_PID ]
then
kill $PB_PID
wait $PB_PID 2>/dev/null
PB_PID=""
fi
echo "------------------------------"
echo "STOPPING... sending notification to fyyd"
echo "bye bye"
echo "------------------------------"
curl -H "Authorization: Bearer $ATOKEN" "https://api.fyyd.de/0.2/transcribe/error/$ID" -d "error=0"
rm -f $PIDFILE
exit
}
progress() {
if [ -z $PB_PID ]
then
while :
do
SECONDSGONE=$((SECONDSGONE+1))
LINE=$(tail -1 ./redir.txt)
if [ -z "$LINE" ]
then
# no output by whisper so far...
echo -n "."
LASTLINE=""
LASTRATE=0
sleep 1
continue
fi
if [ -z "$LASTLINE" ]
then
echo
fi
if [ "$LINE" = "$LASTLINE" ]
then
# no new output, just count down
ETA=$((ETA-1))
else
# it did smth! calculate new rate and estimate the remaining time :)
STAMP=$(echo ${LINE} | awk 'BEGIN { FS="[ ]" } ; { print $3 }')
IFS=: read -r h m s <<<"${STAMP:0:8}"
h=${h#0};m=${m#0};s=${s#0}
WHERE=$(((h * 60 + m) * 60 + s))
if [ $WHERE -eq 0 ]
then
THISRATE=1
else
THISRATE=$(echo "$WHERE/$SECONDSGONE" | bc -l)
fi
ETA=$(echo "$DURATION/$THISRATE - $SECONDSGONE" | bc -l)
fi
ETA=$(printf "%.0f" "$ETA")
if [ "$LINE" = "$LASTLINE" ]
then
printf "\rETA: "
else
printf "\r\033[KETA: "
fi
ETASTRING "$ETA"
if (( $(echo "$THISRATE > $LASTRATE" |bc -l) ))
then
COLOR=$GREEN
elif (( $(echo "$THISRATE < $LASTRATE" |bc -l) ))
then
COLOR=$RED
else
COLOR=$NOCOLOR
fi
printf " ($COLOR%.1fx$NOCOLOR)" $THISRATE
if [ "$LINE" = "$LASTLINE" ]
then
echo -n
else
echo -n " ${LINE:0:$(echo $(tput cols)-25|bc -l)}"
fi
LASTLINE="$LINE"
LASTRATE=$THISRATE
sleep 1
done & 2>/dev/null
PB_PID=$!
else
kill $PB_PID
wait $PB_PID 2>/dev/null
PB_PID=""
fi
}
pid() {
if [ -f $PIDFILE ]
then
PID=$(cat $PIDFILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Process already running"
exit 1
else
## Process not found assume not running
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
else
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
}
pid
echo "Checking for updates"
./update.sh
echo "Starting engines! Let's transcribe some episodes!"
cd whisper.cpp
printf '=%.0s' $(seq 1 $(tput cols))
echo ""
while :
do
#------------------------------------------------------------------------------------
# get data for one episode to transcribe from fyyd.de
#------------------------------------------------------------------------------------
ID=""
echo "getting data from fyyd"
DATA=`echo $(curl -s -H "Authorization: Bearer $ATOKEN" "https://api.fyyd.de/0.2/transcribe/next")`
eval "$(echo $DATA |jq -r '.data | to_entries | .[] | .key + "=" + (.value | @sh)')"
# exit if nothing to do
if [ -z $ID ]
then
echo "nothing to transcribe. exit!"
exit 0;
fi
#------------------------------------------------------------------------------------
# download episode
#------------------------------------------------------------------------------------
echo "starting download of episode $ID, \"$TITLE\", duration $DURATION seconds ($(ETASTRING $DURATION))"
curl -s -L "${URL}" > $TOKEN
if [ $? -ne 0 ]
then
echo "error downloading"
curl -H "Authorization: Bearer $ATOKEN" "https://api.fyyd.de/0.2/transcribe/error/$ID" -d "error=10"
continue
fi
#------------------------------------------------------------------------------------
# convert whatever was donwloaded to 16kHz WAV
#------------------------------------------------------------------------------------
echo "converting to wav"
ffmpeg -y -i $TOKEN -acodec pcm_s16le -ac 1 -ar 16000 $TOKEN.wav >/dev/null 2>/dev/null
if [ $? -eq 1 ]
then
echo "error converting to wav"
curl -H "Authorization: Bearer $ATOKEN" "https://api.fyyd.de/0.2/transcribe/error/$ID" -d "error=11"
continue
fi
rm $TOKEN
#------------------------------------------------------------------------------------
# transcribe wav to vtt
#------------------------------------------------------------------------------------
if [ ${LANG} == "null" ]
then
LANG="auto"
fi
echo -n "loading model"
# start process to display guessed remaining time
START=`date +%s`
SECONDSGONE=0
LASTLINE=""
progress
nice -n 18 ./main -m models/ggml-$MODEL.bin -t $THREADS -l $LANG -ovtt $TOKEN.wav >./redir.txt 2>/dev/null
if [ $? -ne 0 ]
then
echo "error transcribing"
curl -H "Authorization: Bearer $ATOKEN" "https://api.fyyd.de/0.2/transcribe/error/$ID" -d "error=12"
progress
echo
continue
fi
progress
END=`date +%s`
TOOK=$(echo "$END - $START" |bc -l)
echo
echo "finished transcription in $(ETASTRING $TOOK)"
#------------------------------------------------------------------------------------
# push transcript to fyyd
#------------------------------------------------------------------------------------
echo "sending transcript to fyyd"
curl -H "Authorization: Bearer $ATOKEN" "https://api.fyyd.de/0.2/transcribe/set/$ID" --data-binary @$TOKEN.wav.vtt
rm $TOKEN.wav
rm $TOKEN.wav.vtt
if [ -f ~/.fyyd-stop ]; then
rm ~/.fyyd-stop
echo "stopping hard"
exit
fi
printf '=%.0s' $(seq 1 $(tput cols))
echo ""
sleep 2
done
rm $PIDFILE