From c7f3e093b49f18fc83fabab29cc6f58ab0890162 Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Sat, 4 Jan 2025 16:42:31 +0400 Subject: [PATCH] feat: add file existence checks to audio processing methods --- src/utils/speech_recognition.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/utils/speech_recognition.py b/src/utils/speech_recognition.py index e5a4aae..a26796d 100644 --- a/src/utils/speech_recognition.py +++ b/src/utils/speech_recognition.py @@ -21,8 +21,14 @@ def convert_to_wav(file_path: str) -> str: str: The path to the converted or original WAV file. Raises: + FileNotFoundError: If the file does not exist. RuntimeError: If the conversion fails for any reason. """ + # Check if the file exists + if not os.path.exists(file_path): + logger.error(f"File {file_path} does not exist.") + raise FileNotFoundError(f"File {file_path} does not exist.") + if file_path.lower().endswith('.wav'): logger.info(f"File {file_path} is already in WAV format.") return file_path @@ -49,8 +55,14 @@ def get_audio_duration(file_path: str) -> float: float: The duration of the audio file in seconds. Raises: + FileNotFoundError: If the file does not exist. RuntimeError: If unable to get the file duration. """ + # Check if the file exists + if not os.path.exists(file_path): + logger.error(f"File {file_path} does not exist.") + raise FileNotFoundError(f"File {file_path} does not exist.") + try: logger.info(f"Getting duration of {file_path}.") audio = AudioSegment.from_file(file_path) @@ -73,6 +85,7 @@ def convert_voice_to_text(file_path: str, language='ru') -> str: str: The transcribed text if recognition is successful. Raises: + FileNotFoundError: If the file does not exist. RuntimeError: For any errors encountered during processing. """ # Check if the file exists