@thunderkiller/video-clipper 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/.env.example +130 -0
  2. package/.github/workflows/ci.yml +42 -0
  3. package/.github/workflows/release.yml +72 -0
  4. package/.husky/pre-commit +3 -0
  5. package/.prettierignore +6 -0
  6. package/.prettierrc +7 -0
  7. package/.releaserc.json +21 -0
  8. package/AGENTS.md +122 -0
  9. package/CHANGELOG.md +45 -0
  10. package/README.md +410 -0
  11. package/dist/cli.js +187 -0
  12. package/dist/config/env.js +14 -0
  13. package/dist/config/index.js +1 -0
  14. package/dist/index.js +35 -0
  15. package/dist/pipeline/runner.js +132 -0
  16. package/dist/pipeline/stages/audioProcessor.js +75 -0
  17. package/dist/pipeline/stages/clipExporter.js +44 -0
  18. package/dist/pipeline/stages/segmentAnalyzer.js +46 -0
  19. package/dist/pipeline/stages/segmentSelector.js +23 -0
  20. package/dist/pipeline/stages/videoResolver.js +34 -0
  21. package/dist/services/audioAnalyzers/base.js +13 -0
  22. package/dist/services/audioAnalyzers/factory.js +56 -0
  23. package/dist/services/audioAnalyzers/gemini.js +109 -0
  24. package/dist/services/audioAnalyzers/index.js +5 -0
  25. package/dist/services/audioAnalyzers/whisper.js +62 -0
  26. package/dist/services/audioAnalyzers/yamnet.js +40 -0
  27. package/dist/services/audioDownloader/index.js +81 -0
  28. package/dist/services/chunkBuilder/index.js +71 -0
  29. package/dist/services/clipGenerator/index.js +156 -0
  30. package/dist/services/clipRefiner/index.js +103 -0
  31. package/dist/services/eventDetector/index.js +54 -0
  32. package/dist/services/llmAnalyzer/LLMAnalyzer.js +63 -0
  33. package/dist/services/llmAnalyzer/index.js +173 -0
  34. package/dist/services/metadataExtractor/index.js +66 -0
  35. package/dist/services/segmentRanker/index.js +40 -0
  36. package/dist/services/signalMerger/index.js +36 -0
  37. package/dist/services/transcriptAnalyzers/base.js +13 -0
  38. package/dist/services/transcriptAnalyzers/factory.js +51 -0
  39. package/dist/services/transcriptAnalyzers/gemini.js +19 -0
  40. package/dist/services/transcriptAnalyzers/index.js +5 -0
  41. package/dist/services/transcriptAnalyzers/whisper.js +55 -0
  42. package/dist/services/transcriptAnalyzers/ytdlp.js +16 -0
  43. package/dist/services/transcriptDetector/index.js +102 -0
  44. package/dist/services/transcriptFetcher/index.js +124 -0
  45. package/dist/services/urlParser/index.js +46 -0
  46. package/dist/services/videoDownloader/index.js +212 -0
  47. package/dist/types/audio.js +15 -0
  48. package/dist/types/cli.js +1 -0
  49. package/dist/types/config.js +150 -0
  50. package/dist/types/index.js +5 -0
  51. package/dist/types/pipeline.js +9 -0
  52. package/dist/types/segment.js +36 -0
  53. package/dist/types/transcript.js +16 -0
  54. package/dist/types/video.js +14 -0
  55. package/dist/utils/cache.js +143 -0
  56. package/dist/utils/chunker.js +51 -0
  57. package/dist/utils/dumper.js +36 -0
  58. package/dist/utils/format.js +10 -0
  59. package/dist/utils/logger.js +16 -0
  60. package/dist/utils/modelFactory.js +60 -0
  61. package/dist/utils/redactConfig.js +20 -0
  62. package/dist/utils/sliceAudio.js +26 -0
  63. package/docs/free-models.md +78 -0
  64. package/docs/plan.md +442 -0
  65. package/docs/refactorPhases.md +105 -0
  66. package/docs/yt-downloader.md +440 -0
  67. package/package.json +65 -0
  68. package/requirements.txt +5 -0
  69. package/scripts/detect_events.py +81 -0
  70. package/scripts/detect_events_whisper.py +101 -0
  71. package/scripts/transcribe_whisper.py +70 -0
  72. package/src/cli.ts +186 -0
  73. package/src/config/env.ts +18 -0
  74. package/src/config/index.ts +2 -0
  75. package/src/index.ts +46 -0
  76. package/src/pipeline/runner.ts +155 -0
  77. package/src/pipeline/stages/audioProcessor.ts +129 -0
  78. package/src/pipeline/stages/clipExporter.ts +80 -0
  79. package/src/pipeline/stages/segmentAnalyzer.ts +72 -0
  80. package/src/pipeline/stages/segmentSelector.ts +39 -0
  81. package/src/pipeline/stages/videoResolver.ts +47 -0
  82. package/src/services/audioAnalyzers/base.ts +32 -0
  83. package/src/services/audioAnalyzers/factory.ts +71 -0
  84. package/src/services/audioAnalyzers/gemini.ts +137 -0
  85. package/src/services/audioAnalyzers/index.ts +6 -0
  86. package/src/services/audioAnalyzers/whisper.ts +80 -0
  87. package/src/services/audioAnalyzers/yamnet.ts +54 -0
  88. package/src/services/audioDownloader/index.ts +102 -0
  89. package/src/services/chunkBuilder/index.ts +86 -0
  90. package/src/services/clipGenerator/index.ts +210 -0
  91. package/src/services/clipRefiner/index.ts +141 -0
  92. package/src/services/eventDetector/index.ts +68 -0
  93. package/src/services/llmAnalyzer/LLMAnalyzer.ts +114 -0
  94. package/src/services/llmAnalyzer/index.ts +231 -0
  95. package/src/services/metadataExtractor/index.ts +83 -0
  96. package/src/services/segmentRanker/index.ts +88 -0
  97. package/src/services/signalMerger/index.ts +53 -0
  98. package/src/services/transcriptAnalyzers/base.ts +26 -0
  99. package/src/services/transcriptAnalyzers/factory.ts +67 -0
  100. package/src/services/transcriptAnalyzers/gemini.ts +24 -0
  101. package/src/services/transcriptAnalyzers/index.ts +6 -0
  102. package/src/services/transcriptAnalyzers/whisper.ts +68 -0
  103. package/src/services/transcriptAnalyzers/ytdlp.ts +19 -0
  104. package/src/services/transcriptDetector/index.ts +128 -0
  105. package/src/services/transcriptFetcher/index.ts +151 -0
  106. package/src/services/urlParser/index.ts +53 -0
  107. package/src/services/videoDownloader/index.ts +282 -0
  108. package/src/types/audio.ts +19 -0
  109. package/src/types/cli.ts +22 -0
  110. package/src/types/config.ts +174 -0
  111. package/src/types/index.ts +26 -0
  112. package/src/types/pipeline.ts +93 -0
  113. package/src/types/segment.ts +43 -0
  114. package/src/types/transcript.ts +22 -0
  115. package/src/types/video.ts +18 -0
  116. package/src/utils/cache.ts +223 -0
  117. package/src/utils/chunker.ts +60 -0
  118. package/src/utils/dumper.ts +41 -0
  119. package/src/utils/format.ts +10 -0
  120. package/src/utils/logger.ts +17 -0
  121. package/src/utils/modelFactory.ts +71 -0
  122. package/src/utils/redactConfig.ts +23 -0
  123. package/src/utils/sliceAudio.ts +35 -0
  124. package/test-trigger.txt +1 -0
  125. package/tests/analyzerFactory.test.ts +146 -0
  126. package/tests/audioEventDetector.test.ts +69 -0
  127. package/tests/cache.test.ts +203 -0
  128. package/tests/chunkBuilder.test.ts +146 -0
  129. package/tests/chunker.test.ts +95 -0
  130. package/tests/eventDetector.test.ts +103 -0
  131. package/tests/llmAnalyzer.test.ts +283 -0
  132. package/tests/segmentRanker.test.ts +133 -0
  133. package/tests/setup.ts +48 -0
  134. package/tests/signalMerger.test.ts +197 -0
  135. package/tests/transcriptDetector.test.ts +150 -0
  136. package/tests/transcriptFetcher.test.ts +179 -0
  137. package/tests/urlParser.test.ts +70 -0
  138. package/tsconfig.json +16 -0
  139. package/tsconfig.test.json +8 -0
  140. package/vitest.config.ts +8 -0
