feat: add file existence checks to audio processing methods
This commit is contained in:
parent
b9d6cde8fe
commit
c7f3e093b4
@ -21,8 +21,14 @@ def convert_to_wav(file_path: str) -> str:
|
|||||||
str: The path to the converted or original WAV file.
|
str: The path to the converted or original WAV file.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
|
FileNotFoundError: If the file does not exist.
|
||||||
RuntimeError: If the conversion fails for any reason.
|
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'):
|
if file_path.lower().endswith('.wav'):
|
||||||
logger.info(f"File {file_path} is already in WAV format.")
|
logger.info(f"File {file_path} is already in WAV format.")
|
||||||
return file_path
|
return file_path
|
||||||
@ -49,8 +55,14 @@ def get_audio_duration(file_path: str) -> float:
|
|||||||
float: The duration of the audio file in seconds.
|
float: The duration of the audio file in seconds.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
|
FileNotFoundError: If the file does not exist.
|
||||||
RuntimeError: If unable to get the file duration.
|
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:
|
try:
|
||||||
logger.info(f"Getting duration of {file_path}.")
|
logger.info(f"Getting duration of {file_path}.")
|
||||||
audio = AudioSegment.from_file(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.
|
str: The transcribed text if recognition is successful.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
|
FileNotFoundError: If the file does not exist.
|
||||||
RuntimeError: For any errors encountered during processing.
|
RuntimeError: For any errors encountered during processing.
|
||||||
"""
|
"""
|
||||||
# Check if the file exists
|
# Check if the file exists
|
||||||
|
Loading…
Reference in New Issue
Block a user