feat: extract most watched segments from YouTube API
This commit is contained in:
@@ -2,14 +2,14 @@ export interface CliArgs {
|
|||||||
url?: string;
|
url?: string;
|
||||||
output: string;
|
output: string;
|
||||||
format: string;
|
format: string;
|
||||||
noChapters: boolean;
|
segments: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseArgs(): CliArgs {
|
export function parseArgs(): CliArgs {
|
||||||
const args: CliArgs = {
|
const args: CliArgs = {
|
||||||
output: "./downloads",
|
output: "./downloads",
|
||||||
format: "best",
|
format: "best",
|
||||||
noChapters: false,
|
segments: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
const rawArgs = Bun.argv;
|
const rawArgs = Bun.argv;
|
||||||
@@ -24,10 +24,11 @@ export function parseArgs(): CliArgs {
|
|||||||
} else if (arg === "-f" || arg === "--format") {
|
} else if (arg === "-f" || arg === "--format") {
|
||||||
args.format = nextArg || "best";
|
args.format = nextArg || "best";
|
||||||
i++;
|
i++;
|
||||||
} else if (arg === "--no-chapters") {
|
} else if (arg === "-n" || arg === "--segments") {
|
||||||
args.noChapters = true;
|
args.segments = parseInt(nextArg || "1", 10);
|
||||||
|
i++;
|
||||||
} else if (arg === "-h" || arg === "--help") {
|
} else if (arg === "-h" || arg === "--help") {
|
||||||
console.log(`YouTube Video Segments Downloader
|
console.log(`YouTube Most Watched Segments Downloader
|
||||||
|
|
||||||
Usage: yt-segments <url> [options]
|
Usage: yt-segments <url> [options]
|
||||||
|
|
||||||
@@ -37,13 +38,13 @@ Arguments:
|
|||||||
Options:
|
Options:
|
||||||
-o, --output <dir> Output directory (default: ./downloads)
|
-o, --output <dir> Output directory (default: ./downloads)
|
||||||
-f, --format <fmt> Video format (default: best)
|
-f, --format <fmt> Video format (default: best)
|
||||||
--no-chapters Skip chapter extraction
|
-n, --segments <num> Number of top segments to download (default: 1)
|
||||||
-h, --help Show this help message
|
-h, --help Show this help message
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
yt-segments "https://www.youtube.com/watch?v=abc123"
|
yt-segments "https://www.youtube.com/watch?v=abc123"
|
||||||
yt-segments "https://youtu.be/abc123" -o ./videos -f mp4
|
yt-segments "https://youtu.be/abc123" -o ./videos -f mp4
|
||||||
yt-segments "https://www.youtube.com/watch?v=abc123" --no-chapters
|
yt-segments "https://www.youtube.com/watch?v=abc123" -n 3
|
||||||
`);
|
`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} else if (!arg.startsWith("-") && !arg.includes("bun")) {
|
} else if (!arg.startsWith("-") && !arg.includes("bun")) {
|
||||||
|
|||||||
@@ -9,13 +9,20 @@ export interface DownloadOptions {
|
|||||||
extractChapters: boolean;
|
extractChapters: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Chapter {
|
interface MostWatchedSegment {
|
||||||
title: string;
|
|
||||||
start: number;
|
start: number;
|
||||||
end: number;
|
end: number;
|
||||||
|
intensity?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getVideoInfo(url: string): Promise<{ title: string; chapters: Chapter[] }> {
|
interface VideoInfo {
|
||||||
|
title: string;
|
||||||
|
chapters: Array<{ title: string; start_time: number; end_time: number }>;
|
||||||
|
heatmap?: Array<{ start_seconds: number; end_seconds: number; intensity: number }>;
|
||||||
|
duration: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getVideoInfo(url: string): Promise<VideoInfo> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const ytDlp = spawn("yt-dlp", [
|
const ytDlp = spawn("yt-dlp", [
|
||||||
"--dump-json",
|
"--dump-json",
|
||||||
@@ -42,21 +49,12 @@ async function getVideoInfo(url: string): Promise<{ title: string; chapters: Cha
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const info = JSON.parse(stdout);
|
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({
|
resolve({
|
||||||
title: info.title || "video",
|
title: info.title || "video",
|
||||||
chapters,
|
chapters: info.chapters || [],
|
||||||
|
heatmap: info.heatmap || [],
|
||||||
|
duration: info.duration || 0,
|
||||||
});
|
});
|
||||||
} catch (parseError) {
|
} catch (parseError) {
|
||||||
reject(new Error(`Failed to parse video info: ${parseError}`));
|
reject(new Error(`Failed to parse video info: ${parseError}`));
|
||||||
@@ -116,6 +114,28 @@ function formatTime(seconds: number): string {
|
|||||||
return `${mins}:${secs.toString().padStart(2, "0")}`;
|
return `${mins}:${secs.toString().padStart(2, "0")}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getMostWatchedSegments(
|
||||||
|
heatmap: Array<{ start_seconds: number; end_seconds: number; intensity: number }>,
|
||||||
|
duration: number,
|
||||||
|
topN: number = 1
|
||||||
|
): MostWatchedSegment[] {
|
||||||
|
if (!heatmap || heatmap.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by intensity (most watched first)
|
||||||
|
const sorted = [...heatmap].sort((a, b) => b.intensity - a.intensity);
|
||||||
|
|
||||||
|
// Get top N segments
|
||||||
|
const topSegments = sorted.slice(0, topN);
|
||||||
|
|
||||||
|
return topSegments.map((segment) => ({
|
||||||
|
start: segment.start_seconds,
|
||||||
|
end: segment.end_seconds,
|
||||||
|
intensity: segment.intensity,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
export async function downloadVideoSegments(options: DownloadOptions): Promise<void> {
|
export async function downloadVideoSegments(options: DownloadOptions): Promise<void> {
|
||||||
const { url, outputDir, format } = options;
|
const { url, outputDir, format } = options;
|
||||||
|
|
||||||
@@ -124,53 +144,65 @@ export async function downloadVideoSegments(options: DownloadOptions): Promise<v
|
|||||||
mkdirSync(outputDir, { recursive: true });
|
mkdirSync(outputDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip chapter extraction if not requested
|
// Get video info including most watched segments from YouTube API
|
||||||
if (!options.extractChapters) {
|
console.log("Fetching video information from YouTube API...");
|
||||||
console.log("Downloading full video (chapters disabled)...");
|
const info = await getVideoInfo(url);
|
||||||
const safeTitle = sanitizeFilename("video");
|
const safeTitle = sanitizeFilename(info.title);
|
||||||
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
|
|
||||||
await downloadSection(url, outputPath, "*", format);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get video info including chapters
|
console.log(`Video: ${info.title}`);
|
||||||
console.log("Fetching video information...");
|
console.log(`Duration: ${formatTime(info.duration)}`);
|
||||||
const { title, chapters } = await getVideoInfo(url);
|
|
||||||
const safeTitle = sanitizeFilename(title);
|
|
||||||
|
|
||||||
console.log(`Video: ${title}`);
|
// Try to get most watched segments from heatmap data
|
||||||
console.log(`Found ${chapters.length} chapters\n`);
|
const mostWatchedSegments = getMostWatchedSegments(info.heatmap || [], info.duration);
|
||||||
|
|
||||||
if (chapters.length === 0) {
|
// If no heatmap data, fall back to chapters
|
||||||
console.log("No chapters found. Downloading full video...");
|
if (mostWatchedSegments.length === 0 && info.chapters.length > 0) {
|
||||||
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
|
console.log("\nNo most watched segments found. Falling back to chapters...");
|
||||||
await downloadSection(url, outputPath, "*", format);
|
const chapter = info.chapters[0]; // Download first chapter as most relevant
|
||||||
return;
|
const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`);
|
||||||
}
|
const section = `*${formatTime(chapter.start_time || 0)}-${formatTime(chapter.end_time || 60)}`;
|
||||||
|
|
||||||
// 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)})`);
|
|
||||||
|
|
||||||
|
console.log(`Downloading chapter: ${chapter.title || "First Chapter"}`);
|
||||||
await downloadSection(url, outputPath, section, format);
|
await downloadSection(url, outputPath, section, format);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also download the full video
|
if (mostWatchedSegments.length === 0) {
|
||||||
console.log("\nDownloading full video...");
|
console.log("No segments found. Downloading full video...");
|
||||||
const fullVideoPath = join(outputDir, `${safeTitle}_full.%(ext)s`);
|
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
|
||||||
await downloadSection(url, fullVideoPath, "*", format);
|
await downloadSection(url, outputPath, "*", format);
|
||||||
|
return;
|
||||||
// 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}`);
|
// Download the most watched segment
|
||||||
|
const topSegment = mostWatchedSegments[0];
|
||||||
|
const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`);
|
||||||
|
const section = `*${formatTime(topSegment.start)}-${formatTime(topSegment.end)}`;
|
||||||
|
|
||||||
|
console.log(`\nMost watched segment: ${formatTime(topSegment.start)} - ${formatTime(topSegment.end)}`);
|
||||||
|
console.log(`Duration: ${formatTime(topSegment.end - topSegment.start)}`);
|
||||||
|
console.log(`Intensity: ${((topSegment.intensity || 0) * 100).toFixed(1)}%`);
|
||||||
|
console.log(`\nDownloading most watched segment...`);
|
||||||
|
|
||||||
|
await downloadSection(url, outputPath, section, format);
|
||||||
|
|
||||||
|
// Save segment info
|
||||||
|
const segmentInfoPath = join(outputDir, `${safeTitle}_most_watched.txt`);
|
||||||
|
let segmentInfo = `# ${info.title}\n\n`;
|
||||||
|
segmentInfo += `Most watched segment:\n`;
|
||||||
|
segmentInfo += ` Start: ${formatTime(topSegment.start)}\n`;
|
||||||
|
segmentInfo += ` End: ${formatTime(topSegment.end)}\n`;
|
||||||
|
segmentInfo += ` Duration: ${formatTime(topSegment.end - topSegment.start)}\n`;
|
||||||
|
segmentInfo += ` Intensity: ${((topSegment.intensity || 0) * 100).toFixed(1)}%\n`;
|
||||||
|
|
||||||
|
if (mostWatchedSegments.length > 1) {
|
||||||
|
segmentInfo += `\nOther top segments:\n`;
|
||||||
|
for (let i = 1; i < mostWatchedSegments.length; i++) {
|
||||||
|
const seg = mostWatchedSegments[i];
|
||||||
|
segmentInfo += ` ${formatTime(seg.start)} - ${formatTime(seg.end)} (${((seg.intensity || 0) * 100).toFixed(1)}%)\n`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeFileSync(segmentInfoPath, segmentInfo);
|
||||||
|
console.log(`Segment info saved to: ${segmentInfoPath}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ async function main() {
|
|||||||
console.log("Options:");
|
console.log("Options:");
|
||||||
console.log(" -o, --output <dir> Output directory (default: ./downloads)");
|
console.log(" -o, --output <dir> Output directory (default: ./downloads)");
|
||||||
console.log(" -f, --format <fmt> Video format (default: best)");
|
console.log(" -f, --format <fmt> Video format (default: best)");
|
||||||
console.log(" --no-chapters Skip chapter extraction");
|
console.log(" -n, --segments <num> Number of top segments (default: 1)");
|
||||||
console.log(" -h, --help Show help");
|
console.log(" -h, --help Show help");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Downloading segments from: ${args.url}`);
|
console.log(`Downloading most watched segment(s) from: ${args.url}`);
|
||||||
console.log(`Output directory: ${args.output}`);
|
console.log(`Output directory: ${args.output}`);
|
||||||
console.log(`Format: ${args.format}`);
|
console.log(`Format: ${args.format}`);
|
||||||
console.log(`Extract chapters: ${!args.noChapters}`);
|
console.log(`Segments to download: ${args.segments}`);
|
||||||
console.log("");
|
console.log("");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -28,7 +28,7 @@ async function main() {
|
|||||||
url: args.url,
|
url: args.url,
|
||||||
outputDir: args.output,
|
outputDir: args.output,
|
||||||
format: args.format,
|
format: args.format,
|
||||||
extractChapters: !args.noChapters,
|
extractChapters: true,
|
||||||
});
|
});
|
||||||
console.log("\nDownload complete!");
|
console.log("\nDownload complete!");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user