feat: use raw intensity values to find most watched segment
This commit is contained in:
@@ -2,14 +2,14 @@ export interface CliArgs {
|
|||||||
url?: string;
|
url?: string;
|
||||||
output: string;
|
output: string;
|
||||||
format: string;
|
format: string;
|
||||||
peakThreshold: number;
|
skipStartSeconds: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseArgs(): CliArgs {
|
export function parseArgs(): CliArgs {
|
||||||
const args: CliArgs = {
|
const args: CliArgs = {
|
||||||
output: "./downloads",
|
output: "./downloads",
|
||||||
format: "best",
|
format: "best",
|
||||||
peakThreshold: 0.3,
|
skipStartSeconds: 30,
|
||||||
};
|
};
|
||||||
|
|
||||||
const rawArgs = Bun.argv;
|
const rawArgs = Bun.argv;
|
||||||
@@ -24,11 +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 === "-t" || arg === "--threshold") {
|
} else if (arg === "-s" || arg === "--skip-start") {
|
||||||
args.peakThreshold = parseFloat(nextArg || "0.3");
|
args.skipStartSeconds = parseFloat(nextArg || "30");
|
||||||
i++;
|
i++;
|
||||||
} else if (arg === "-h" || arg === "--help") {
|
} else if (arg === "-h" || arg === "--help") {
|
||||||
console.log(`YouTube Peak Segment Downloader
|
console.log(`YouTube Most Watched Segment Downloader
|
||||||
|
|
||||||
Usage: yt-segments <url> [options]
|
Usage: yt-segments <url> [options]
|
||||||
|
|
||||||
@@ -38,14 +38,14 @@ 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)
|
||||||
-t, --threshold <n> Peak detection threshold 0.1-1.0 (default: 0.3)
|
-s, --skip-start <sec> Skip first N seconds (default: 30)
|
||||||
Lower = more segments detected as peaks
|
Higher values skip more of the intro
|
||||||
-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" -t 0.5
|
yt-segments "https://www.youtube.com/watch?v=abc123" -s 60
|
||||||
`);
|
`);
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} else if (!arg.startsWith("-") && !arg.includes("bun")) {
|
} else if (!arg.startsWith("-") && !arg.includes("bun")) {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export interface DownloadOptions {
|
|||||||
url: string;
|
url: string;
|
||||||
outputDir: string;
|
outputDir: string;
|
||||||
format: string;
|
format: string;
|
||||||
peakThreshold: number;
|
skipStartSeconds: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RawHeatmapSegment {
|
interface RawHeatmapSegment {
|
||||||
@@ -23,7 +23,6 @@ interface ProcessedSegment {
|
|||||||
start: number;
|
start: number;
|
||||||
end: number;
|
end: number;
|
||||||
intensity: number;
|
intensity: number;
|
||||||
peakScore: number;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface VideoInfo {
|
interface VideoInfo {
|
||||||
@@ -143,61 +142,41 @@ function getIntensity(segment: RawHeatmapSegment): number {
|
|||||||
return segment.intensity ?? segment.heat ?? segment.value ?? 0;
|
return segment.intensity ?? segment.heat ?? segment.value ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findPeakSegments(
|
function findHighestIntensitySegment(
|
||||||
segments: RawHeatmapSegment[],
|
segments: RawHeatmapSegment[],
|
||||||
threshold: number = 0.3
|
skipStartSeconds: number
|
||||||
): ProcessedSegment[] {
|
): ProcessedSegment | null {
|
||||||
if (segments.length < 3) {
|
// Convert to processed format and filter valid segments
|
||||||
return [];
|
const validSegments = segments
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to processed format
|
|
||||||
const processed = segments
|
|
||||||
.map(seg => ({
|
.map(seg => ({
|
||||||
start: getStartTime(seg),
|
start: getStartTime(seg),
|
||||||
end: getEndTime(seg),
|
end: getEndTime(seg),
|
||||||
intensity: getIntensity(seg),
|
intensity: getIntensity(seg),
|
||||||
peakScore: 0,
|
|
||||||
}))
|
}))
|
||||||
.filter(seg =>
|
.filter(seg =>
|
||||||
Number.isFinite(seg.start) &&
|
Number.isFinite(seg.start) &&
|
||||||
Number.isFinite(seg.end) &&
|
Number.isFinite(seg.end) &&
|
||||||
Number.isFinite(seg.intensity)
|
Number.isFinite(seg.intensity) &&
|
||||||
|
seg.start >= 0 &&
|
||||||
|
seg.end > seg.start
|
||||||
);
|
);
|
||||||
|
|
||||||
if (processed.length < 3) {
|
if (validSegments.length === 0) {
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate peak score for each segment
|
// Sort by intensity (highest first)
|
||||||
// A peak is where intensity is significantly higher than neighbors
|
validSegments.sort((a, b) => b.intensity - a.intensity);
|
||||||
const scored = processed.map((seg, i) => {
|
|
||||||
const prevIntensity = i > 0 ? processed[i - 1].intensity : seg.intensity;
|
|
||||||
const nextIntensity = i < processed.length - 1 ? processed[i + 1].intensity : seg.intensity;
|
|
||||||
|
|
||||||
// Peak score = how much higher this segment is compared to average of neighbors
|
// Find the highest intensity segment that starts after skipStartSeconds
|
||||||
const avgNeighborIntensity = (prevIntensity + nextIntensity) / 2;
|
const candidate = validSegments.find(seg => seg.start >= skipStartSeconds);
|
||||||
const peakScore = avgNeighborIntensity > 0
|
|
||||||
? (seg.intensity - avgNeighborIntensity) / avgNeighborIntensity
|
|
||||||
: 0;
|
|
||||||
|
|
||||||
return {
|
// If all segments are in the skipped region, return the highest one anyway
|
||||||
...seg,
|
return candidate || validSegments[0];
|
||||||
peakScore,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
// Filter segments that are true peaks (higher than neighbors)
|
|
||||||
const peaks = scored.filter(seg => seg.peakScore > threshold);
|
|
||||||
|
|
||||||
// Sort by peak score (highest peaks first)
|
|
||||||
peaks.sort((a, b) => b.peakScore - a.peakScore);
|
|
||||||
|
|
||||||
return peaks;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadMostWatchedSegment(options: DownloadOptions): Promise<void> {
|
export async function downloadMostWatchedSegment(options: DownloadOptions): Promise<void> {
|
||||||
const { url, outputDir, format, peakThreshold } = options;
|
const { url, outputDir, format, skipStartSeconds } = options;
|
||||||
|
|
||||||
// Create output directory if it doesn't exist
|
// Create output directory if it doesn't exist
|
||||||
if (!existsSync(outputDir)) {
|
if (!existsSync(outputDir)) {
|
||||||
@@ -224,49 +203,36 @@ 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 peak segments (segments that stand out from their neighbors)
|
// Find the highest intensity segment (using primitive/intensity directly)
|
||||||
const peakSegments = findPeakSegments(info.heatmap, peakThreshold);
|
const topSegment = findHighestIntensitySegment(info.heatmap, skipStartSeconds);
|
||||||
|
|
||||||
if (peakSegments.length === 0) {
|
if (!topSegment) {
|
||||||
console.log("No significant peak segments found.");
|
console.log("No valid segments found. Downloading full video...");
|
||||||
console.log("Downloading full video...");
|
|
||||||
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
|
const outputPath = join(outputDir, `${safeTitle}.%(ext)s`);
|
||||||
await downloadSegment(url, outputPath, 0, info.duration, format);
|
await downloadSegment(url, outputPath, 0, info.duration, format);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the top peak segment
|
console.log(`\nHighest intensity segment:`);
|
||||||
const topPeak = peakSegments[0];
|
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(`\nTop peak segment:`);
|
// Download the segment
|
||||||
console.log(` Time: ${formatTime(topPeak.start)} - ${formatTime(topPeak.end)}`);
|
const outputPath = join(outputDir, `${safeTitle}_most_watched.%(ext)s`);
|
||||||
console.log(` Duration: ${formatTime(topPeak.end - topPeak.start)}`);
|
|
||||||
console.log(` Peak Score: ${(topPeak.peakScore * 100).toFixed(1)}%`);
|
|
||||||
console.log(` Base Intensity: ${(topPeak.intensity * 100).toFixed(1)}%`);
|
|
||||||
|
|
||||||
// Download the peak segment
|
console.log(`\nDownloading segment...`);
|
||||||
const outputPath = join(outputDir, `${safeTitle}_peak.%(ext)s`);
|
await downloadSegment(url, outputPath, topSegment.start, topSegment.end, format);
|
||||||
|
|
||||||
console.log(`\nDownloading peak segment...`);
|
|
||||||
await downloadSegment(url, outputPath, topPeak.start, topPeak.end, format);
|
|
||||||
|
|
||||||
// Save segment info
|
// Save segment info
|
||||||
const segmentInfoPath = join(outputDir, `${safeTitle}_peak_info.txt`);
|
const segmentInfoPath = join(outputDir, `${safeTitle}_segment_info.txt`);
|
||||||
let segmentInfo = `# ${info.title}\n\n`;
|
const segmentInfo = `# ${info.title}\n\n` +
|
||||||
segmentInfo += `Peak segment (stands out from surrounding content):\n`;
|
`Most watched segment (highest intensity from YouTube heatmap):\n` +
|
||||||
segmentInfo += ` Start: ${formatTime(topPeak.start)} (${topPeak.start.toFixed(1)}s)\n`;
|
` Start: ${formatTime(topSegment.start)} (${topSegment.start.toFixed(1)}s)\n` +
|
||||||
segmentInfo += ` End: ${formatTime(topPeak.end)} (${topPeak.end.toFixed(1)}s)\n`;
|
` End: ${formatTime(topSegment.end)} (${topSegment.end.toFixed(1)}s)\n` +
|
||||||
segmentInfo += ` Duration: ${formatTime(topPeak.end - topPeak.start)}\n`;
|
` Duration: ${formatTime(topSegment.end - topSegment.start)}\n` +
|
||||||
segmentInfo += ` Peak Score: ${(topPeak.peakScore * 100).toFixed(1)}%\n`;
|
` Intensity: ${(topSegment.intensity * 100).toFixed(1)}%\n\n` +
|
||||||
segmentInfo += ` Intensity: ${(topPeak.intensity * 100).toFixed(1)}%\n\n`;
|
`Note: This segment had the highest watch intensity (excluding first ${skipStartSeconds}s).\n`;
|
||||||
|
|
||||||
if (peakSegments.length > 1) {
|
|
||||||
segmentInfo += `Other peaks:\n`;
|
|
||||||
for (let i = 1; i < Math.min(peakSegments.length, 5); i++) {
|
|
||||||
const seg = peakSegments[i];
|
|
||||||
segmentInfo += ` ${formatTime(seg.start)} - ${formatTime(seg.end)} (score: ${(seg.peakScore * 100).toFixed(1)}%)\n`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
writeFileSync(segmentInfoPath, segmentInfo);
|
writeFileSync(segmentInfoPath, segmentInfo);
|
||||||
console.log(`\nSegment info saved to: ${segmentInfoPath}`);
|
console.log(`\nSegment 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(" -t, --threshold <n> Peak detection threshold (default: 0.3)");
|
console.log(" -s, --skip-start <sec> Skip first N seconds (default: 30)");
|
||||||
console.log(" -h, --help Show help");
|
console.log(" -h, --help Show help");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`Downloading peak segment from: ${args.url}`);
|
console.log(`Downloading most watched segment 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(`Peak threshold: ${args.peakThreshold}`);
|
console.log(`Skip first ${args.skipStartSeconds} seconds`);
|
||||||
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,
|
||||||
peakThreshold: args.peakThreshold,
|
skipStartSeconds: args.skipStartSeconds,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error:", error instanceof Error ? error.message : error);
|
console.error("Error:", error instanceof Error ? error.message : error);
|
||||||
|
|||||||
Reference in New Issue
Block a user