Download behavior_backend/app/utils/file_utils.py from urjob/test: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/urjob/test/resolve/main/behavior_backend/app/utils/file_utils.py
- Command line
-
hf download hf://spaces/urjob/test/behavior_backend/app/utils/file_utils.py
-
curl -L -o file_utils.py https://huggingface.co/spaces/urjob/test/resolve/main/behavior_backend/app/utils/file_utils.py
1.28 kB
| 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 |