Skip to content

Commit 7a8df75

Browse files
semiformal-netPaul Edwardsggerganov
authored
livestream : fix losing words across audio chunk (ggml-org#195)
* improve livestream script * Update examples/livestream.sh Co-authored-by: Georgi Gerganov <[email protected]> Co-authored-by: Paul Edwards <[email protected]> Co-authored-by: Georgi Gerganov <[email protected]>
1 parent e7a4b73 commit 7a8df75

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

examples/livestream.sh

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
2+
set -eo pipefail
33
# Transcribe audio livestream by feeding ffmpeg output to whisper.cpp at regular intervals
44
# Idea by @semiformal-net
55
# ref: https://github.com/ggerganov/whisper.cpp/issues/185
@@ -10,22 +10,23 @@
1010
#
1111

1212
url="http://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/nonuk/sbr_low/ak/bbc_world_service.m3u8"
13-
step_ms=10000
13+
fmt=aac # the audio format extension of the stream (TODO: auto detect)
14+
step_s=30
1415
model="base.en"
1516

1617
if [ -z "$1" ]; then
17-
echo "Usage: $0 stream_url [step_ms] [model]"
18+
echo "Usage: $0 stream_url [step_s] [model]"
1819
echo ""
1920
echo " Example:"
20-
echo " $0 $url $step_ms $model"
21+
echo " $0 $url $step_s $model"
2122
echo ""
2223
echo "No url specified, using default: $url"
2324
else
2425
url="$1"
2526
fi
2627

2728
if [ -n "$2" ]; then
28-
step_ms="$2"
29+
step_s="$2"
2930
fi
3031

3132
if [ -n "$3" ]; then
@@ -54,16 +55,35 @@ fi
5455

5556
running=1
5657

57-
trap "running=0" SIGINT SIGTERM
58+
#trap "running=0" SIGINT SIGTERM
5859

59-
printf "[+] Transcribing stream with model '$model', step_ms $step_ms (press Ctrl+C to stop):\n\n"
60+
printf "[+] Transcribing stream with model '$model', step_s $step_s (press Ctrl+C to stop):\n\n"
6061

62+
# continuous stream in native fmt (this file will grow forever!)
63+
ffmpeg -loglevel quiet -y -re -probesize 32 -i $url -c copy /tmp/whisper-live0.${fmt} &
64+
if [ $? -ne 0 ]; then
65+
printf "Error: ffmpeg failed to capture audio stream\n"
66+
exit 1
67+
fi
68+
printf "Buffering audio. Please wait...\n"
69+
# For some reason, the initial buffer can end up smaller than step_s (even though we sleep for step_s)
70+
sleep $(($step_s*2))
71+
i=0
6172
while [ $running -eq 1 ]; do
62-
ffmpeg -y -re -probesize 32 -i $url -ar 16000 -ac 1 -c:a pcm_s16le -t ${step_ms}ms /tmp/whisper-live0.wav > /dev/null 2> /tmp/whisper-live.err
63-
if [ $? -ne 0 ]; then
64-
printf "Error: ffmpeg failed to capture audio stream\n"
65-
exit 1
73+
# a handy bash built-in, SECONDS,
74+
# > "This variable expands to the number of seconds since the shell was started. Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned
75+
# > plus the number of seconds since the assignment."
76+
SECONDS=0
77+
# extract the next piece from the main file above and transcode to wav. -ss sets start time and nudges it by -0.5s to catch missing words (??)
78+
if [ $i -gt 0 ]; then
79+
ffmpeg -loglevel quiet -noaccurate_seek -i /tmp/whisper-live0.${fmt} -y -ar 16000 -ac 1 -c:a pcm_s16le -ss $(($i*$step_s-1)).5 -t $step_s /tmp/whisper-live.wav
80+
else
81+
ffmpeg -loglevel quiet -noaccurate_seek -i /tmp/whisper-live0.${fmt} -y -ar 16000 -ac 1 -c:a pcm_s16le -ss $(($i*$step_s)) -t $step_s /tmp/whisper-live.wav
6682
fi
67-
mv /tmp/whisper-live0.wav /tmp/whisper-live.wav
68-
./main -t 8 -m ./models/ggml-small.en.bin -f /tmp/whisper-live.wav --no-timestamps -otxt 2> /tmp/whispererr | tail -n 1 &
83+
./main -t 8 -m ./models/ggml-base.en.bin -f /tmp/whisper-live.wav --no-timestamps -otxt 2> /tmp/whispererr | tail -n 1
84+
echo
85+
while [ $SECONDS -lt $step_s ]; do
86+
sleep 1
87+
done
88+
((i=i+1))
6989
done

0 commit comments

Comments
 (0)