alisaadhq's picture
Upload 19 files
d286c92 verified
Raw
History Blame Contribute Delete
1.07 kB
"""Shared utilities."""
import os
VIDEO_EXTENSIONS = {
".mp4", ".mkv", ".avi", ".mov", ".wmv", ".flv", ".webm",
".m4v", ".mpg", ".mpeg", ".ts", ".m2ts", ".3gp",
}
def fmt_size(n: int | float) -> str:
if n < 0:
return "—"
for unit in ("B", "KB", "MB", "GB", "TB"):
if n < 1024:
return f"{n:.1f} {unit}"
n /= 1024
return f"{n:.1f} PB"
def fmt_eta(seconds: float) -> str:
if seconds <= 0 or seconds != seconds:
return "—"
seconds = int(seconds)
h, r = divmod(seconds, 3600)
m, s = divmod(r, 60)
if h:
return f"{h}h {m}m"
if m:
return f"{m}m {s}s"
return f"{s}s"
def is_video(path: str) -> bool:
return os.path.splitext(path)[1].lower() in VIDEO_EXTENSIONS
def find_first_video(directory: str) -> str:
for root, _, files in os.walk(directory):
for f in files:
if is_video(f):
return os.path.join(root, f)
return ""
def ffmpeg_ok() -> bool:
import shutil
return shutil.which("ffmpeg") is not None