fix(transcription): read the frame count once to keep the type gate flat

This commit is contained in:
devin-ai-integration[bot] 2026-11-10 05:50:33 +00:00
parent b6d25f2752
commit 54dcd9230f
2 changed files with 6 additions and 4 deletions

View file

@ -343,9 +343,10 @@ def calculate_request_duration(file: FileTypes) -> float | None:
# Extract duration using soundfile # Extract duration using soundfile
file_object: Final = io.BytesIO(file_content) file_object: Final = io.BytesIO(file_content)
with sf.SoundFile(file_object) as audio: with sf.SoundFile(file_object) as audio:
if len(audio) >= LIBSNDFILE_UNKNOWN_FRAME_COUNT: frames: Final = len(audio)
if frames >= LIBSNDFILE_UNKNOWN_FRAME_COUNT:
return None return None
duration: Final = len(audio) / audio.samplerate duration: Final = frames / audio.samplerate
return duration return duration
except Exception: except Exception:

View file

@ -524,9 +524,10 @@ async def write_media_within_limit(response: httpx.Response, media_file: IO[byte
def media_file_seconds(path: Path) -> float | None: def media_file_seconds(path: Path) -> float | None:
try: try:
with soundfile.SoundFile(str(path)) as audio: with soundfile.SoundFile(str(path)) as audio:
if len(audio) >= LIBSNDFILE_UNKNOWN_FRAME_COUNT: frames: Final = len(audio)
if frames >= LIBSNDFILE_UNKNOWN_FRAME_COUNT:
return None return None
return len(audio) / audio.samplerate return frames / audio.samplerate
except (RuntimeError, ValueError, OSError) as e: except (RuntimeError, ValueError, OSError) as e:
verbose_proxy_logger.warning("Transcribe media could not be decoded for its duration: %s", e) verbose_proxy_logger.warning("Transcribe media could not be decoded for its duration: %s", e)
return None return None