|
from fastapi import APIRouter, UploadFile, File |
|
from fastapi.responses import JSONResponse |
|
from services.extractor import extract_features |
|
from services.summarizer import get_scores, get_selected_indices, save_summary_video |
|
from uuid import uuid4 |
|
import time |
|
import os |
|
|
|
router = APIRouter() |
|
|
|
@router.post("/summarize") |
|
|
|
def summarize_video(video: UploadFile = File(...)): |
|
if not video.filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')): |
|
return JSONResponse(content={"error": "Unsupported file format"}, status_code=400) |
|
|
|
extension = video.filename.split('.')[-1].lower() |
|
id = str(time.time()).replace('.', '_') |
|
filename = f"{id}.{extension}" |
|
processed_filename = f"{id}_processed.{extension}" |
|
filepath = os.path.join(os.getcwd(), "static", filename) |
|
processed_filepath = os.path.join(os.getcwd(), "static", processed_filename) |
|
url = f"/static/{processed_filename}" |
|
|
|
print("\n-----------> Saving Video Locally ....") |
|
with open(filepath, "wb") as f: |
|
f.write(video.file.read()) |
|
|
|
print("\n-----------> Extracting Features ....") |
|
features, picks = extract_features(filepath) |
|
|
|
print("\n-----------> Getting Scores ....") |
|
scores = get_scores(features) |
|
|
|
print("\n-----------> Selecting Indices ....") |
|
selected = get_selected_indices(scores, picks) |
|
|
|
print("\n-----------> Saving Video ....") |
|
save_summary_video(filepath, selected, processed_filepath) |
|
|
|
print("\n-----------> Returning Response ....") |
|
return JSONResponse(content={ |
|
"message": "Summarization complete", |
|
"summary_video_url": url |
|
}) |