import os from openai import AsyncOpenAI from typing import Optional import logging import tempfile from app.services.s3_service import download_file_from_s3 logger = logging.getLogger(__name__) OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") if not OPENAI_API_KEY: logger.warning("OpenAI API key not configured") client = AsyncOpenAI(api_key=OPENAI_API_KEY) if OPENAI_API_KEY else None async def transcribe_video_audio(video_s3_url: str) -> Optional[str]: if not client: logger.error("OpenAI API key not configured") return None try: # Extract filename from S3 URL file_name = video_s3_url.split('/')[-1] # Download video from S3 video_content = await download_file_from_s3(file_name) if not video_content: logger.error("Failed to download video from S3") return None # Create temporary file for the video with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file: temp_file.write(video_content) temp_file_path = temp_file.name try: # Transcribe using OpenAI Whisper with open(temp_file_path, 'rb') as audio_file: transcript = await client.audio.transcriptions.create( model="whisper-1", file=audio_file, response_format="text" ) # Clean up temporary file os.unlink(temp_file_path) return transcript except Exception as e: # Clean up temporary file on error if os.path.exists(temp_file_path): os.unlink(temp_file_path) raise e except Exception as e: logger.error(f"Error transcribing video: {e}") return None