feat: add YouTube segments CLI using yt-dlp

This commit is contained in:
Kilo Code Cloud
2026-01-14 18:46:21 +00:00
parent dbb067fbab
commit a657434723
6 changed files with 393 additions and 6 deletions

58
src/cli/args.ts Normal file
View File

@@ -0,0 +1,58 @@
export interface CliArgs {
url?: string;
output: string;
format: string;
noChapters: boolean;
}
export function parseArgs(): CliArgs {
const args: CliArgs = {
output: "./downloads",
format: "best",
noChapters: false,
};
const rawArgs = Bun.argv;
for (let i = 0; i < rawArgs.length; i++) {
const arg = rawArgs[i];
const nextArg = rawArgs[i + 1];
if (arg === "-o" || arg === "--output") {
args.output = nextArg || "./downloads";
i++;
} else if (arg === "-f" || arg === "--format") {
args.format = nextArg || "best";
i++;
} else if (arg === "--no-chapters") {
args.noChapters = true;
} else if (arg === "-h" || arg === "--help") {
console.log(`YouTube Video Segments Downloader
Usage: yt-segments <url> [options]
Arguments:
<url> YouTube video URL (required)
Options:
-o, --output <dir> Output directory (default: ./downloads)
-f, --format <fmt> Video format (default: best)
--no-chapters Skip chapter extraction
-h, --help Show this help message
Examples:
yt-segments "https://www.youtube.com/watch?v=abc123"
yt-segments "https://youtu.be/abc123" -o ./videos -f mp4
yt-segments "https://www.youtube.com/watch?v=abc123" --no-chapters
`);
process.exit(0);
} else if (!arg.startsWith("-") && !arg.includes("bun")) {
// This is likely the URL
if (!arg.startsWith("bun") && !arg.includes("node")) {
args.url = arg;
}
}
}
return args;
}

176
src/cli/downloader.ts Normal file
View File

@@ -0,0 +1,176 @@
import { spawn } from "child_process";
import { writeFileSync, mkdirSync, existsSync } from "fs";
import { join } from "path";
export interface DownloadOptions {
url: string;
outputDir: string;
format: string;
extractChapters: boolean;
}
interface Chapter {
title: string;
start: number;
end: number;
}
async function getVideoInfo(url: string): Promise<{ title: string; chapters: Chapter[] }> {
return new Promise((resolve, reject) => {
const ytDlp = spawn("yt-dlp", [
"--dump-json",
"--no-download",
url,
]);
let stdout = "";
let stderr = "";
ytDlp.stdout.on("data", (data) => {
stdout += data.toString();
});
ytDlp.stderr.on("data", (data) => {
stderr += data.toString();
});
ytDlp.on("close", (code) => {
if (code !== 0) {
reject(new Error(`yt-dlp failed: ${stderr}`));
return;
}
try {
const info = JSON.parse(stdout);
const chapters: Chapter[] = [];
if (info.chapters && Array.isArray(info.chapters)) {
for (const chapter of info.chapters) {
chapters.push({
title: chapter.title || `Chapter ${chapters.length + 1}`,
start: chapter.start_time || 0,
end: chapter.end_time || 0,
});
}
}
resolve({
title: info.title || "video",
chapters,
});
} catch (parseError) {
reject(new Error(`Failed to parse video info: ${parseError}`));
}
});
ytDlp.on("error", (err) => {
reject(new Error(`Failed to run yt-dlp: ${err.message}`));
});
});
}
async function downloadSection(
url: string,
outputPath: string,
section: string,
format: string
): Promise<void> {
return new Promise((resolve, reject) => {
const ytDlp = spawn("yt-dlp", [
"-f", format,
"--download-sections", section,
"-o", outputPath,
url,
]);
let stderr = "";
ytDlp.stderr.on("data", (data) => {
stderr += data.toString();
});
ytDlp.on("close", (code) => {
if (code !== 0) {
reject(new Error(`yt-dlp failed: ${stderr}`));
return;
}
resolve();
});
ytDlp.on("error", (err) => {
reject(new Error(`Failed to run yt-dlp: ${err.message}`));
});
});
}
function sanitizeFilename(filename: string): string {
return filename
.replace(/[^a-zA-Z0-9\s\-_]/g, "")
.replace(/\s+/g, "_")
.substring(0, 100);
}
function formatTime(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, "0")}`;
}
export async function downloadVideoSegments(options: DownloadOptions): Promise<void> {
const { url, outputDir, format } = options;
// Create output directory if it doesn't exist
if (!existsSync(outputDir)) {
mkdirSync(outputDir, { recursive: true });
}
// Skip chapter extraction if not requested
if (!options.extractChapters) {
console.log("Downloading full video (chapters disabled)...");
const safeTitle = sanitizeFilename("video");
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
await downloadSection(url, outputPath, "*", format);
return;
}
// Get video info including chapters
console.log("Fetching video information...");
const { title, chapters } = await getVideoInfo(url);
const safeTitle = sanitizeFilename(title);
console.log(`Video: ${title}`);
console.log(`Found ${chapters.length} chapters\n`);
if (chapters.length === 0) {
console.log("No chapters found. Downloading full video...");
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
await downloadSection(url, outputPath, "*", format);
return;
}
// Download each chapter as a separate segment
for (let i = 0; i < chapters.length; i++) {
const chapter = chapters[i];
const segmentNumber = (i + 1).toString().padStart(2, "0");
const outputPath = join(outputDir, `${safeTitle}_${segmentNumber}_${sanitizeFilename(chapter.title)}.%(ext)s`);
const section = `*${formatTime(chapter.start)}-${formatTime(chapter.end)}`;
console.log(`[${i + 1}/${chapters.length}] Downloading: ${chapter.title} (${formatTime(chapter.end - chapter.start)})`);
await downloadSection(url, outputPath, section, format);
}
// Also download the full video
console.log("\nDownloading full video...");
const fullVideoPath = join(outputDir, `${safeTitle}_full.%(ext)s`);
await downloadSection(url, fullVideoPath, "*", format);
// Save chapter list
const chapterListPath = join(outputDir, `${safeTitle}_chapters.txt`);
let chapterList = `# ${title}\n\n`;
for (const chapter of chapters) {
chapterList += `${formatTime(chapter.start)} - ${chapter.title}\n`;
}
writeFileSync(chapterListPath, chapterList);
console.log(`Chapter list saved to: ${chapterListPath}`);
}

40
src/cli/index.ts Normal file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bun
import { downloadVideoSegments } from "./downloader.js";
import { parseArgs } from "./args.js";
async function main() {
const args = parseArgs();
if (!args.url) {
console.error("Error: YouTube URL is required");
console.log("Usage: yt-segments <url> [options]");
console.log("Options:");
console.log(" -o, --output <dir> Output directory (default: ./downloads)");
console.log(" -f, --format <fmt> Video format (default: best)");
console.log(" --no-chapters Skip chapter extraction");
console.log(" -h, --help Show help");
process.exit(1);
}
console.log(`Downloading segments from: ${args.url}`);
console.log(`Output directory: ${args.output}`);
console.log(`Format: ${args.format}`);
console.log(`Extract chapters: ${!args.noChapters}`);
console.log("");
try {
await downloadVideoSegments({
url: args.url,
outputDir: args.output,
format: args.format,
extractChapters: !args.noChapters,
});
console.log("\nDownload complete!");
} catch (error) {
console.error("Error:", error instanceof Error ? error.message : error);
process.exit(1);
}
}
main();