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

140 lines
4.0 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status, BackgroundTasks
from sqlalchemy.orm import Session
from pydantic import BaseModel
from app.db.session import get_db
from app.models.user import User
from app.models.video import Video
from app.models.transcription import Transcription
from app.utils.auth import get_current_user
from app.services.transcription_service import transcribe_video_audio
router = APIRouter()
class TranscriptionResponse(BaseModel):
id: int
video_id: int
text: str
created_at: str
class Config:
orm_mode = True
class TranscriptionStartResponse(BaseModel):
message: str
video_id: int
async def background_transcribe(video_id: int, video_s3_url: str, db: Session):
try:
# Update video status
video = db.query(Video).filter(Video.id == video_id).first()
if video:
video.status = "transcribing"
db.commit()
# Transcribe the video
transcript_text = await transcribe_video_audio(video_s3_url)
if transcript_text:
# Save transcription to database
transcription = Transcription(
video_id=video_id,
text=transcript_text
)
db.add(transcription)
# Update video status
if video:
video.status = "transcribed"
db.commit()
else:
# Update video status to error
if video:
video.status = "transcription_failed"
db.commit()
except Exception:
# Update video status to error
video = db.query(Video).filter(Video.id == video_id).first()
if video:
video.status = "transcription_failed"
db.commit()
@router.post("/{video_id}", response_model=TranscriptionStartResponse)
async def start_transcription(
video_id: int,
background_tasks: BackgroundTasks,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
# Check if video exists and belongs to user
video = db.query(Video).filter(
Video.id == video_id,
Video.user_id == current_user.id
).first()
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Check if transcription already exists
existing_transcription = db.query(Transcription).filter(
Transcription.video_id == video_id
).first()
if existing_transcription:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Transcription already exists for this video"
)
# Start background transcription
background_tasks.add_task(background_transcribe, video_id, video.original_s3_url, db)
return TranscriptionStartResponse(
message="Transcription started in background",
video_id=video_id
)
@router.get("/{video_id}", response_model=TranscriptionResponse)
async def get_transcription(
video_id: int,
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db)
):
# Check if video exists and belongs to user
video = db.query(Video).filter(
Video.id == video_id,
Video.user_id == current_user.id
).first()
if not video:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Video not found"
)
# Get transcription
transcription = db.query(Transcription).filter(
Transcription.video_id == video_id
).first()
if not transcription:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Transcription not found"
)
return TranscriptionResponse(
id=transcription.id,
video_id=transcription.video_id,
text=transcription.text,
created_at=str(transcription.created_at)
)