-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
from pathlib import Path | ||
import tempfile | ||
from typing import Literal | ||
import ffmpeg | ||
|
||
AudioFormat = Literal["mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm"] | ||
|
||
|
||
def convert_ogg( | ||
input_file: str | Path | bytes, | ||
output_format: AudioFormat = "mp3", | ||
output_path: str | Path | None = None, | ||
) -> str: | ||
""" | ||
Convert OGG audio file to another format using ffmpeg. | ||
Args: | ||
input_file: Path to input OGG file or bytes content | ||
output_format: Desired output format | ||
output_path: Optional output path. If None, uses a temporary file | ||
Returns: | ||
str: Path to the converted audio file | ||
""" | ||
try: | ||
# Handle bytes input by writing to temp file first | ||
if isinstance(input_file, bytes): | ||
with tempfile.NamedTemporaryFile(suffix=".ogg", delete=False) as temp_ogg: | ||
temp_ogg.write(input_file) | ||
input_file = temp_ogg.name | ||
|
||
# If no output path specified, create temp file | ||
if output_path is None: | ||
temp_dir = tempfile.gettempdir() | ||
output_path = os.path.join(temp_dir, f"converted_audio.{output_format}") | ||
|
||
# Ensure output directory exists | ||
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True) | ||
|
||
# Convert audio using ffmpeg | ||
stream = ffmpeg.input(str(input_file)) | ||
stream = ffmpeg.output(stream, str(output_path)) | ||
ffmpeg.run( | ||
stream, overwrite_output=True, capture_stdout=True, capture_stderr=True | ||
) | ||
|
||
# Clean up temp input file if we created one | ||
if isinstance(input_file, str) and input_file.startswith(tempfile.gettempdir()): | ||
os.unlink(input_file) | ||
|
||
return str(output_path) | ||
|
||
except ffmpeg.Error as e: | ||
raise RuntimeError(f"FFmpeg error: {e.stderr.decode()}") from e | ||
except Exception as e: | ||
raise RuntimeError(f"Error converting audio: {str(e)}") from e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
from groq import Groq | ||
from loguru import logger | ||
|
||
|
||
def transcribe_audio(audio_path: str, language: str = "en") -> str: | ||
""" | ||
Transcribe audio file using Groq's Whisper API. | ||
Args: | ||
audio_path: Path to the audio file | ||
language: Language code (default: "en") | ||
Returns: | ||
str: Transcribed text | ||
""" | ||
try: | ||
client = Groq() | ||
|
||
with open(audio_path, "rb") as file: | ||
transcription = client.audio.transcriptions.create( | ||
file=(audio_path, file.read()), | ||
model="whisper-large-v3-turbo", | ||
response_format="json", | ||
# language=language, | ||
temperature=0.0, | ||
) | ||
|
||
logger.info(f"Transcription result: {transcription.text}") | ||
return transcription.text | ||
|
||
except Exception as e: | ||
logger.error(f"Error transcribing audio: {str(e)}") | ||
raise RuntimeError(f"Transcription failed: {str(e)}") from e |