converter

This commit is contained in:
2025-10-30 01:58:28 +01:00
parent dd9d076bd2
commit 8dd4f6e731
23 changed files with 1142 additions and 1286 deletions

View File

@@ -1,73 +1,64 @@
import Client from "../Client";
export type FormatOption = {
format_id: string;
ext: string | null;
vcodec: string | null;
acodec: string | null;
fps: number | null;
tbr: number | null;
abr: number | null;
vbr: number | null;
asr: number | null;
filesize: number | null;
filesize_approx: number | null;
estimated_size_bytes: number | null;
size_ok: boolean;
format_note: string | null;
resolution: string | null; // e.g. "1920x1080"
audio_only: boolean;
};
// Available output containers (must match backend)
export const FORMAT_EXTS = ["mp4", "mkv", "webm", "flv", "mov", "avi", "ogg"] as const;
export type FormatExt = (typeof FORMAT_EXTS)[number];
export type FormatsResponse = {
export type InfoResponse = {
title: string | null;
duration: number | null;
extractor: string | null;
video_id: string | null;
max_size_bytes: number;
options: FormatOption[];
thumbnail: string | null;
video_resolutions: string[]; // e.g. ["2160p", "1440p", "1080p", ...]
audio_resolutions: string[]; // e.g. ["320kbps", "160kbps", ...]
};
// Probe available formats for a URL (no auth required)
export async function probeFormats(url: string): Promise<FormatsResponse> {
const res = await Client.public.post("/api/downloader/formats/", { url });
return res.data as FormatsResponse;
// GET info for a URL
export async function fetchInfo(url: string): Promise<InfoResponse> {
const res = await Client.public.get("/api/downloader/download/", {
params: { url },
});
return res.data as InfoResponse;
}
// Download selected format as a Blob and resolve filename from headers
export async function downloadFormat(url: string, format_id: string): Promise<{ blob: Blob; filename: string }> {
// POST to stream binary immediately; returns { blob, filename }
export async function downloadImmediate(args: {
url: string;
ext: FormatExt;
videoResolution?: string | number; // "1080p" or 1080
audioResolution?: string | number; // "160kbps" or 160
}): Promise<{ blob: Blob; filename: string }> {
const video_quality = toHeight(args.videoResolution);
const audio_quality = toKbps(args.audioResolution);
if (video_quality == null || audio_quality == null) {
throw new Error("Please select both video and audio quality.");
}
const res = await Client.public.post(
"/api/downloader/download/",
{ url, format_id },
{
url: args.url,
ext: args.ext,
video_quality,
audio_quality,
},
{ responseType: "blob" }
);
// Try to parse Content-Disposition filename first, then X-Filename (exposed by backend)
const cd = res.headers?.["content-disposition"] as string | undefined;
const xfn = res.headers?.["x-filename"] as string | undefined;
const filename =
parseContentDispositionFilename(cd) ||
(xfn && xfn.trim()) ||
inferFilenameFromUrl(url, (res.headers?.["content-type"] as string | undefined)) ||
"download.bin";
inferFilenameFromUrl(args.url, res.headers?.["content-type"] as string | undefined) ||
`download.${args.ext}`;
return { blob: res.data as Blob, filename };
}
// Deprecated types kept for compatibility if referenced elsewhere
export type Choices = { file_types: string[]; qualities: string[] };
export type DownloadJobResponse = {
id: string;
status: "pending" | "running" | "finished" | "failed";
detail?: string;
download_url?: string;
progress?: number;
};
// Helpers
function parseContentDispositionFilename(cd?: string): string | null {
export function parseContentDispositionFilename(cd?: string): string | null {
if (!cd) return null;
// filename*=UTF-8''encoded or filename="plain"
const utf8Match = cd.match(/filename\*\s*=\s*UTF-8''([^;]+)/i);
if (utf8Match?.[1]) return decodeURIComponent(utf8Match[1]);
const plainMatch = cd.match(/filename\s*=\s*"([^"]+)"/i) || cd.match(/filename\s*=\s*([^;]+)/i);
@@ -89,16 +80,35 @@ function inferFilenameFromUrl(url: string, contentType?: string): string {
return "download.bin";
}
function contentTypeToExt(ct: string): string | null {
function contentTypeToExt(ct?: string): string | null {
if (!ct) return null;
const map: Record<string, string> = {
"video/mp4": "mp4",
"audio/mpeg": "mp3",
"audio/mp4": "m4a",
"audio/aac": "aac",
"audio/ogg": "ogg",
"video/x-matroska": "mkv",
"video/webm": "webm",
"audio/webm": "webm",
"video/x-flv": "flv",
"video/quicktime": "mov",
"video/x-msvideo": "avi",
"video/ogg": "ogg",
"application/octet-stream": "bin",
};
return map[ct] || null;
}
function toHeight(v?: string | number): number | undefined {
if (typeof v === "number") return v || undefined;
if (!v) return undefined;
const m = /^(\d{2,4})p$/i.exec(v.trim());
if (m) return parseInt(m[1], 10);
const n = Number(v);
return Number.isFinite(n) ? (n as number) : undefined;
}
function toKbps(v?: string | number): number | undefined {
if (typeof v === "number") return v || undefined;
if (!v) return undefined;
const m = /^(\d{2,4})\s*kbps$/i.exec(v.trim());
if (m) return parseInt(m[1], 10);
const n = Number(v);
return Number.isFinite(n) ? (n as number) : undefined;
}