@@ -0,0 +1,128 @@
1
+ import { buildMicroBlocks, buildLLMChunks } from '../chunkBuilder/index.js';
2
+ import { log } from '../../utils/logger.js';
3
+ import { config } from '../../config/index.js';
4
+ import type { TranscriptAnalyzer } from '../transcriptAnalyzers/index.js';
5
+ import type { Cache } from '../../utils/cache.js';
6
+ import type { TranscriptLine, MicroBlock, LLMChunk } from '../../types/index.js';
7
+
8
+ export interface TranscriptDetectorResult {
9
+ lines: TranscriptLine[];
10
+ microBlocks: MicroBlock[];
11
+ chunks: LLMChunk[];
12
+ }
13
+
14
+ /**
15
+ * Top-level transcript detector.
16
+ *
17
+ * Holds an ordered chain of TranscriptAnalyzer instances and walks the chain
18
+ * on each `detect()` call: the first analyzer that succeeds wins. If an
19
+ * analyzer throws, the error is logged and the next analyzer in the chain is
20
+ * tried. If the entire chain is exhausted without success the error from the
21
+ * last analyzer is re-thrown.
22
+ *
23
+ * After obtaining raw transcript lines the detector groups them into
24
+ * micro-blocks and builds overlapping LLM analysis chunks — keeping the full
25
+ * "transcript concern" self-contained under one class.
26
+ *
27
+ * The chain is built once at startup via `createTranscriptChain(config.TRANSCRIPT_PROVIDER)`
28
+ * and injected here, keeping provider-selection logic out of this class.
29
+ *
30
+ * Results are cached via the injected Cache instance so that repeat runs skip
31
+ * the network round-trip to yt-dlp / Whisper.
32
+ *
33
+ * @example
34
+ * const chain = createTranscriptChain('ytdlp,whisper');
35
+ * const detector = new TranscriptDetector(chain);
36
+ * const { lines, microBlocks, chunks } = await detector.detect(videoId, audioPath, cache);
37
+ */
38
+ export class TranscriptDetector {
39
+ constructor(private readonly chain: TranscriptAnalyzer[]) {
40
+ if (chain.length === 0) {
41
+ throw new Error('TranscriptDetector requires at least one TranscriptAnalyzer in the chain.');
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Fetches, groups, and chunks the transcript for the given video ID.
47
+ *
48
+ * Walks the analyzer chain in order, falling back on error. Cache is checked
49
+ * first (before any analyzer is tried) and written after the first successful
50
+ * fetch so subsequent runs with the same provider config are instant.
51
+ *
52
+ * @param videoId - YouTube video ID
53
+ * @param audioPath - Path to the downloaded WAV, or null if audio is not yet available
54
+ * @param cache - Cache instance for read/write of transcript lines
55
+ */
56
+ async detect(
57
+ videoId: string,
58
+ audioPath: string | null,
59
+ cache: Cache,
60
+ ): Promise<TranscriptDetectorResult> {
61
+ let lines: TranscriptLine[];
62
+
63
+ // Cache-first: if we already have lines on disk, skip the provider chain entirely
64
+ const cached = await cache.readTranscript(videoId);
65
+ if (cached) {
66
+ log.info(`[cache hit] Transcript loaded from cache (${cached.length} lines)`);
67
+ lines = cached;
68
+ } else {
69
+ lines = await this.fetchFromChain(videoId, audioPath);
70
+ await cache.writeTranscript(videoId, lines);
71
+ }
72
+
73
+ const microBlocks = this.buildMicroBlocks(lines);
74
+ const chunks = this.buildChunks(microBlocks);
75
+
76
+ return { lines, microBlocks, chunks };
77
+ }
78
+
79
+ // -------------------------------------------------------------------------
80
+ // Private helpers
81
+ // -------------------------------------------------------------------------
82
+
83
+ /**
84
+ * Walks the analyzer chain in order.
85
+ * Falls back to the next analyzer whenever one throws.
86
+ */
87
+ private async fetchFromChain(
88
+ videoId: string,
89
+ audioPath: string | null,
90
+ ): Promise<TranscriptLine[]> {
91
+ let lastError: unknown;
92
+
93
+ for (let i = 0; i < this.chain.length; i++) {
94
+ const analyzer = this.chain[i];
95
+ const isLast = i === this.chain.length - 1;
96
+
97
+ try {
98
+ const lines = await analyzer.detect(videoId, audioPath);
99
+ log.info(`[transcript:${analyzer.source}] fetched ${lines.length} lines`);
100
+ return lines;
101
+ } catch (err) {
102
+ lastError = err;
103
+ const message = err instanceof Error ? err.message : String(err);
104
+
105
+ if (!isLast) {
106
+ const nextSource = this.chain[i + 1].source;
107
+ log.warn(
108
+ `[transcript:${analyzer.source}] failed, falling back to ${nextSource}: ${message}`,
109
+ );
110
+ } else {
111
+ log.error(`[transcript:${analyzer.source}] failed (no more fallbacks): ${message}`);
112
+ }
113
+ }
114
+ }
115
+
116
+ throw lastError;
117
+ }
118
+
119
+ /** Groups raw transcript lines into micro-blocks. */
120
+ private buildMicroBlocks(lines: TranscriptLine[]): MicroBlock[] {
121
+ return buildMicroBlocks(lines, config.MICRO_BLOCK_SEC);
122
+ }
123
+
124
+ /** Builds overlapping LLM analysis chunks from micro-blocks. */
125
+ private buildChunks(microBlocks: MicroBlock[]): LLMChunk[] {
126
+ return buildLLMChunks(microBlocks, config.CHUNK_LENGTH_SEC, config.CHUNK_OVERLAP_SEC);
127
+ }
128
+ }
@@ -0,0 +1,151 @@
1
+ import { execa } from 'execa';
2
+ import fs from 'node:fs/promises';
3
+ import os from 'node:os';
4
+ import path from 'node:path';
5
+ import { log } from '../../utils/logger.js';
6
+ import { config } from '../../config/index.js';
7
+ import type { TranscriptLine } from '../../types/index.js';
8
+
9
+ /**
10
+ * Parses a WebVTT string into TranscriptLine[].
11
+ *
12
+ * Handles:
13
+ * - `HH:MM:SS.mmm --> HH:MM:SS.mmm` timestamp lines
14
+ * - `<MM:SS.mmm><c>text</c>` inline cue tags (stripped)
15
+ * - Duplicate / empty cues (skipped)
16
+ *
17
+ * Exported for unit testing.
18
+ */
19
+ export function parseVtt(vttContent: string): TranscriptLine[] {
20
+ const lines = vttContent.split(/\r?\n/);
21
+ const result: TranscriptLine[] = [];
22
+
23
+ // Regex: HH:MM:SS.mmm --> HH:MM:SS.mmm (optional positioning metadata after)
24
+ const TIMESTAMP_RE =
25
+ /^(\d{2}):(\d{2}):(\d{2})[.,](\d{3})\s+-->\s+(\d{2}):(\d{2}):(\d{2})[.,](\d{3})/;
26
+
27
+ let i = 0;
28
+ while (i < lines.length) {
29
+ const line = lines[i].trim();
30
+ const match = TIMESTAMP_RE.exec(line);
31
+
32
+ if (match) {
33
+ const startSec =
34
+ parseInt(match[1], 10) * 3600 +
35
+ parseInt(match[2], 10) * 60 +
36
+ parseInt(match[3], 10) +
37
+ parseInt(match[4], 10) / 1000;
38
+
39
+ const endSec =
40
+ parseInt(match[5], 10) * 3600 +
41
+ parseInt(match[6], 10) * 60 +
42
+ parseInt(match[7], 10) +
43
+ parseInt(match[8], 10) / 1000;
44
+
45
+ // Collect cue text lines until blank line or EOF
46
+ i++;
47
+ const textLines: string[] = [];
48
+ while (i < lines.length && lines[i].trim() !== '') {
49
+ textLines.push(lines[i].trim());
50
+ i++;
51
+ }
52
+
53
+ const rawText = textLines.join(' ');
54
+
55
+ // Strip VTT inline tags: <00:00:00.000>, <c>, </c>, <b>, </b>, <i>, </i>, etc.
56
+ const text = rawText
57
+ .replace(/<[^>]+>/g, '')
58
+ .replace(/&amp;/g, '&')
59
+ .replace(/&lt;/g, '<')
60
+ .replace(/&gt;/g, '>')
61
+ .replace(/&nbsp;/g, ' ')
62
+ .replace(/\s+/g, ' ')
63
+ .trim();
64
+
65
+ if (text.length === 0) {
66
+ continue;
67
+ }
68
+
69
+ const duration = Math.max(0, endSec - startSec);
70
+
71
+ // Deduplicate: skip if this cue text is identical to the previous one
72
+ // (YouTube VTT often repeats the same line as text scrolls)
73
+ if (result.length > 0 && result[result.length - 1].text === text) {
74
+ continue;
75
+ }
76
+
77
+ result.push({ text, start: startSec, duration });
78
+ continue;
79
+ }
80
+
81
+ i++;
82
+ }
83
+
84
+ return result;
85
+ }
86
+
87
+ /**
88
+ * Fetches the transcript for a given YouTube video ID using yt-dlp
89
+ * auto-generated subtitles (VTT format).
90
+ *
91
+ * The VTT file is written to a temp directory, parsed into TranscriptLine[],
92
+ * then cleaned up. Cookie config (YT_DLP_COOKIES_FROM_BROWSER /
93
+ * YT_DLP_COOKIES_FILE) is forwarded to yt-dlp automatically.
94
+ *
95
+ * @throws {Error} with the yt-dlp stderr if the command fails
96
+ * @throws {Error} if no subtitle file is produced
97
+ * @throws {Error} if the subtitle file contains no parseable cues
98
+ */
99
+ export async function fetchTranscript(videoId: string): Promise<TranscriptLine[]> {
100
+ const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'vc-vtt-'));
101
+
102
+ try {
103
+ const args = [
104
+ '--write-auto-sub',
105
+ '--sub-format',
106
+ 'vtt',
107
+ '--sub-lang',
108
+ 'en.*',
109
+ '--skip-download',
110
+ '--output',
111
+ path.join(tmpDir, '%(id)s.%(ext)s'),
112
+ `https://www.youtube.com/watch?v=${videoId}`,
113
+ ];
114
+
115
+ if (config.YT_DLP_COOKIES_FROM_BROWSER) {
116
+ args.unshift('--cookies-from-browser', config.YT_DLP_COOKIES_FROM_BROWSER);
117
+ } else if (config.YT_DLP_COOKIES_FILE) {
118
+ args.unshift('--cookies', config.YT_DLP_COOKIES_FILE);
119
+ }
120
+
121
+ try {
122
+ await execa('yt-dlp', args);
123
+ } catch (err) {
124
+ const message = err instanceof Error ? err.message : String(err);
125
+ throw new Error(`yt-dlp failed to fetch subtitles for "${videoId}": ${message}`);
126
+ }
127
+
128
+ // Find the downloaded .vtt file (yt-dlp names it <id>.<lang>.vtt)
129
+ const files = await fs.readdir(tmpDir);
130
+ const vttFile = files.find((f) => f.endsWith('.vtt'));
131
+
132
+ if (!vttFile) {
133
+ throw new Error(
134
+ `No subtitles found for "${videoId}". The video may not have auto-generated captions.`,
135
+ );
136
+ }
137
+
138
+ const content = await fs.readFile(path.join(tmpDir, vttFile), 'utf8');
139
+ const lines = parseVtt(content);
140
+
141
+ log.info(`Parsed ${lines.length} cues from subtitle file "${vttFile}".`);
142
+
143
+ if (lines.length === 0) {
144
+ throw new Error(`Subtitle file for "${videoId}" was empty or contained no parseable cues.`);
145
+ }
146
+
147
+ return lines;
148
+ } finally {
149
+ await fs.rm(tmpDir, { recursive: true, force: true });
150
+ }
151
+ }
@@ -0,0 +1,53 @@
1
+ const VIDEO_ID_LENGTH = 11;
2
+
3
+ /**
4
+ * Parses a YouTube URL and returns the 11-character video ID.
5
+ * Supports:
6
+ * - https://www.youtube.com/watch?v=VIDEO_ID
7
+ * - https://youtu.be/VIDEO_ID
8
+ * - https://www.youtube.com/embed/VIDEO_ID
9
+ * - https://www.youtube.com/shorts/VIDEO_ID
10
+ *
11
+ * @throws {Error} if the URL is not a valid YouTube URL or the video ID is not 11 characters
12
+ */
13
+ export function parseUrl(url: string): string {
14
+ let parsed: URL;
15
+
16
+ try {
17
+ parsed = new URL(url);
18
+ } catch {
19
+ throw new Error(`Invalid URL: "${url}"`);
20
+ }
21
+
22
+ const { hostname, pathname, searchParams } = parsed;
23
+ const host = hostname.replace(/^www\./, '');
24
+
25
+ let videoId: string | null = null;
26
+
27
+ if (host === 'youtube.com') {
28
+ if (pathname === '/watch') {
29
+ videoId = searchParams.get('v');
30
+ } else if (pathname.startsWith('/embed/')) {
31
+ videoId = pathname.split('/embed/')[1]?.split('/')[0] ?? null;
32
+ } else if (pathname.startsWith('/shorts/')) {
33
+ videoId = pathname.split('/shorts/')[1]?.split('/')[0] ?? null;
34
+ }
35
+ } else if (host === 'youtu.be') {
36
+ videoId = pathname.slice(1).split('/')[0] ?? null;
37
+ }
38
+
39
+ if (!videoId) {
40
+ throw new Error(`Could not extract video ID from URL: "${url}"`);
41
+ }
42
+
43
+ // Strip any extra query params that may have been part of the path segment
44
+ videoId = videoId.split('?')[0];
45
+
46
+ if (videoId.length !== VIDEO_ID_LENGTH) {
47
+ throw new Error(
48
+ `Invalid video ID "${videoId}": expected ${VIDEO_ID_LENGTH} characters, got ${videoId.length}`,
49
+ );
50
+ }
51
+
52
+ return videoId;
53
+ }
@@ -0,0 +1,282 @@
1
+ import { execa } from 'execa';
2
+ import { promises as fs } from 'fs';
3
+ import { join } from 'path';
4
+ import pLimit from 'p-limit';
5
+ import { config } from '../../config/index.js';
6
+ import { log } from '../../utils/logger.js';
7
+ import type { RankedSegment } from '../../types/index.js';
8
+
9
+ export type DownloadMode = 'all' | 'segments';
10
+
11
+ export interface DownloadResultAll {
12
+ mode: 'all';
13
+ path: string;
14
+ }
15
+
16
+ export interface DownloadResultSegments {
17
+ mode: 'segments';
18
+ paths: string[];
19
+ }
20
+
21
+ export type DownloadResult = DownloadResultAll | DownloadResultSegments;
22
+
23
+ /**
24
+ * Formats a timestamp for yt-dlp --download-sections.
25
+ * Converts seconds to HH:MM:SS.mmm format with millisecond precision.
26
+ */
27
+ function formatTimestamp(seconds: number): string {
28
+ const h = Math.floor(seconds / 3600);
29
+ const m = Math.floor((seconds % 3600) / 60);
30
+ const s = seconds % 60;
31
+ const sInt = Math.floor(s);
32
+ const ms = Math.round((s - sInt) * 1000);
33
+ return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(sInt).padStart(2, '0')}.${String(ms).padStart(3, '0')}`;
34
+ }
35
+
36
+ /**
37
+ * Displays progress from yt-dlp stdout/stderr.
38
+ */
39
+ function displayProgress(stream: 'stdout' | 'stderr'): (data: Buffer | string) => void {
40
+ return (data: Buffer | string) => {
41
+ const text = String(data);
42
+ const lines = text.split('\n').filter((line) => line.trim());
43
+
44
+ for (const line of lines) {
45
+ const progressMatch = line.match(/\[download\]\s+(\d+\.?\d*%)/);
46
+ if (progressMatch) {
47
+ process.stdout.write(`\r${progressMatch[0]}`);
48
+ }
49
+ }
50
+ };
51
+ }
52
+
53
+ /**
54
+ * Downloads a YouTube video using yt-dlp and returns the local file path.
55
+ *
56
+ * Strategy:
57
+ * - Skips download if the target file already exists.
58
+ * - Auto-creates the download directory if it doesn't exist.
59
+ * - Surfaces clear errors for common failure modes (yt-dlp not installed,
60
+ * private/geo-blocked video, etc.).
61
+ *
62
+ * @param videoId - 11-character YouTube video ID
63
+ * @param customPath - Custom output directory (optional, overrides DOWNLOAD_DIR)
64
+ * @returns Absolute path to the downloaded mp4 file
65
+ * @throws {Error} if yt-dlp is not installed or the download fails
66
+ */
67
+ export async function downloadFullVideo(videoId: string, customPath?: string): Promise<string> {
68
+ const downloadDir = customPath || config.DOWNLOAD_DIR;
69
+ await fs.mkdir(downloadDir, { recursive: true });
70
+
71
+ const outputPath = join(downloadDir, `${videoId}.mp4`);
72
+
73
+ try {
74
+ await fs.access(outputPath);
75
+ log.info(`Video already downloaded: ${outputPath}`);
76
+ return outputPath;
77
+ } catch {}
78
+
79
+ log.info(`Downloading full video ${videoId} via yt-dlp...`);
80
+
81
+ try {
82
+ const args = [
83
+ '-f',
84
+ 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]',
85
+ '--merge-output-format',
86
+ 'mp4',
87
+ '-o',
88
+ outputPath,
89
+ '--no-playlist',
90
+ '--newline',
91
+ `https://www.youtube.com/watch?v=${videoId}`,
92
+ ];
93
+
94
+ if (config.YT_DLP_COOKIES_FROM_BROWSER) {
95
+ args.splice(0, 0, '--cookies-from-browser', config.YT_DLP_COOKIES_FROM_BROWSER);
96
+ } else if (config.YT_DLP_COOKIES_FILE) {
97
+ args.splice(0, 0, '--cookies', config.YT_DLP_COOKIES_FILE);
98
+ }
99
+
100
+ const subprocess = execa('yt-dlp', args);
101
+
102
+ subprocess.stdout?.on('data', displayProgress('stdout'));
103
+ subprocess.stderr?.on('data', displayProgress('stderr'));
104
+
105
+ await subprocess;
106
+ process.stdout.write('\n');
107
+ } catch (err) {
108
+ const message = err instanceof Error ? err.message : String(err);
109
+
110
+ if (message.includes('command not found') || message.includes('ENOENT')) {
111
+ throw new Error('yt-dlp is required. Install it: https://github.com/yt-dlp/yt-dlp');
112
+ }
113
+
114
+ if (message.includes('Private video') || message.includes('Sign in')) {
115
+ throw new Error(`Video "${videoId}" is private and cannot be downloaded.`);
116
+ }
117
+
118
+ if (message.includes('not available in your country') || message.includes('geo')) {
119
+ throw new Error(`Video "${videoId}" is geo-blocked in your region.`);
120
+ }
121
+
122
+ throw new Error(`Download failed: ${message}`);
123
+ }
124
+
125
+ log.info(`Download complete: ${outputPath}`);
126
+ return outputPath;
127
+ }
128
+
129
+ /**
130
+ * Downloads a single segment using yt-dlp --download-sections.
131
+ */
132
+ async function downloadSegment(
133
+ videoId: string,
134
+ segment: RankedSegment,
135
+ index: number,
136
+ customPath?: string,
137
+ ): Promise<string> {
138
+ const downloadDir = customPath || config.DOWNLOAD_DIR;
139
+ await fs.mkdir(downloadDir, { recursive: true });
140
+
141
+ const adjustedStart = Math.max(0, segment.start + config.TIMESTAMP_OFFSET_SECONDS);
142
+ const adjustedEnd = Math.max(adjustedStart + 1, segment.end + config.TIMESTAMP_OFFSET_SECONDS);
143
+ const startInt = Math.floor(adjustedStart);
144
+ const endInt = Math.ceil(adjustedEnd);
145
+ const outputPath = join(downloadDir, `${videoId}_${startInt}_${endInt}.mp4`);
146
+
147
+ try {
148
+ await fs.access(outputPath);
149
+ log.info(`Segment ${index + 1}/${index} already downloaded: ${outputPath}`);
150
+ return outputPath;
151
+ } catch {}
152
+
153
+ const startTs = formatTimestamp(adjustedStart);
154
+ const endTs = formatTimestamp(adjustedEnd);
155
+
156
+ log.info(`Downloading segment ${index + 1}: ${startTs} - ${endTs} (${segment.reason})`);
157
+ log.info(` Requested: ${segment.start.toFixed(2)}s - ${segment.end.toFixed(2)}s`);
158
+ if (config.TIMESTAMP_OFFSET_SECONDS !== 0) {
159
+ log.info(
160
+ ` Adjusted: ${adjustedStart.toFixed(2)}s - ${adjustedEnd.toFixed(2)}s (offset: ${config.TIMESTAMP_OFFSET_SECONDS}s)`,
161
+ );
162
+ }
163
+
164
+ try {
165
+ const args = [
166
+ '-f',
167
+ 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]',
168
+ '--merge-output-format',
169
+ 'mp4',
170
+ '--download-sections',
171
+ `*${startTs}-${endTs}`,
172
+ '-o',
173
+ outputPath,
174
+ '--no-playlist',
175
+ '--newline',
176
+ `https://www.youtube.com/watch?v=${videoId}`,
177
+ ];
178
+
179
+ if (config.YT_DLP_COOKIES_FROM_BROWSER) {
180
+ args.splice(0, 0, '--cookies-from-browser', config.YT_DLP_COOKIES_FROM_BROWSER);
181
+ } else if (config.YT_DLP_COOKIES_FILE) {
182
+ args.splice(0, 0, '--cookies', config.YT_DLP_COOKIES_FILE);
183
+ }
184
+
185
+ const subprocess = execa('yt-dlp', args);
186
+
187
+ subprocess.stdout?.on('data', displayProgress('stdout'));
188
+ subprocess.stderr?.on('data', displayProgress('stderr'));
189
+
190
+ await subprocess;
191
+ process.stdout.write('\n');
192
+ } catch (err) {
193
+ const message = err instanceof Error ? err.message : String(err);
194
+
195
+ if (message.includes('command not found') || message.includes('ENOENT')) {
196
+ throw new Error('yt-dlp is required. Install it: https://github.com/yt-dlp/yt-dlp');
197
+ }
198
+
199
+ if (message.includes('Private video') || message.includes('Sign in')) {
200
+ throw new Error(`Video "${videoId}" is private and cannot be downloaded.`);
201
+ }
202
+
203
+ if (message.includes('not available in your country') || message.includes('geo')) {
204
+ throw new Error(`Video "${videoId}" is geo-blocked in your region.`);
205
+ }
206
+
207
+ throw new Error(`Segment download failed: ${message}`);
208
+ }
209
+
210
+ log.info(`Segment complete: ${outputPath}`);
211
+ return outputPath;
212
+ }
213
+
214
+ /**
215
+ * Downloads multiple segments in parallel.
216
+ */
217
+ async function downloadSegments(
218
+ videoId: string,
219
+ segments: RankedSegment[],
220
+ customPath?: string,
221
+ ): Promise<string[]> {
222
+ if (segments.length === 0) {
223
+ return [];
224
+ }
225
+
226
+ const limit = pLimit(Math.min(config.LLM_CONCURRENCY, 3));
227
+ const results: Array<PromiseSettledResult<string>> = await Promise.allSettled(
228
+ segments.map((segment, index) =>
229
+ limit(() => downloadSegment(videoId, segment, index, customPath)),
230
+ ),
231
+ );
232
+
233
+ const paths: string[] = [];
234
+ for (let i = 0; i < results.length; i++) {
235
+ const result = results[i];
236
+ const segment = segments[i];
237
+ if (result.status === 'fulfilled') {
238
+ paths.push(result.value);
239
+ } else {
240
+ const reason = result.reason instanceof Error ? result.reason.message : String(result.reason);
241
+ log.warn(
242
+ `Failed to download segment [${formatTimestamp(segment.start)} – ${formatTimestamp(segment.end)}] (rank ${segment.rank}): ${reason}`,
243
+ );
244
+ }
245
+ }
246
+
247
+ return paths;
248
+ }
249
+
250
+ /**
251
+ * Downloads a YouTube video based on the specified mode.
252
+ *
253
+ * @param videoId - 11-character YouTube video ID
254
+ * @param mode - Download mode: 'all' (full video) or 'segments' (individual clips)
255
+ * @param segments - Ranked segments (required when mode is 'segments')
256
+ * @param customPath - Custom output directory (optional, overrides config defaults)
257
+ * @returns Download result containing the mode and either path or paths
258
+ */
259
+ export async function downloadVideo(
260
+ videoId: string,
261
+ mode: DownloadMode = 'all',
262
+ segments: RankedSegment[] = [],
263
+ customPath?: string,
264
+ ): Promise<DownloadResult> {
265
+ if (mode === 'all') {
266
+ const path = await downloadFullVideo(videoId, customPath);
267
+ return { mode: 'all', path };
268
+ }
269
+
270
+ if (mode === 'segments') {
271
+ if (segments.length === 0) {
272
+ log.warn('No segments provided for download-segments mode. Skipping download.');
273
+ return { mode: 'segments', paths: [] };
274
+ }
275
+
276
+ log.info(`Downloading ${segments.length} segments in parallel...`);
277
+ const paths = await downloadSegments(videoId, segments, customPath);
278
+ return { mode: 'segments', paths };
279
+ }
280
+
281
+ throw new Error(`Invalid download mode: ${mode}`);
282
+ }
@@ -0,0 +1,19 @@
1
+ import { z } from 'zod';
2
+
3
+ export const AudioEventSchema = z.object({
4
+ time: z.number(),
5
+ event: z.string(),
6
+ confidence: z.number().min(0).max(1),
7
+ source: z.enum(['gemini', 'yamnet', 'whisper']),
8
+ });
9
+ export type AudioEvent = z.infer<typeof AudioEventSchema>;
10
+
11
+ export const MergedCandidateSchema = z.object({
12
+ start: z.number(),
13
+ end: z.number(),
14
+ score: z.number().min(1).max(10),
15
+ source: z.enum(['transcript', 'audio', 'both']),
16
+ reason: z.string(),
17
+ audio_event: z.string().optional(),
18
+ });
19
+ export type MergedCandidate = z.infer<typeof MergedCandidateSchema>;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Parsed CLI argument shape.
3
+ * Defined here so both `src/cli.ts` (which creates it) and
4
+ * `src/pipeline/runner.ts` (which consumes it) share a single source of truth.
5
+ */
6
+ export interface CliArgs {
7
+ url: string | undefined;
8
+ clip: boolean;
9
+ downloadSections: 'all' | number | undefined;
10
+ localVideo?: string;
11
+ videoPath: string | undefined;
12
+ threshold: number | undefined;
13
+ topN: number | undefined;
14
+ maxDuration: number | undefined;
15
+ maxChunks: number | undefined;
16
+ maxParallel: number | undefined;
17
+ outputJson: string | undefined;
18
+ noCache: boolean;
19
+ noAudio: boolean;
20
+ gameProfile?: string;
21
+ help: boolean;
22
+ }