File size: 1,279 Bytes
8ae78b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)
    """
    # Generate unique ID for the video
    video_id = str(uuid.uuid4())
    file_extension = os.path.splitext(file.filename)[1] if file.filename else ""
    
    # Save the uploaded file
    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  # Default to 0 if video cannot be opened
    
    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