Download src/utils.ts from omarod2in/vi-2: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/spaces/omarod2in/vi-2/resolve/main/src/utils.ts
- Command line
-
hf download hf://spaces/omarod2in/vi-2/src/utils.ts
-
curl -L -o utils.ts https://huggingface.co/spaces/omarod2in/vi-2/resolve/main/src/utils.ts
5.11 kB
| // βββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export function fmt(bytes: number): string { | |
| if (bytes < 1024) return `${bytes} B`; | |
| if (bytes < 1024 ** 2) return `${(bytes / 1024).toFixed(1)} KB`; | |
| if (bytes < 1024 ** 3) return `${(bytes / 1024 ** 2).toFixed(2)} MB`; | |
| return `${(bytes / 1024 ** 3).toFixed(2)} GB`; | |
| } | |
| export function fmtEta(s: number): string { | |
| if (!Number.isFinite(s) || s <= 0) return "0s"; | |
| const h = Math.floor(s / 3600); | |
| const m = Math.floor((s % 3600) / 60); | |
| const sec = Math.floor(s % 60); | |
| return [h && `${h}h`, m && `${m}m`, `${sec}s`].filter(Boolean).join(" "); | |
| } | |
| export function secondsToHms(total: number): string { | |
| const h = Math.floor(total / 3600); | |
| const m = Math.floor((total % 3600) / 60); | |
| const s = Math.floor(total % 60); | |
| return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`; | |
| } | |
| export function hmsToSeconds(hms: string): number { | |
| const parts = hms.split(":").map(Number); | |
| if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2]; | |
| if (parts.length === 2) return parts[0] * 60 + parts[1]; | |
| return parts[0] || 0; | |
| } | |
| export function pad2(v: number): string { | |
| return String(Math.max(0, v)).padStart(2, "0"); | |
| } | |
| export function toHmsString(h: number, m: number, s: number): string { | |
| return `${pad2(h)}:${pad2(m)}:${pad2(s)}`; | |
| } | |
| export function clampParts(h: number, m: number, s: number, maxSeconds?: number): { h: number; m: number; s: number } { | |
| let hh = Math.max(0, h); | |
| let mm = Math.min(59, Math.max(0, m)); | |
| let ss = Math.min(59, Math.max(0, s)); | |
| if (maxSeconds !== undefined) { | |
| const total = Math.max(0, hh * 3600 + mm * 60 + ss); | |
| if (total > maxSeconds) { | |
| const clamped = Math.max(0, Math.floor(maxSeconds)); | |
| hh = Math.floor(clamped / 3600); | |
| mm = Math.floor((clamped % 3600) / 60); | |
| ss = Math.floor(clamped % 60); | |
| } | |
| } | |
| return { h: hh, m: mm, s: ss }; | |
| } | |
| export function parseHmsParts(value: string): { h: number; m: number; s: number } { | |
| const parts = value.split(":").map((p) => parseInt(p, 10)); | |
| if (parts.length === 3) return { h: parts[0] || 0, m: parts[1] || 0, s: parts[2] || 0 }; | |
| if (parts.length === 2) return { h: 0, m: parts[0] || 0, s: parts[1] || 0 }; | |
| if (parts.length === 1 && parts[0]) return { h: 0, m: 0, s: parts[0] || 0 }; | |
| return { h: 0, m: 0, s: 0 }; | |
| } | |
| export function parseLooseTimeToSeconds(input: string): number | null { | |
| const value = input.trim(); | |
| if (!value) return null; | |
| const parts = value.split(":").map((p) => p.trim()); | |
| if (parts.some((p) => p === "" || Number.isNaN(Number(p)))) return null; | |
| if (parts.length === 2) { | |
| const [mm, ss] = parts.map(Number); | |
| if (mm < 0 || ss < 0 || ss >= 60) return null; | |
| return mm * 60 + ss; | |
| } | |
| if (parts.length === 3) { | |
| const [hh, mm, ss] = parts.map(Number); | |
| if (hh < 0 || mm < 0 || ss < 0 || mm >= 60 || ss >= 60) return null; | |
| return hh * 3600 + mm * 60 + ss; | |
| } | |
| return null; | |
| } | |
| export function parsePastedSegments(text: string): { start: string; end: string }[] { | |
| const lines = text | |
| .split(/\r?\n/) | |
| .map((line) => line.trim()) | |
| .filter(Boolean); | |
| const parsed: { start: string; end: string }[] = []; | |
| for (const line of lines) { | |
| const match = line.match(/(\d{1,2}:\d{2}(?::\d{2})?)\s*-\s*(\d{1,2}:\d{2}(?::\d{2})?)/); | |
| if (!match) continue; | |
| const startSec = parseLooseTimeToSeconds(match[1]); | |
| const endSec = parseLooseTimeToSeconds(match[2]); | |
| if (startSec == null || endSec == null || endSec <= startSec) continue; | |
| parsed.push({ | |
| start: secondsToHms(startSec), | |
| end: secondsToHms(endSec), | |
| }); | |
| } | |
| return parsed; | |
| } | |
| // βββ File Type Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export const MEDIA_EXTS = new Set([ | |
| ".mp4",".mkv",".avi",".mov",".webm",".ts",".m2ts",".flv",".wmv",".mpg",".mpeg", | |
| ".mp3",".aac",".opus",".flac",".wav",".m4a",".ogg",".wma",".m4v",".3gp",".vob", | |
| ]); | |
| export const ARCHIVE_EXTS = new Set([ | |
| ".zip",".tar",".gz",".tgz",".bz2",".xz",".rar",".7z", | |
| ]); | |
| export const SUBTITLE_EXTS = new Set([ | |
| ".srt", ".ass", ".ssa", ".vtt", ".sub", | |
| ]); | |
| export function isMediaFile(name: string): boolean { | |
| const lower = name.toLowerCase(); | |
| for (const ext of MEDIA_EXTS) { | |
| if (lower.endsWith(ext)) return true; | |
| } | |
| return false; | |
| } | |
| export function isSubtitleFile(name: string): boolean { | |
| const lower = name.toLowerCase(); | |
| for (const ext of SUBTITLE_EXTS) { | |
| if (lower.endsWith(ext)) return true; | |
| } | |
| return false; | |
| } | |
| export function isArchiveFile(name: string): boolean { | |
| const lower = name.toLowerCase(); | |
| if (lower.endsWith(".tar.gz") || lower.endsWith(".tar.xz") || lower.endsWith(".tar.bz2")) return true; | |
| for (const ext of ARCHIVE_EXTS) { | |
| if (lower.endsWith(ext)) return true; | |
| } | |
| return false; | |
| } |