v2thegreat's picture
Add .gitignore to exclude pycache files and compiled Python files
2278e25
Raw
History Blame Contribute Delete
1.73 kB
import cv2
import os
from pathlib import Path
def get_duration_and_frames(video_path: str) -> tuple[float, int] | tuple[None, None]:
"""Extract duration (seconds) and total frames from a video using OpenCV."""
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return None, None # Failed to open video
# Get frames per second (fps) and total frame count
fps = cap.get(cv2.CAP_PROP_FPS)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Calculate duration (avoid division by zero)
duration = total_frames / fps if fps > 0 else 0
cap.release()
return duration, total_frames
def _find_files(directory: str | Path) -> list[str]:
files = []
for root, _, filenames in os.walk(directory):
for filename in filenames:
files.append(os.path.join(root, filename))
return files
def get_file_list(path: Path) -> list[Path]:
all_files = _find_files(path)
all_files = map(Path, all_files)
all_files = filter(lambda x: x.suffix, all_files)
return list(all_files)
def get_extensions(file_list) -> set[Path]:
extensions = set(map(lambda x: x.suffix, file_list))
return extensions
def get_video_type_metadata(file_path):
user = file_path.parts[-2]
duration, num_frames = get_duration_and_frames(file_path)
extension = file_path.suffix
return {
"user": user,
"duration": duration,
"extension": extension,
"num_frames": num_frames,
"path": file_path,
}
def get_image_type_metadata(file_path):
user = file_path.parts[-2]
extension = file_path.suffix
return {
"user": user,
"extension": extension,
"path": file_path,
}