Browse Source

fail on ffmpeg errors

subDesTagesMitExtraKaese 2 years ago
parent
commit
f3a6a33a23
1 changed files with 4 additions and 1 deletions
  1. 4 1
      speech_recognition.py

+ 4 - 1
speech_recognition.py

@@ -13,12 +13,15 @@ def convert_audio(data: bytes, out_filename: str):
       file.flush()
       file.flush()
       print(f"Converting media {file.name} to {out_filename}")
       print(f"Converting media {file.name} to {out_filename}")
 
 
-      out, _ = (
+      out, err = (
         ffmpeg.input(file.name, threads=0)
         ffmpeg.input(file.name, threads=0)
         .output(out_filename, format="wav", acodec="pcm_s16le", ac=1, ar=SAMPLE_RATE)
         .output(out_filename, format="wav", acodec="pcm_s16le", ac=1, ar=SAMPLE_RATE)
         .overwrite_output()
         .overwrite_output()
         .run(cmd="ffmpeg", capture_stdout=True, capture_stderr=True, input=data)
         .run(cmd="ffmpeg", capture_stdout=True, capture_stderr=True, input=data)
       )
       )
+    if os.path.getsize(out_filename) == 0:
+      print(str(err, "utf-8"))
+      raise Exception("Converted file is empty")
   except ffmpeg.Error as e:
   except ffmpeg.Error as e:
     raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
     raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e