feat: add file existence checks to audio processing methods

This commit is contained in:
parent b9d6cde8fe
commit c7f3e093b4

View File

@ -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