Skip to content

Commit ceada8c

Browse files
committed
livestream.sh : simple tool to transcribe audio livestreams (ggml-org#185)
1 parent def7851 commit ceada8c

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

Diff for: examples/livestream.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
# Transcribe audio livestream by feeding ffmpeg output to whisper.cpp at regular intervals
4+
# Idea by @semiformal-net
5+
# ref: https://github.com/ggerganov/whisper.cpp/issues/185
6+
#
7+
# TODO:
8+
# - Currently, there is a gap between sequential chunks, so some of the words are dropped. Need to figure out a
9+
# way to produce a continuous stream of audio chunks.
10+
#
11+
12+
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
14+
model="base.en"
15+
16+
if [ -z "$1" ]; then
17+
echo "Usage: $0 stream_url [step_ms] [model]"
18+
echo ""
19+
echo " Example:"
20+
echo " $0 $url $step_ms $model"
21+
echo ""
22+
echo "No url specified, using default: $url"
23+
else
24+
url="$1"
25+
fi
26+
27+
if [ -n "$2" ]; then
28+
step_ms="$2"
29+
fi
30+
31+
if [ -n "$3" ]; then
32+
model="$3"
33+
fi
34+
35+
# Whisper models
36+
models=( "tiny.en" "tiny" "base.en" "base" "small.en" "small" "medium.en" "medium" "large" )
37+
38+
# list available models
39+
function list_models {
40+
printf "\n"
41+
printf " Available models:"
42+
for model in "${models[@]}"; do
43+
printf " $model"
44+
done
45+
printf "\n\n"
46+
}
47+
48+
if [[ ! " ${models[@]} " =~ " ${model} " ]]; then
49+
printf "Invalid model: $model\n"
50+
list_models
51+
52+
exit 1
53+
fi
54+
55+
running=1
56+
57+
trap "running=0" SIGINT SIGTERM
58+
59+
printf "[+] Transcribing stream with model '$model', step_ms $step_ms (press Ctrl+C to stop):\n\n"
60+
61+
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
66+
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 &
69+
done

Diff for: examples/stream.wasm/emscripten.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void stream_main(size_t index) {
5151

5252
wparams.language = "en";
5353

54-
printf("stream: using %d threads\n", N_THREAD);
54+
printf("stream: using %d threads\n", wparams.n_threads);
5555

5656
std::vector<float> pcmf32;
5757

Diff for: examples/talk.wasm/emscripten.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void talk_main(size_t index) {
6868

6969
g_gpt2 = gpt2_init("gpt-2.bin");
7070

71-
printf("talk: using %d threads\n", N_THREAD);
71+
printf("talk: using %d threads\n", wparams.n_threads);
7272

7373
std::vector<float> pcmf32;
7474

0 commit comments

Comments
 (0)