asi se fue xd
This commit is contained in:
@@ -26,35 +26,25 @@ except ImportError as e:
|
||||
def convert_audio_to_wav(input_data: bytes, input_format: str = "webm") -> bytes:
|
||||
"""
|
||||
Convert audio data to WAV format using ffmpeg.
|
||||
Whisper requires WAV/PCM format, but browsers typically record in WebM/Opus.
|
||||
Uses stdin/stdout pipes so ffmpeg probes the actual data format
|
||||
instead of relying on file extensions.
|
||||
"""
|
||||
# Create temp files for input and output
|
||||
with tempfile.NamedTemporaryFile(suffix=f".{input_format}", delete=False) as in_file:
|
||||
in_file.write(input_data)
|
||||
input_path = in_file.name
|
||||
|
||||
output_path = input_path.replace(f".{input_format}", ".wav")
|
||||
|
||||
try:
|
||||
# Use ffmpeg to convert to WAV (16kHz mono, which Whisper prefers)
|
||||
result = subprocess.run([
|
||||
"ffmpeg", "-y", # Overwrite output
|
||||
"-i", input_path, # Input file
|
||||
"-ar", "16000", # Sample rate 16kHz
|
||||
"-ac", "1", # Mono
|
||||
"ffmpeg", "-y",
|
||||
"-i", "pipe:0", # Read from stdin (auto-detect format)
|
||||
"-ar", "16000", # Sample rate 16kHz
|
||||
"-ac", "1", # Mono
|
||||
"-c:a", "pcm_s16le", # PCM 16-bit little-endian
|
||||
output_path
|
||||
], capture_output=True, text=True, timeout=30)
|
||||
"-f", "wav", # Output format
|
||||
"pipe:1" # Write to stdout
|
||||
], input=input_data, capture_output=True, timeout=30)
|
||||
|
||||
if result.returncode != 0:
|
||||
print(f"[Whisper] ffmpeg error: {result.stderr}")
|
||||
print(f"[Whisper] ffmpeg error: {result.stderr.decode('utf-8', errors='replace')}")
|
||||
return None
|
||||
|
||||
# Read the converted WAV file
|
||||
with open(output_path, "rb") as f:
|
||||
wav_data = f.read()
|
||||
|
||||
return wav_data
|
||||
return result.stdout
|
||||
|
||||
except subprocess.TimeoutExpired:
|
||||
print("[Whisper] ffmpeg conversion timed out")
|
||||
@@ -65,16 +55,6 @@ def convert_audio_to_wav(input_data: bytes, input_format: str = "webm") -> bytes
|
||||
except Exception as e:
|
||||
print(f"[Whisper] Conversion error: {e}")
|
||||
return None
|
||||
finally:
|
||||
# Cleanup temp files
|
||||
try:
|
||||
os.unlink(input_path)
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
os.unlink(output_path)
|
||||
except:
|
||||
pass
|
||||
|
||||
# Configuration
|
||||
HOST = "0.0.0.0" # Listen on all interfaces (needed for Traefik proxy)
|
||||
|
||||
Reference in New Issue
Block a user