mirror of
https://github.com/open-webui/open-webui
synced 2025-05-24 06:44:24 +00:00
Merge pull request #12603 from alpha-pet/fix-convert-ogg-container-openai-transcription
fix: Convert ogg to wav for OpenAI transcription endpoint
This commit is contained in:
commit
f0efee5de4
@ -68,8 +68,8 @@ from pydub import AudioSegment
|
|||||||
from pydub.utils import mediainfo
|
from pydub.utils import mediainfo
|
||||||
|
|
||||||
|
|
||||||
def is_mp4_audio(file_path):
|
def audio_needs_conversion(file_path):
|
||||||
"""Check if the given file is an MP4 audio file."""
|
"""Check if the given file needs to be converted to a different format."""
|
||||||
if not os.path.isfile(file_path):
|
if not os.path.isfile(file_path):
|
||||||
log.error(f"File not found: {file_path}")
|
log.error(f"File not found: {file_path}")
|
||||||
return False
|
return False
|
||||||
@ -80,13 +80,17 @@ def is_mp4_audio(file_path):
|
|||||||
and info.get("codec_type") == "audio"
|
and info.get("codec_type") == "audio"
|
||||||
and info.get("codec_tag_string") == "mp4a"
|
and info.get("codec_tag_string") == "mp4a"
|
||||||
):
|
):
|
||||||
return True
|
return "mp4"
|
||||||
return False
|
elif info.get("format_name") == "ogg":
|
||||||
|
return "ogg"
|
||||||
|
elif info.get("format_name") == "matroska,webm":
|
||||||
|
return "webm"
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def convert_mp4_to_wav(file_path, output_path):
|
def convert_audio_to_wav(file_path, output_path, conversion_type):
|
||||||
"""Convert MP4 audio file to WAV format."""
|
"""Convert MP4/OGG audio file to WAV format."""
|
||||||
audio = AudioSegment.from_file(file_path, format="mp4")
|
audio = AudioSegment.from_file(file_path, format=conversion_type)
|
||||||
audio.export(output_path, format="wav")
|
audio.export(output_path, format="wav")
|
||||||
log.info(f"Converted {file_path} to {output_path}")
|
log.info(f"Converted {file_path} to {output_path}")
|
||||||
|
|
||||||
@ -496,10 +500,15 @@ def transcribe(request: Request, file_path):
|
|||||||
log.debug(data)
|
log.debug(data)
|
||||||
return data
|
return data
|
||||||
elif request.app.state.config.STT_ENGINE == "openai":
|
elif request.app.state.config.STT_ENGINE == "openai":
|
||||||
if is_mp4_audio(file_path):
|
conversion_type = audio_needs_conversion(file_path)
|
||||||
os.rename(file_path, file_path.replace(".wav", ".mp4"))
|
if conversion_type:
|
||||||
# Convert MP4 audio file to WAV format
|
os.rename(file_path, file_path.replace(".wav", f".{conversion_type}"))
|
||||||
convert_mp4_to_wav(file_path.replace(".wav", ".mp4"), file_path)
|
# Convert unsupported audio file to WAV format
|
||||||
|
convert_audio_to_wav(
|
||||||
|
file_path.replace(".wav", f".{conversion_type}"),
|
||||||
|
file_path,
|
||||||
|
conversion_type,
|
||||||
|
)
|
||||||
|
|
||||||
r = None
|
r = None
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user