import { spawn } from "child_process"; import { writeFileSync, mkdirSync, existsSync } from "fs"; import { join } from "path"; export interface DownloadOptions { url: string; outputDir: string; format: string; skipStartSeconds: number; } interface RawHeatmapSegment { start_seconds?: number; start_time?: number; end_seconds?: number; end_time?: number; intensity?: number; heat?: number; value?: number; } interface ProcessedSegment { start: number; end: number; intensity: number; integral: number; } interface VideoInfo { title: string; duration: number; heatmap?: RawHeatmapSegment[]; } async function getVideoInfo(url: string): Promise { return new Promise((resolve, reject) => { const ytDlp = spawn("yt-dlp", [ "--dump-json", "--no-download", "--compat-option", "no-youtube-channel-redirect", 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); resolve({ title: info.title || "video", duration: info.duration || 0, heatmap: info.heatmap, }); } 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 downloadSegment( url: string, outputPath: string, startTime: number, endTime: number, format: string ): Promise { const section = `*${startTime.toFixed(3)}-${endTime.toFixed(3)}`; 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 { if (!Number.isFinite(seconds) || seconds < 0) { return "0:00"; } const mins = Math.floor(seconds / 60); const secs = Math.floor(seconds % 60); return `${mins}:${secs.toString().padStart(2, "0")}`; } function getStartTime(segment: RawHeatmapSegment): number { return segment.start_seconds ?? segment.start_time ?? 0; } function getEndTime(segment: RawHeatmapSegment): number { return segment.end_seconds ?? segment.end_time ?? 0; } function getIntensity(segment: RawHeatmapSegment): number { return segment.intensity ?? segment.heat ?? segment.value ?? 0; } function findSegmentByIntegral( segments: RawHeatmapSegment[], skipStartSeconds: number ): ProcessedSegment | null { // Convert to processed format and filter valid segments const validSegments = segments .map(seg => ({ start: getStartTime(seg), end: getEndTime(seg), intensity: getIntensity(seg), })) .filter(seg => Number.isFinite(seg.start) && Number.isFinite(seg.end) && Number.isFinite(seg.intensity) && seg.start >= 0 && seg.end > seg.start ); if (validSegments.length === 0) { return null; } // Calculate the primitive (integral) for each segment // The integral represents cumulative watch time contribution let cumulativeIntegral = 0; const withIntegral = validSegments.map(seg => { const segmentDuration = seg.end - seg.start; const segmentIntegral = seg.intensity * segmentDuration; cumulativeIntegral += segmentIntegral; return { ...seg, integral: cumulativeIntegral, }; }); // Sort by integral value (highest contribution first) // The integral tells us which segment contributed most to total watch time withIntegral.sort((a, b) => b.integral - a.integral); // Find the highest integral segment that starts after skipStartSeconds const candidate = withIntegral.find(seg => seg.start >= skipStartSeconds); // If all segments are in the skipped region, return the highest one anyway return candidate || withIntegral[0]; } export async function downloadMostWatchedSegment(options: DownloadOptions): Promise { const { url, outputDir, format, skipStartSeconds } = options; // Create output directory if it doesn't exist if (!existsSync(outputDir)) { mkdirSync(outputDir, { recursive: true }); } // Get video info with heatmap data from YouTube console.log("Fetching video information from YouTube..."); const info = await getVideoInfo(url); const safeTitle = sanitizeFilename(info.title); console.log(`Video: ${info.title}`); console.log(`Duration: ${formatTime(info.duration)}`); // Check for heatmap data if (!info.heatmap || info.heatmap.length === 0) { console.log("\nNo heatmap data available for this video."); console.log("Downloading full video instead..."); const outputPath = join(outputDir, `${safeTitle}.%(ext)s`); await downloadSegment(url, outputPath, 0, info.duration, format); return; } console.log(`\nHeatmap data found: ${info.heatmap.length} segments`); // Find the segment with highest integral (using primitive function) const topSegment = findSegmentByIntegral(info.heatmap, skipStartSeconds); if (!topSegment) { console.log("No valid segments found. Downloading full video..."); const outputPath = join(outputDir, `${safeTitle}.%(ext)s`); await downloadSegment(url, outputPath, 0, info.duration, format); return; } console.log(`\nSegment with highest integral (primitive):`); console.log(` Time: ${formatTime(topSegment.start)} - ${formatTime(topSegment.end)}`); console.log(` Duration: ${formatTime(topSegment.end - topSegment.start)}`); console.log(` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%`); console.log(` Integral: ${topSegment.integral.toFixed(4)}`); // Download the segment const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`); console.log(`\nDownloading segment...`); await downloadSegment(url, outputPath, topSegment.start, topSegment.end, format); // Save segment info const segmentInfoPath = join(outputDir, `${safeTitle}_segment_info.txt`); const segmentInfo = `# ${info.title}\n\n` + `Most watched segment (highest integral from YouTube heatmap):\n` + ` Start: ${formatTime(topSegment.start)} (${topSegment.start.toFixed(1)}s)\n` + ` End: ${formatTime(topSegment.end)} (${topSegment.end.toFixed(1)}s)\n` + ` Duration: ${formatTime(topSegment.end - topSegment.start)}\n` + ` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%\n` + ` Integral: ${topSegment.integral.toFixed(4)}\n\n` + `Note: This segment had the highest integral value (cumulative watch contribution).\n`; writeFileSync(segmentInfoPath, segmentInfo); console.log(`\nSegment info saved to: ${segmentInfoPath}`); console.log("Download complete!"); }