feat: use integral (primitive) to find most watched segment

This commit is contained in:
Kilo Code Cloud
2026-01-14 19:56:13 +00:00
parent 8f4344e763
commit 44415040d5

View File

@@ -23,6 +23,7 @@ interface ProcessedSegment {
start: number; start: number;
end: number; end: number;
intensity: number; intensity: number;
integral: number;
} }
interface VideoInfo { interface VideoInfo {
@@ -142,7 +143,7 @@ function getIntensity(segment: RawHeatmapSegment): number {
return segment.intensity ?? segment.heat ?? segment.value ?? 0; return segment.intensity ?? segment.heat ?? segment.value ?? 0;
} }
function findHighestIntensitySegment( function findSegmentByIntegral(
segments: RawHeatmapSegment[], segments: RawHeatmapSegment[],
skipStartSeconds: number skipStartSeconds: number
): ProcessedSegment | null { ): ProcessedSegment | null {
@@ -165,14 +166,28 @@ function findHighestIntensitySegment(
return null; return null;
} }
// Sort by intensity (highest first) // Calculate the primitive (integral) for each segment
validSegments.sort((a, b) => b.intensity - a.intensity); // 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,
};
});
// Find the highest intensity segment that starts after skipStartSeconds // Sort by integral value (highest contribution first)
const candidate = validSegments.find(seg => seg.start >= skipStartSeconds); // 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 // If all segments are in the skipped region, return the highest one anyway
return candidate || validSegments[0]; return candidate || withIntegral[0];
} }
export async function downloadMostWatchedSegment(options: DownloadOptions): Promise<void> { export async function downloadMostWatchedSegment(options: DownloadOptions): Promise<void> {
@@ -203,8 +218,8 @@ export async function downloadMostWatchedSegment(options: DownloadOptions): Prom
console.log(`\nHeatmap data found: ${info.heatmap.length} segments`); console.log(`\nHeatmap data found: ${info.heatmap.length} segments`);
// Find the highest intensity segment (using primitive/intensity directly) // Find the segment with highest integral (using primitive function)
const topSegment = findHighestIntensitySegment(info.heatmap, skipStartSeconds); const topSegment = findSegmentByIntegral(info.heatmap, skipStartSeconds);
if (!topSegment) { if (!topSegment) {
console.log("No valid segments found. Downloading full video..."); console.log("No valid segments found. Downloading full video...");
@@ -213,10 +228,11 @@ export async function downloadMostWatchedSegment(options: DownloadOptions): Prom
return; return;
} }
console.log(`\nHighest intensity segment:`); console.log(`\nSegment with highest integral (primitive):`);
console.log(` Time: ${formatTime(topSegment.start)} - ${formatTime(topSegment.end)}`); console.log(` Time: ${formatTime(topSegment.start)} - ${formatTime(topSegment.end)}`);
console.log(` Duration: ${formatTime(topSegment.end - topSegment.start)}`); console.log(` Duration: ${formatTime(topSegment.end - topSegment.start)}`);
console.log(` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%`); console.log(` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%`);
console.log(` Integral: ${topSegment.integral.toFixed(4)}`);
// Download the segment // Download the segment
const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`); const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`);
@@ -227,12 +243,13 @@ export async function downloadMostWatchedSegment(options: DownloadOptions): Prom
// Save segment info // Save segment info
const segmentInfoPath = join(outputDir, `${safeTitle}_segment_info.txt`); const segmentInfoPath = join(outputDir, `${safeTitle}_segment_info.txt`);
const segmentInfo = `# ${info.title}\n\n` + const segmentInfo = `# ${info.title}\n\n` +
`Most watched segment (highest intensity from YouTube heatmap):\n` + `Most watched segment (highest integral from YouTube heatmap):\n` +
` Start: ${formatTime(topSegment.start)} (${topSegment.start.toFixed(1)}s)\n` + ` Start: ${formatTime(topSegment.start)} (${topSegment.start.toFixed(1)}s)\n` +
` End: ${formatTime(topSegment.end)} (${topSegment.end.toFixed(1)}s)\n` + ` End: ${formatTime(topSegment.end)} (${topSegment.end.toFixed(1)}s)\n` +
` Duration: ${formatTime(topSegment.end - topSegment.start)}\n` + ` Duration: ${formatTime(topSegment.end - topSegment.start)}\n` +
` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%\n\n` + ` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%\n` +
`Note: This segment had the highest watch intensity (excluding first ${skipStartSeconds}s).\n`; ` Integral: ${topSegment.integral.toFixed(4)}\n\n` +
`Note: This segment had the highest integral value (cumulative watch contribution).\n`;
writeFileSync(segmentInfoPath, segmentInfo); writeFileSync(segmentInfoPath, segmentInfo);
console.log(`\nSegment info saved to: ${segmentInfoPath}`); console.log(`\nSegment info saved to: ${segmentInfoPath}`);