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,59 +1,98 @@
import { useState } from "react";
import { probeFormats, downloadFormat, type FormatsResponse, type FormatOption } from "../../api/apps/Downloader";
import { useEffect, useMemo, useState } from "react";
import {
fetchInfo,
downloadImmediate,
FORMAT_EXTS,
type InfoResponse,
parseContentDispositionFilename,
} from "../../api/apps/Downloader";
export default function Downloader() {
const [url, setUrl] = useState("");
const [probing, setProbing] = useState(false);
const [downloadingId, setDownloadingId] = useState<string | null>(null);
const [downloading, setDownloading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [formats, setFormats] = useState<FormatsResponse | null>(null);
const [info, setInfo] = useState<InfoResponse | null>(null);
const [ext, setExt] = useState<typeof FORMAT_EXTS[number]>("mp4");
const [videoRes, setVideoRes] = useState<string | undefined>(undefined);
const [audioRes, setAudioRes] = useState<string | undefined>(undefined);
useEffect(() => {
if (info?.video_resolutions?.length && !videoRes) {
setVideoRes(info.video_resolutions[0]);
}
if (info?.audio_resolutions?.length && !audioRes) {
setAudioRes(info.audio_resolutions[0]);
}
}, [info]);
async function onProbe(e: React.FormEvent) {
e.preventDefault();
setError(null);
setFormats(null);
setInfo(null);
setProbing(true);
try {
const res = await probeFormats(url);
setFormats(res);
const res = await fetchInfo(url);
setInfo(res);
// reset selections from fresh info
setVideoRes(res.video_resolutions?.[0]);
setAudioRes(res.audio_resolutions?.[0]);
} catch (e: any) {
setError(e?.response?.data?.detail || e?.message || "Failed to load formats.");
setError(
e?.response?.data?.error ||
e?.response?.data?.detail ||
e?.message ||
"Failed to get info."
);
} finally {
setProbing(false);
}
}
async function onDownload(fmt: FormatOption) {
async function onDownload() {
setError(null);
setDownloadingId(fmt.format_id);
setDownloading(true);
try {
const { blob, filename } = await downloadFormat(url, fmt.format_id);
const link = document.createElement("a");
const { blob, filename } = await downloadImmediate({
url,
ext,
videoResolution: videoRes,
audioResolution: audioRes,
});
const name = filename || parseContentDispositionFilename("") || `download.${ext}`;
const href = URL.createObjectURL(blob);
link.href = href;
link.download = filename || "download.bin";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
const a = document.createElement("a");
a.href = href;
a.download = name;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(href);
} catch (e: any) {
setError(e?.response?.data?.detail || e?.message || "Download failed.");
setError(
e?.response?.data?.error ||
e?.response?.data?.detail ||
e?.message ||
"Download failed."
);
} finally {
setDownloadingId(null);
setDownloading(false);
}
}
const canDownload = useMemo(
() => !!url && !!ext && !!videoRes && !!audioRes,
[url, ext, videoRes, audioRes]
);
return (
<div className="max-w-3xl mx-auto p-4">
<h1 className="text-2xl font-semibold mb-4">Downloader</h1>
<div className="max-w-3xl mx-auto p-4 space-y-4">
<h1 className="text-2xl font-semibold">Downloader</h1>
{error && (
<div className="mb-3 rounded border border-red-300 bg-red-50 text-red-700 p-2">
{error}
</div>
)}
{error && <div className="rounded border border-red-300 bg-red-50 text-red-700 p-2">{error}</div>}
<form onSubmit={onProbe} className="grid gap-3 mb-4">
<form onSubmit={onProbe} className="grid gap-3">
<label className="grid gap-1">
<span className="text-sm font-medium">URL</span>
<input
@@ -65,76 +104,103 @@ export default function Downloader() {
className="w-full border rounded p-2"
/>
</label>
<div>
<div className="flex gap-2">
<button
type="submit"
disabled={!url || probing}
className="px-3 py-2 rounded bg-blue-600 text-white disabled:opacity-50"
>
{probing ? "Probing..." : "Find formats"}
{probing ? "Probing..." : "Get info"}
</button>
<button
type="button"
onClick={onDownload}
disabled={!canDownload || downloading}
className="px-3 py-2 rounded bg-emerald-600 text-white disabled:opacity-50"
>
{downloading ? "Downloading..." : "Download"}
</button>
</div>
</form>
{formats && (
{info && (
<div className="space-y-3">
<div className="text-sm text-gray-700">
<div><span className="font-medium">Title:</span> {formats.title || "-"}</div>
<div><span className="font-medium">Duration:</span> {formats.duration ? `${Math.round(formats.duration)} s` : "-"}</div>
<div><span className="font-medium">Max size:</span> {formatBytes(formats.max_size_bytes)}</div>
</div>
<div className="border rounded overflow-hidden">
<div className="grid grid-cols-6 gap-2 p-2 bg-gray-50 text-sm font-medium">
<div>Format</div>
<div>Resolution</div>
<div>Type</div>
<div>Note</div>
<div>Est. size</div>
<div></div>
</div>
<div className="divide-y">
{formats.options.map((o) => (
<div key={o.format_id} className="grid grid-cols-6 gap-2 p-2 items-center text-sm">
<div className="truncate">{o.format_id}{o.ext ? `.${o.ext}` : ""}</div>
<div>{o.resolution || (o.audio_only ? "audio" : "-")}</div>
<div>{o.audio_only ? "Audio" : "Video"}</div>
<div className="truncate">{o.format_note || "-"}</div>
<div className={o.size_ok ? "text-gray-800" : "text-red-600"}>
{o.estimated_size_bytes ? formatBytes(o.estimated_size_bytes) : (o.filesize || o.filesize_approx) ? "~" + formatBytes((o.filesize || o.filesize_approx)!) : "?"}
{!o.size_ok && " (too big)"}
</div>
<div className="text-right">
<button
onClick={() => onDownload(o)}
disabled={!o.size_ok || downloadingId === o.format_id}
className="px-2 py-1 rounded bg-emerald-600 text-white disabled:opacity-50"
>
{downloadingId === o.format_id ? "Downloading..." : "Download"}
</button>
</div>
</div>
))}
<div className="flex items-start gap-3">
{info.thumbnail && (
<img
src={info.thumbnail}
alt={info.title || "thumbnail"}
className="w-40 h-24 object-cover rounded border"
/>
)}
<div className="text-sm text-gray-800 space-y-1">
<div>
<span className="font-medium">Title:</span> {info.title || "-"}
</div>
<div>
<span className="font-medium">Duration:</span>{" "}
{info.duration ? `${Math.round(info.duration)} s` : "-"}
</div>
</div>
</div>
{!formats.options.length && (
<div className="text-sm text-gray-600">No formats available.</div>
)}
<div className="grid md:grid-cols-3 gap-3">
<label className="grid gap-1">
<span className="text-sm font-medium">Container</span>
<select
value={ext}
onChange={(e) => setExt(e.target.value as any)}
className="border rounded p-2"
>
{FORMAT_EXTS.map((x) => (
<option key={x} value={x}>
{x.toUpperCase()}
</option>
))}
</select>
</label>
<label className="grid gap-1">
<span className="text-sm font-medium">Video resolution</span>
<select
value={videoRes || ""}
onChange={(e) => setVideoRes(e.target.value || undefined)}
className="border rounded p-2"
>
{info.video_resolutions?.length ? (
info.video_resolutions.map((r) => (
<option key={r} value={r}>
{r}
</option>
))
) : (
<option value="">-</option>
)}
</select>
</label>
<label className="grid gap-1">
<span className="text-sm font-medium">Audio bitrate</span>
<select
value={audioRes || ""}
onChange={(e) => setAudioRes(e.target.value || undefined)}
className="border rounded p-2"
>
{info.audio_resolutions?.length ? (
info.audio_resolutions.map((r) => (
<option key={r} value={r}>
{r}
</option>
))
) : (
<option value="">-</option>
)}
</select>
</label>
</div>
</div>
)}
</div>
);
}
function formatBytes(bytes?: number | null): string {
if (!bytes || bytes <= 0) return "-";
const units = ["B", "KB", "MB", "GB"];
let i = 0;
let n = bytes;
while (n >= 1024 && i < units.length - 1) {
n /= 1024;
i++;
}
return `${n.toFixed(n < 10 && i > 0 ? 1 : 0)} ${units[i]}`;
}