ggerganov commited on
Commit
520d318
·
unverified ·
1 Parent(s): dec4507

livestream : handle ffmpeg errors gracefully and stabilize transcript

Browse files
Files changed (1) hide show
  1. examples/livestream.sh +25 -15
examples/livestream.sh CHANGED
@@ -53,36 +53,46 @@ if [[ ! " ${models[@]} " =~ " ${model} " ]]; then
53
  exit 1
54
  fi
55
 
 
 
 
 
56
  printf "[+] Transcribing stream with model '$model', step_s $step_s (press Ctrl+C to stop):\n\n"
57
 
58
  # continuous stream in native fmt (this file will grow forever!)
59
- ffmpeg -loglevel quiet -y -re -probesize 32 -i $url -c copy /tmp/whisper-live0.${fmt} &
60
  if [ $? -ne 0 ]; then
61
  printf "Error: ffmpeg failed to capture audio stream\n"
62
  exit 1
63
  fi
64
- printf "Buffering audio. Please wait...\n"
65
 
66
- # For some reason, the initial buffer can end up smaller than step_s (even though we sleep for step_s)
67
- sleep $(($step_s*2))
 
 
 
68
 
69
  i=0
70
- while [ true ]; do
71
- # a handy bash built-in, SECONDS,
72
- # > "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
73
- # > plus the number of seconds since the assignment."
74
- SECONDS=0
75
  # 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 (??)
76
- if [ $i -gt 0 ]; then
77
- 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
78
- else
79
- 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
80
- fi
 
 
 
 
81
 
82
  ./main -t 8 -m ./models/ggml-base.en.bin -f /tmp/whisper-live.wav --no-timestamps -otxt 2> /tmp/whispererr | tail -n 1
83
 
84
- while [ $SECONDS -lt $step_s ]; do
85
  sleep 1
86
  done
87
  ((i=i+1))
88
  done
 
 
 
 
53
  exit 1
54
  fi
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
 
69
+ printf "Buffering audio. Please wait...\n\n"
70
+ sleep $(($step_s))
71
+
72
+ # do not stop script on error
73
+ set +e
74
 
75
  i=0
76
+ SECONDS=0
77
+ while [ $running -eq 1 ]; do
 
 
 
78
  # 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 (??)
79
+ err=1
80
+ while [ $err -ne 0 ]; do
81
+ if [ $i -gt 0 ]; then
82
+ ffmpeg -loglevel quiet -v error -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 2> /tmp/whisper-live.err
83
+ else
84
+ ffmpeg -loglevel quiet -v error -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 2> /tmp/whisper-live.err
85
+ fi
86
+ err=$(cat /tmp/whisper-live.err | wc -l)
87
+ done
88
 
89
  ./main -t 8 -m ./models/ggml-base.en.bin -f /tmp/whisper-live.wav --no-timestamps -otxt 2> /tmp/whispererr | tail -n 1
90
 
91
+ while [ $SECONDS -lt $((($i+1)*$step_s)) ]; do
92
  sleep 1
93
  done
94
  ((i=i+1))
95
  done
96
+
97
+ killall -v ffmpeg
98
+ killall -v main