| import os |
| import shutil |
| import cv2 |
| from pathlib import Path |
| from fastapi import UploadFile |
| import uuid |
|
|
| from app.core.config import settings |
|
|
| def save_upload_file(file: UploadFile) -> tuple[str, Path]: |
| """ |
| Save an uploaded file to the upload directory. |
| |
| Args: |
| file: The uploaded file |
| |
| Returns: |
| Tuple of (video_id, file_path) |
| """ |
| |
| video_id = str(uuid.uuid4()) |
| file_extension = os.path.splitext(file.filename)[1] if file.filename else "" |
| |
| |
| upload_path = settings.UPLOAD_DIR / f"{video_id}{file_extension}" |
| with open(upload_path, "wb") as buffer: |
| shutil.copyfileobj(file.file, buffer) |
| |
| return video_id, upload_path |
|
|
| def get_video_duration(video_path: str) -> float: |
| """ |
| Extract video duration using OpenCV. |
| |
| Args: |
| video_path: Path to the video file |
| |
| Returns: |
| Duration of the video in seconds |
| """ |
| cap = cv2.VideoCapture(video_path) |
| if not cap.isOpened(): |
| return 0.0 |
| |
| fps = cap.get(cv2.CAP_PROP_FPS) |
| frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT) |
| cap.release() |
|
|
| return frame_count / fps if fps > 0 else 0.0 |