File size: 5,114 Bytes
7d751ef | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | // βββ 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;
} |