semiformal-net Paul Edwards ggerganov commited on
Commit
b3a9b29
·
unverified ·
1 Parent(s): ecd7237

livestream : fix losing words across audio chunk (#195)

Browse files

* 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]>

Files changed (1) hide show
  1. examples/livestream.sh +33 -13
examples/livestream.sh CHANGED
@@ -1,5 +1,5 @@
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
@@ -10,14 +10,15 @@
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
@@ -25,7 +26,7 @@ else
25
  fi
26
 
27
  if [ -n "$2" ]; then
28
- step_ms="$2"
29
  fi
30
 
31
  if [ -n "$3" ]; then
@@ -54,16 +55,35 @@ 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
 
1
  #!/bin/bash
2
+ set -eo pipefail
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
 
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
+ fmt=aac # the audio format extension of the stream (TODO: auto detect)
14
+ step_s=30
15
  model="base.en"
16
 
17
  if [ -z "$1" ]; then
18
+ echo "Usage: $0 stream_url [step_s] [model]"
19
  echo ""
20
  echo " Example:"
21
+ echo " $0 $url $step_s $model"
22
  echo ""
23
  echo "No url specified, using default: $url"
24
  else
 
26
  fi
27
 
28
  if [ -n "$2" ]; then
29
+ step_s="$2"
30
  fi
31
 
32
  if [ -n "$3" ]; then
 
55
 
56
  running=1
57
 
58
+ #trap "running=0" SIGINT SIGTERM
59
 
60
+ printf "[+] Transcribing stream with model '$model', step_s $step_s (press Ctrl+C to stop):\n\n"
61
 
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
72
  while [ $running -eq 1 ]; do
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
82
  fi
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))
89
  done