2026-01-14 18:46:21 +00:00
|
|
|
import { spawn } from "child_process";
|
|
|
|
|
import { writeFileSync, mkdirSync, existsSync } from "fs";
|
|
|
|
|
import { join } from "path";
|
|
|
|
|
|
|
|
|
|
export interface DownloadOptions {
|
|
|
|
|
url: string;
|
|
|
|
|
outputDir: string;
|
|
|
|
|
format: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
interface HeatmapSegment {
|
|
|
|
|
start_seconds: number;
|
|
|
|
|
end_seconds: number;
|
|
|
|
|
intensity: number;
|
2026-01-14 19:39:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface VideoInfo {
|
|
|
|
|
title: string;
|
|
|
|
|
duration: number;
|
2026-01-14 19:42:45 +00:00
|
|
|
heatmap?: HeatmapSegment[];
|
2026-01-14 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:39:34 +00:00
|
|
|
async function getVideoInfo(url: string): Promise<VideoInfo> {
|
2026-01-14 18:46:21 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const ytDlp = spawn("yt-dlp", [
|
|
|
|
|
"--dump-json",
|
|
|
|
|
"--no-download",
|
2026-01-14 19:42:45 +00:00
|
|
|
"--compat-option",
|
|
|
|
|
"no-youtube-channel-redirect",
|
2026-01-14 18:46:21 +00:00
|
|
|
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);
|
|
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
// Extract heatmap data from YouTube's internal API
|
|
|
|
|
// The heatmap shows what segments were re-watched the most
|
|
|
|
|
const heatmapData = info.heatmap;
|
|
|
|
|
|
2026-01-14 18:46:21 +00:00
|
|
|
resolve({
|
|
|
|
|
title: info.title || "video",
|
2026-01-14 19:39:34 +00:00
|
|
|
duration: info.duration || 0,
|
2026-01-14 19:42:45 +00:00
|
|
|
heatmap: heatmapData,
|
2026-01-14 18:46:21 +00:00
|
|
|
});
|
|
|
|
|
} 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}`));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
async function downloadSegment(
|
2026-01-14 18:46:21 +00:00
|
|
|
url: string,
|
|
|
|
|
outputPath: string,
|
2026-01-14 19:42:45 +00:00
|
|
|
startTime: number,
|
|
|
|
|
endTime: number,
|
2026-01-14 18:46:21 +00:00
|
|
|
format: string
|
|
|
|
|
): Promise<void> {
|
2026-01-14 19:42:45 +00:00
|
|
|
const section = `*${startTime.toFixed(3)}-${endTime.toFixed(3)}`;
|
|
|
|
|
|
2026-01-14 18:46:21 +00:00
|
|
|
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")}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
export async function downloadMostWatchedSegment(options: DownloadOptions): Promise<void> {
|
2026-01-14 18:46:21 +00:00
|
|
|
const { url, outputDir, format } = options;
|
|
|
|
|
|
|
|
|
|
// Create output directory if it doesn't exist
|
|
|
|
|
if (!existsSync(outputDir)) {
|
|
|
|
|
mkdirSync(outputDir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
// Get video info with heatmap data from YouTube
|
|
|
|
|
console.log("Fetching video information from YouTube...");
|
2026-01-14 19:39:34 +00:00
|
|
|
const info = await getVideoInfo(url);
|
|
|
|
|
const safeTitle = sanitizeFilename(info.title);
|
|
|
|
|
|
|
|
|
|
console.log(`Video: ${info.title}`);
|
|
|
|
|
console.log(`Duration: ${formatTime(info.duration)}`);
|
2026-01-14 18:46:21 +00:00
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
// Check for heatmap data - this shows what was re-watched the most
|
|
|
|
|
if (!info.heatmap || info.heatmap.length === 0) {
|
|
|
|
|
console.log("\nNo heatmap data available for this video.");
|
|
|
|
|
console.log("The video may not have enough view data to determine most watched segments.");
|
|
|
|
|
console.log("Downloading full video instead...");
|
|
|
|
|
|
2026-01-14 18:46:21 +00:00
|
|
|
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
|
2026-01-14 19:42:45 +00:00
|
|
|
await downloadSegment(url, outputPath, 0, info.duration, format);
|
2026-01-14 18:46:21 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:42:45 +00:00
|
|
|
// Find the most watched segment (highest intensity)
|
|
|
|
|
const mostWatched = info.heatmap.reduce((max, current) => {
|
|
|
|
|
return current.intensity > max.intensity ? current : max;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`\nHeatmap data found: ${info.heatmap.length} segments`);
|
|
|
|
|
console.log(`Most watched segment intensity: ${(mostWatched.intensity * 100).toFixed(1)}%`);
|
|
|
|
|
console.log(`Segment: ${formatTime(mostWatched.start_seconds)} - ${formatTime(mostWatched.end_seconds)}`);
|
|
|
|
|
|
2026-01-14 19:39:34 +00:00
|
|
|
// Download the most watched segment
|
|
|
|
|
const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`);
|
|
|
|
|
|
|
|
|
|
console.log(`\nDownloading most watched segment...`);
|
2026-01-14 19:42:45 +00:00
|
|
|
await downloadSegment(
|
|
|
|
|
url,
|
|
|
|
|
outputPath,
|
|
|
|
|
mostWatched.start_seconds,
|
|
|
|
|
mostWatched.end_seconds,
|
|
|
|
|
format
|
|
|
|
|
);
|
2026-01-14 19:39:34 +00:00
|
|
|
|
|
|
|
|
// Save segment info
|
2026-01-14 19:42:45 +00:00
|
|
|
const segmentInfoPath = join(outputDir, `${safeTitle}_segment_info.txt`);
|
|
|
|
|
const segmentInfo = `# ${info.title}\n\n` +
|
|
|
|
|
`Most watched segment (from YouTube heatmap):\n` +
|
|
|
|
|
` Start: ${formatTime(mostWatched.start_seconds)} (${mostWatched.start_seconds}s)\n` +
|
|
|
|
|
` End: ${formatTime(mostWatched.end_seconds)} (${mostWatched.end_seconds}s)\n` +
|
|
|
|
|
` Duration: ${formatTime(mostWatched.end_seconds - mostWatched.start_seconds)}\n` +
|
|
|
|
|
` Intensity: ${(mostWatched.intensity * 100).toFixed(1)}%\n\n` +
|
|
|
|
|
`Note: This segment had the highest re-watch rate according to YouTube's analytics.\n`;
|
2026-01-14 18:46:21 +00:00
|
|
|
|
2026-01-14 19:39:34 +00:00
|
|
|
writeFileSync(segmentInfoPath, segmentInfo);
|
2026-01-14 19:42:45 +00:00
|
|
|
console.log(`\nSegment info saved to: ${segmentInfoPath}`);
|
|
|
|
|
console.log("Download complete!");
|
2026-01-14 18:46:21 +00:00
|
|
|
}
|