From 2a40f154af60ab67066ecca827add46ded59ce7a Mon Sep 17 00:00:00 2001 From: Alexander Metzger Date: Mon, 23 Oct 2023 14:47:11 -0700 Subject: [PATCH] better error message for unsupported audio formats uploaded via url --- daras_ai_v2/asr.py | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/daras_ai_v2/asr.py b/daras_ai_v2/asr.py index 588de82ba..6d416d327 100644 --- a/daras_ai_v2/asr.py +++ b/daras_ai_v2/asr.py @@ -539,20 +539,32 @@ def audio_url_to_wav(audio_url: str) -> tuple[str, int]: def audio_bytes_to_wav(audio_bytes: bytes) -> tuple[bytes | None, int]: - with tempfile.NamedTemporaryFile() as infile: - infile.write(audio_bytes) - infile.flush() - - if check_wav_audio_format(infile.name): - # already a wav file - return None, os.path.getsize(infile.name) - - with tempfile.NamedTemporaryFile(suffix=".wav") as outfile: - # convert audio to single channel wav - args = ["ffmpeg", "-y", "-i", infile.name, *FFMPEG_WAV_ARGS, outfile.name] - print("\t$ " + " ".join(args)) - subprocess.check_call(args) - return outfile.read(), os.path.getsize(outfile.name) + try: + with tempfile.NamedTemporaryFile() as infile: + infile.write(audio_bytes) + infile.flush() + + if check_wav_audio_format(infile.name): + # already a wav file + return None, os.path.getsize(infile.name) + + with tempfile.NamedTemporaryFile(suffix=".wav") as outfile: + # convert audio to single channel wav + args = [ + "ffmpeg", + "-y", + "-i", + infile.name, + *FFMPEG_WAV_ARGS, + outfile.name, + ] + print("\t$ " + " ".join(args)) + subprocess.check_call(args) + return outfile.read(), os.path.getsize(outfile.name) + except subprocess.CalledProcessError: + raise ValueError( + "Invalid audio file. Could not convert audio to wav format. Please confirm the file is not corrupted and has a supported format (google 'ffmpeg supported audio file types')" + ) def check_wav_audio_format(filename: str) -> bool: