aivideodubbingapi-r08gi1/app/services/transcription_service.py
Automated Action 92e4d992b2 Implement complete AI video dubbing backend with FastAPI
Features:
- JWT authentication with user registration and login
- Video upload to Amazon S3 with file validation (200MB limit)
- Audio transcription using OpenAI Whisper API
- Text translation using GPT-4 API
- Voice cloning and audio synthesis using ElevenLabs API
- Video processing with ffmpeg for audio replacement
- Complete SQLite database with proper models and migrations
- Background task processing for long-running operations
- Health endpoint and comprehensive API documentation

Tech stack:
- FastAPI with SQLAlchemy ORM
- SQLite database with Alembic migrations
- Amazon S3 for file storage
- OpenAI APIs for transcription and translation
- ElevenLabs API for voice cloning
- ffmpeg for video processing
- JWT authentication with bcrypt password hashing
2025-06-24 17:56:12 +00:00

60 lines
1.8 KiB
Python

import os
import openai
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")
openai.api_key = OPENAI_API_KEY
async def transcribe_video_audio(video_s3_url: str) -> Optional[str]:
if not OPENAI_API_KEY:
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 = openai.Audio.transcribe(
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