Production-ready TypeScript code using the REST API, with your key from the dashboard
ml-dataset-builder.ts Copyconst API = "https://videodownloadapi.com/api/v1";
const headers = {
Authorization: `Bearer ${process.env.VIDEO_API_KEY}`,
"Content-Type": "application/json",
};
type Download = { download_id: string; status: string; title: string; download_url?: string };
async function download(url: string, options: { format?: string; quality?: string } = {}) {
const res = await fetch(`${API}/downloads`, {
method: "POST",
headers,
body: JSON.stringify({ url, ...options }),
});
if (res.status === 402) throw new Error("Not enough minutes");
if (res.status === 429) throw new Error("Too many requests: retry in a few seconds");
let item: Download = await res.json();
// Poll until the file is ready (or failed: minutes are refunded automatically)
while (item.status !== "ready" && item.status !== "failed") {
await new Promise((resolve) => setTimeout(resolve, 3000));
item = await fetch(`${API}/downloads/${item.download_id}`, { headers }).then((r) => r.json());
}
if (item.status === "failed") throw new Error(`Download failed: ${url}`);
return item;
}
// Build a training set: video for vision models, audio for speech models
async function buildTrainingDataset(urls: string[]) {
const dataset = [];
for (const url of urls) {
const video = await download(url, { format: "mp4", quality: "720p" });
const audio = await download(url, { format: "wav" });
dataset.push({ url, title: video.title, video: video.download_url, audio: audio.download_url });
}
console.log(JSON.stringify(dataset, null, 2));
}
buildTrainingDataset(["https://youtube.com/watch?v=VIDEO_ID"]);