pi-web-access 0.5.1 → 0.7.1
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.
- package/CHANGELOG.md +59 -1
- package/README.md +140 -25
- package/chrome-cookies.ts +240 -0
- package/extract.ts +266 -27
- package/gemini-api.ts +103 -0
- package/gemini-search.ts +236 -0
- package/gemini-url-context.ts +119 -0
- package/gemini-web.ts +296 -0
- package/index.ts +112 -22
- package/package.json +29 -4
- package/perplexity.ts +7 -2
- package/pi-web-fetch-demo.mp4 +0 -0
- package/rsc-extract.ts +1 -1
- package/skills/librarian/SKILL.md +40 -0
- package/utils.ts +44 -0
- package/video-extract.ts +329 -0
- package/youtube-extract.ts +280 -0
package/video-extract.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { resolve, extname, basename, join, dirname } from "node:path";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { activityMonitor } from "./activity.js";
|
|
6
|
+
import { isGeminiWebAvailable, queryWithCookies } from "./gemini-web.js";
|
|
7
|
+
import { queryGeminiApiWithVideo, getApiKey, API_BASE } from "./gemini-api.js";
|
|
8
|
+
import { extractHeadingTitle, type ExtractedContent, type ExtractOptions, type FrameResult } from "./extract.js";
|
|
9
|
+
import { readExecError, trimErrorText, mapFfmpegError } from "./utils.js";
|
|
10
|
+
|
|
11
|
+
const CONFIG_PATH = join(homedir(), ".pi", "web-search.json");
|
|
12
|
+
const UPLOAD_BASE = "https://generativelanguage.googleapis.com/upload/v1beta";
|
|
13
|
+
|
|
14
|
+
const DEFAULT_VIDEO_PROMPT = `Extract the complete content of this video. Include:
|
|
15
|
+
1. Video title (infer from content if not explicit), duration
|
|
16
|
+
2. A brief summary (2-3 sentences)
|
|
17
|
+
3. Full transcript with timestamps
|
|
18
|
+
4. Descriptions of any code, terminal commands, diagrams, slides, or UI shown on screen
|
|
19
|
+
|
|
20
|
+
Format as markdown.`;
|
|
21
|
+
|
|
22
|
+
const VIDEO_EXTENSIONS: Record<string, string> = {
|
|
23
|
+
".mp4": "video/mp4",
|
|
24
|
+
".mov": "video/quicktime",
|
|
25
|
+
".webm": "video/webm",
|
|
26
|
+
".avi": "video/x-msvideo",
|
|
27
|
+
".mpeg": "video/mpeg",
|
|
28
|
+
".mpg": "video/mpeg",
|
|
29
|
+
".wmv": "video/x-ms-wmv",
|
|
30
|
+
".flv": "video/x-flv",
|
|
31
|
+
".3gp": "video/3gpp",
|
|
32
|
+
".3gpp": "video/3gpp",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
interface VideoFileInfo {
|
|
36
|
+
absolutePath: string;
|
|
37
|
+
mimeType: string;
|
|
38
|
+
sizeBytes: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface VideoConfig {
|
|
42
|
+
enabled: boolean;
|
|
43
|
+
preferredModel: string;
|
|
44
|
+
maxSizeMB: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const VIDEO_CONFIG_DEFAULTS: VideoConfig = {
|
|
48
|
+
enabled: true,
|
|
49
|
+
preferredModel: "gemini-2.5-flash",
|
|
50
|
+
maxSizeMB: 50,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
let cachedVideoConfig: VideoConfig | null = null;
|
|
54
|
+
|
|
55
|
+
function loadVideoConfig(): VideoConfig {
|
|
56
|
+
if (cachedVideoConfig) return cachedVideoConfig;
|
|
57
|
+
try {
|
|
58
|
+
if (existsSync(CONFIG_PATH)) {
|
|
59
|
+
const raw = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
|
|
60
|
+
const v = raw.video ?? {};
|
|
61
|
+
cachedVideoConfig = {
|
|
62
|
+
enabled: v.enabled ?? VIDEO_CONFIG_DEFAULTS.enabled,
|
|
63
|
+
preferredModel: v.preferredModel ?? VIDEO_CONFIG_DEFAULTS.preferredModel,
|
|
64
|
+
maxSizeMB: v.maxSizeMB ?? VIDEO_CONFIG_DEFAULTS.maxSizeMB,
|
|
65
|
+
};
|
|
66
|
+
return cachedVideoConfig;
|
|
67
|
+
}
|
|
68
|
+
} catch {}
|
|
69
|
+
cachedVideoConfig = { ...VIDEO_CONFIG_DEFAULTS };
|
|
70
|
+
return cachedVideoConfig;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function isVideoFile(input: string): VideoFileInfo | null {
|
|
74
|
+
const config = loadVideoConfig();
|
|
75
|
+
if (!config.enabled) return null;
|
|
76
|
+
|
|
77
|
+
const isFilePath = input.startsWith("/") || input.startsWith("./") || input.startsWith("../") || input.startsWith("file://");
|
|
78
|
+
if (!isFilePath) return null;
|
|
79
|
+
|
|
80
|
+
const filePath = input.startsWith("file://") ? new URL(input).pathname : input;
|
|
81
|
+
|
|
82
|
+
const ext = extname(filePath).toLowerCase();
|
|
83
|
+
const mimeType = VIDEO_EXTENSIONS[ext];
|
|
84
|
+
if (!mimeType) return null;
|
|
85
|
+
|
|
86
|
+
const absolutePath = resolveFilePath(filePath);
|
|
87
|
+
if (!absolutePath) return null;
|
|
88
|
+
|
|
89
|
+
const stat = statSync(absolutePath);
|
|
90
|
+
if (!stat.isFile()) return null;
|
|
91
|
+
|
|
92
|
+
const maxBytes = config.maxSizeMB * 1024 * 1024;
|
|
93
|
+
if (stat.size > maxBytes) return null;
|
|
94
|
+
|
|
95
|
+
return { absolutePath, mimeType, sizeBytes: stat.size };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function resolveFilePath(filePath: string): string | null {
|
|
99
|
+
const absolutePath = resolve(filePath);
|
|
100
|
+
if (existsSync(absolutePath)) return absolutePath;
|
|
101
|
+
|
|
102
|
+
const dir = dirname(absolutePath);
|
|
103
|
+
const base = basename(absolutePath);
|
|
104
|
+
if (!existsSync(dir)) return null;
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
const normalizedBase = normalizeSpaces(base);
|
|
108
|
+
const match = readdirSync(dir).find(f => normalizeSpaces(f) === normalizedBase);
|
|
109
|
+
return match ? join(dir, match) : null;
|
|
110
|
+
} catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function normalizeSpaces(s: string): string {
|
|
116
|
+
return s.replace(/[\u00A0\u2000-\u200B\u202F\u205F\u3000\uFEFF]/g, " ");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function extractVideo(
|
|
120
|
+
info: VideoFileInfo,
|
|
121
|
+
signal?: AbortSignal,
|
|
122
|
+
options?: ExtractOptions,
|
|
123
|
+
): Promise<ExtractedContent | null> {
|
|
124
|
+
const config = loadVideoConfig();
|
|
125
|
+
const effectivePrompt = options?.prompt ?? DEFAULT_VIDEO_PROMPT;
|
|
126
|
+
const displayName = basename(info.absolutePath);
|
|
127
|
+
const activityId = activityMonitor.logStart({ type: "fetch", url: `video:${displayName}` });
|
|
128
|
+
|
|
129
|
+
const result = await tryVideoGeminiApi(info, effectivePrompt, config, signal)
|
|
130
|
+
?? await tryVideoGeminiWeb(info, effectivePrompt, config, signal);
|
|
131
|
+
|
|
132
|
+
if (result) {
|
|
133
|
+
const thumbnail = await extractVideoFrame(info.absolutePath);
|
|
134
|
+
if (!("error" in thumbnail)) {
|
|
135
|
+
result.thumbnail = thumbnail;
|
|
136
|
+
}
|
|
137
|
+
activityMonitor.logComplete(activityId, 200);
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
activityMonitor.logError(activityId, "all video extraction paths failed");
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function mapFfprobeError(err: unknown): string {
|
|
146
|
+
const { code, stderr, message } = readExecError(err);
|
|
147
|
+
if (code === "ENOENT") return "ffprobe is not installed. Install ffmpeg which includes ffprobe";
|
|
148
|
+
const snippet = trimErrorText(stderr || message);
|
|
149
|
+
return snippet ? `ffprobe failed: ${snippet}` : "ffprobe failed";
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export async function extractVideoFrame(filePath: string, seconds: number = 1): Promise<FrameResult> {
|
|
153
|
+
try {
|
|
154
|
+
const { execFileSync } = await import("node:child_process");
|
|
155
|
+
const buffer = execFileSync("ffmpeg", [
|
|
156
|
+
"-ss", String(seconds), "-i", filePath,
|
|
157
|
+
"-frames:v", "1", "-f", "image2pipe", "-vcodec", "mjpeg", "pipe:1",
|
|
158
|
+
], { maxBuffer: 5 * 1024 * 1024, timeout: 10000, stdio: ["pipe", "pipe", "pipe"] });
|
|
159
|
+
if (buffer.length === 0) return { error: "ffmpeg failed: empty output" };
|
|
160
|
+
return { data: buffer.toString("base64"), mimeType: "image/jpeg" };
|
|
161
|
+
} catch (err) {
|
|
162
|
+
return { error: mapFfmpegError(err) };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export async function getLocalVideoDuration(filePath: string): Promise<number | { error: string }> {
|
|
167
|
+
try {
|
|
168
|
+
const { execFileSync } = await import("node:child_process");
|
|
169
|
+
const output = execFileSync("ffprobe", [
|
|
170
|
+
"-v", "quiet",
|
|
171
|
+
"-show_entries", "format=duration",
|
|
172
|
+
"-of", "csv=p=0",
|
|
173
|
+
filePath,
|
|
174
|
+
], { timeout: 10000, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }).trim();
|
|
175
|
+
const duration = Number.parseFloat(output);
|
|
176
|
+
if (!Number.isFinite(duration)) return { error: "ffprobe failed: invalid duration output" };
|
|
177
|
+
return duration;
|
|
178
|
+
} catch (err) {
|
|
179
|
+
return { error: mapFfprobeError(err) };
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function tryVideoGeminiWeb(
|
|
184
|
+
info: VideoFileInfo,
|
|
185
|
+
prompt: string,
|
|
186
|
+
config: VideoConfig,
|
|
187
|
+
signal?: AbortSignal,
|
|
188
|
+
): Promise<ExtractedContent | null> {
|
|
189
|
+
try {
|
|
190
|
+
const cookies = await isGeminiWebAvailable();
|
|
191
|
+
if (!cookies) return null;
|
|
192
|
+
if (signal?.aborted) return null;
|
|
193
|
+
|
|
194
|
+
const text = await queryWithCookies(prompt, cookies, {
|
|
195
|
+
files: [info.absolutePath],
|
|
196
|
+
model: config.preferredModel,
|
|
197
|
+
signal,
|
|
198
|
+
timeoutMs: 180000,
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
url: info.absolutePath,
|
|
203
|
+
title: extractVideoTitle(text, info.absolutePath),
|
|
204
|
+
content: text,
|
|
205
|
+
error: null,
|
|
206
|
+
};
|
|
207
|
+
} catch {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function tryVideoGeminiApi(
|
|
213
|
+
info: VideoFileInfo,
|
|
214
|
+
prompt: string,
|
|
215
|
+
config: VideoConfig,
|
|
216
|
+
signal?: AbortSignal,
|
|
217
|
+
): Promise<ExtractedContent | null> {
|
|
218
|
+
const apiKey = getApiKey();
|
|
219
|
+
if (!apiKey) return null;
|
|
220
|
+
if (signal?.aborted) return null;
|
|
221
|
+
|
|
222
|
+
let fileName: string | null = null;
|
|
223
|
+
try {
|
|
224
|
+
const uploaded = await uploadToFilesApi(info, apiKey, signal);
|
|
225
|
+
fileName = uploaded.name;
|
|
226
|
+
|
|
227
|
+
await pollFileState(fileName, apiKey, signal, 120000);
|
|
228
|
+
|
|
229
|
+
const text = await queryGeminiApiWithVideo(prompt, uploaded.uri, {
|
|
230
|
+
model: config.preferredModel,
|
|
231
|
+
mimeType: info.mimeType,
|
|
232
|
+
signal,
|
|
233
|
+
timeoutMs: 120000,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
url: info.absolutePath,
|
|
238
|
+
title: extractVideoTitle(text, info.absolutePath),
|
|
239
|
+
content: text,
|
|
240
|
+
error: null,
|
|
241
|
+
};
|
|
242
|
+
} catch {
|
|
243
|
+
return null;
|
|
244
|
+
} finally {
|
|
245
|
+
if (fileName) deleteGeminiFile(fileName, apiKey);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async function uploadToFilesApi(
|
|
250
|
+
info: VideoFileInfo,
|
|
251
|
+
apiKey: string,
|
|
252
|
+
signal?: AbortSignal,
|
|
253
|
+
): Promise<{ name: string; uri: string }> {
|
|
254
|
+
const displayName = basename(info.absolutePath);
|
|
255
|
+
|
|
256
|
+
const initRes = await fetch(`${UPLOAD_BASE}/files`, {
|
|
257
|
+
method: "POST",
|
|
258
|
+
headers: {
|
|
259
|
+
"x-goog-api-key": apiKey,
|
|
260
|
+
"X-Goog-Upload-Protocol": "resumable",
|
|
261
|
+
"X-Goog-Upload-Command": "start",
|
|
262
|
+
"X-Goog-Upload-Header-Content-Length": String(info.sizeBytes),
|
|
263
|
+
"X-Goog-Upload-Header-Content-Type": info.mimeType,
|
|
264
|
+
"Content-Type": "application/json",
|
|
265
|
+
},
|
|
266
|
+
body: JSON.stringify({ file: { display_name: displayName } }),
|
|
267
|
+
signal,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
if (!initRes.ok) {
|
|
271
|
+
const text = await initRes.text();
|
|
272
|
+
throw new Error(`File upload init failed: ${initRes.status} (${text.slice(0, 200)})`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const uploadUrl = initRes.headers.get("x-goog-upload-url");
|
|
276
|
+
if (!uploadUrl) throw new Error("No upload URL in response headers");
|
|
277
|
+
|
|
278
|
+
const fileData = await readFile(info.absolutePath);
|
|
279
|
+
const uploadRes = await fetch(uploadUrl, {
|
|
280
|
+
method: "PUT",
|
|
281
|
+
headers: {
|
|
282
|
+
"Content-Length": String(info.sizeBytes),
|
|
283
|
+
"X-Goog-Upload-Offset": "0",
|
|
284
|
+
"X-Goog-Upload-Command": "upload, finalize",
|
|
285
|
+
},
|
|
286
|
+
body: fileData,
|
|
287
|
+
signal,
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
if (!uploadRes.ok) {
|
|
291
|
+
const text = await uploadRes.text();
|
|
292
|
+
throw new Error(`File upload failed: ${uploadRes.status} (${text.slice(0, 200)})`);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const result = await uploadRes.json() as { file: { name: string; uri: string } };
|
|
296
|
+
return result.file;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async function pollFileState(
|
|
300
|
+
fileName: string,
|
|
301
|
+
apiKey: string,
|
|
302
|
+
signal?: AbortSignal,
|
|
303
|
+
timeoutMs: number = 120000,
|
|
304
|
+
): Promise<void> {
|
|
305
|
+
const deadline = Date.now() + timeoutMs;
|
|
306
|
+
|
|
307
|
+
while (Date.now() < deadline) {
|
|
308
|
+
if (signal?.aborted) throw new Error("Aborted");
|
|
309
|
+
|
|
310
|
+
const res = await fetch(`${API_BASE}/${fileName}?key=${apiKey}`, { signal });
|
|
311
|
+
if (!res.ok) throw new Error(`File state check failed: ${res.status}`);
|
|
312
|
+
|
|
313
|
+
const data = await res.json() as { state: string };
|
|
314
|
+
if (data.state === "ACTIVE") return;
|
|
315
|
+
if (data.state === "FAILED") throw new Error("File processing failed");
|
|
316
|
+
|
|
317
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
throw new Error("File processing timed out");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function deleteGeminiFile(fileName: string, apiKey: string): void {
|
|
324
|
+
fetch(`${API_BASE}/${fileName}?key=${apiKey}`, { method: "DELETE" }).catch(() => {});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function extractVideoTitle(text: string, filePath: string): string {
|
|
328
|
+
return extractHeadingTitle(text) ?? basename(filePath, extname(filePath));
|
|
329
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { activityMonitor } from "./activity.js";
|
|
5
|
+
import { isGeminiWebAvailable, queryWithCookies } from "./gemini-web.js";
|
|
6
|
+
import { isGeminiApiAvailable, queryGeminiApiWithVideo } from "./gemini-api.js";
|
|
7
|
+
import { searchWithPerplexity } from "./perplexity.js";
|
|
8
|
+
import { extractHeadingTitle, type ExtractedContent, type FrameResult, type VideoFrame } from "./extract.js";
|
|
9
|
+
import { formatSeconds, readExecError, isTimeoutError, trimErrorText, mapFfmpegError } from "./utils.js";
|
|
10
|
+
|
|
11
|
+
const CONFIG_PATH = join(homedir(), ".pi", "web-search.json");
|
|
12
|
+
|
|
13
|
+
const YOUTUBE_PROMPT = `Extract the complete content of this YouTube video. Include:
|
|
14
|
+
1. Video title, channel name, and duration
|
|
15
|
+
2. A brief summary (2-3 sentences)
|
|
16
|
+
3. Full transcript with timestamps
|
|
17
|
+
4. Descriptions of any code, terminal commands, diagrams, slides, or UI shown on screen
|
|
18
|
+
|
|
19
|
+
Format as markdown.`;
|
|
20
|
+
|
|
21
|
+
const YOUTUBE_REGEX =
|
|
22
|
+
/(?:(?:www\.|m\.)?youtube\.com\/(?:watch\?.*v=|shorts\/|live\/|embed\/|v\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
|
|
23
|
+
|
|
24
|
+
interface YouTubeConfig {
|
|
25
|
+
enabled: boolean;
|
|
26
|
+
preferredModel: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const defaults: YouTubeConfig = { enabled: true, preferredModel: "gemini-2.5-flash" };
|
|
30
|
+
let cachedConfig: YouTubeConfig | null = null;
|
|
31
|
+
|
|
32
|
+
function loadYouTubeConfig(): YouTubeConfig {
|
|
33
|
+
if (cachedConfig) return cachedConfig;
|
|
34
|
+
try {
|
|
35
|
+
if (existsSync(CONFIG_PATH)) {
|
|
36
|
+
const raw = JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
|
|
37
|
+
const yt = raw.youtube ?? {};
|
|
38
|
+
cachedConfig = {
|
|
39
|
+
enabled: yt.enabled ?? defaults.enabled,
|
|
40
|
+
preferredModel: yt.preferredModel ?? defaults.preferredModel,
|
|
41
|
+
};
|
|
42
|
+
return cachedConfig;
|
|
43
|
+
}
|
|
44
|
+
} catch {}
|
|
45
|
+
cachedConfig = { ...defaults };
|
|
46
|
+
return cachedConfig;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function isYouTubeURL(url: string): { isYouTube: boolean; videoId: string | null } {
|
|
50
|
+
try {
|
|
51
|
+
const parsed = new URL(url);
|
|
52
|
+
if (parsed.pathname === "/playlist") {
|
|
53
|
+
return { isYouTube: false, videoId: null };
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
return { isYouTube: false, videoId: null };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const match = url.match(YOUTUBE_REGEX);
|
|
60
|
+
if (!match) return { isYouTube: false, videoId: null };
|
|
61
|
+
return { isYouTube: true, videoId: match[1] };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function isYouTubeEnabled(): boolean {
|
|
65
|
+
return loadYouTubeConfig().enabled;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function extractYouTube(
|
|
69
|
+
url: string,
|
|
70
|
+
signal?: AbortSignal,
|
|
71
|
+
prompt?: string,
|
|
72
|
+
): Promise<ExtractedContent | null> {
|
|
73
|
+
const config = loadYouTubeConfig();
|
|
74
|
+
const { videoId } = isYouTubeURL(url);
|
|
75
|
+
const canonicalUrl = videoId
|
|
76
|
+
? `https://www.youtube.com/watch?v=${videoId}`
|
|
77
|
+
: url;
|
|
78
|
+
const effectivePrompt = prompt ?? YOUTUBE_PROMPT;
|
|
79
|
+
|
|
80
|
+
const activityId = activityMonitor.logStart({ type: "fetch", url: `youtube.com/${videoId ?? "video"}` });
|
|
81
|
+
|
|
82
|
+
const result = await tryGeminiWeb(canonicalUrl, effectivePrompt, config, signal)
|
|
83
|
+
?? await tryGeminiApi(canonicalUrl, effectivePrompt, config, signal)
|
|
84
|
+
?? await tryPerplexity(url, effectivePrompt, signal);
|
|
85
|
+
|
|
86
|
+
if (result) {
|
|
87
|
+
result.url = url;
|
|
88
|
+
if (videoId) {
|
|
89
|
+
const thumb = await fetchYouTubeThumbnail(videoId);
|
|
90
|
+
if (thumb) result.thumbnail = thumb;
|
|
91
|
+
}
|
|
92
|
+
activityMonitor.logComplete(activityId, 200);
|
|
93
|
+
return result;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
activityMonitor.logError(activityId, "all extraction paths failed");
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
type StreamInfo = { streamUrl: string; duration: number | null };
|
|
101
|
+
type StreamResult = StreamInfo | { error: string };
|
|
102
|
+
|
|
103
|
+
function mapYtDlpError(err: unknown): string {
|
|
104
|
+
const { code, stderr, message } = readExecError(err);
|
|
105
|
+
if (code === "ENOENT") return "yt-dlp is not installed. Install with: brew install yt-dlp";
|
|
106
|
+
if (isTimeoutError(err)) return "yt-dlp timed out fetching video info";
|
|
107
|
+
const lower = stderr.toLowerCase();
|
|
108
|
+
if (lower.includes("private")) return "Video is private or unavailable";
|
|
109
|
+
if (lower.includes("sign in")) return "Video is age-restricted and requires authentication";
|
|
110
|
+
if (lower.includes("not available")) return "Video is unavailable in your region or has been removed";
|
|
111
|
+
if (lower.includes("live")) return "Cannot extract frames from a live stream";
|
|
112
|
+
const snippet = trimErrorText(stderr || message);
|
|
113
|
+
return snippet ? `yt-dlp failed: ${snippet}` : "yt-dlp failed";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function getYouTubeStreamInfo(videoId: string): Promise<StreamResult> {
|
|
117
|
+
try {
|
|
118
|
+
const { execFileSync } = await import("node:child_process");
|
|
119
|
+
const output = execFileSync("yt-dlp", [
|
|
120
|
+
"--print", "duration",
|
|
121
|
+
"-g", `https://www.youtube.com/watch?v=${videoId}`,
|
|
122
|
+
], { timeout: 15000, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }).trim();
|
|
123
|
+
const lines = output.split(/\r?\n/);
|
|
124
|
+
const rawDuration = lines[0]?.trim();
|
|
125
|
+
const streamUrl = lines[1]?.trim();
|
|
126
|
+
if (!streamUrl) return { error: "yt-dlp failed: missing stream URL" };
|
|
127
|
+
const parsedDuration = rawDuration && rawDuration !== "NA" ? Number.parseFloat(rawDuration) : NaN;
|
|
128
|
+
const duration = Number.isFinite(parsedDuration) ? parsedDuration : null;
|
|
129
|
+
return { streamUrl, duration };
|
|
130
|
+
} catch (err) {
|
|
131
|
+
return { error: mapYtDlpError(err) };
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function extractFrameFromStream(streamUrl: string, seconds: number): Promise<FrameResult> {
|
|
136
|
+
try {
|
|
137
|
+
const { execFileSync } = await import("node:child_process");
|
|
138
|
+
const buffer = execFileSync("ffmpeg", [
|
|
139
|
+
"-ss", String(seconds), "-i", streamUrl,
|
|
140
|
+
"-frames:v", "1", "-f", "image2pipe", "-vcodec", "mjpeg", "pipe:1",
|
|
141
|
+
], { maxBuffer: 5 * 1024 * 1024, timeout: 30000, stdio: ["pipe", "pipe", "pipe"] });
|
|
142
|
+
if (buffer.length === 0) return { error: "ffmpeg failed: empty output" };
|
|
143
|
+
return { data: buffer.toString("base64"), mimeType: "image/jpeg" };
|
|
144
|
+
} catch (err) {
|
|
145
|
+
return { error: mapFfmpegError(err) };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function extractYouTubeFrame(
|
|
150
|
+
videoId: string,
|
|
151
|
+
seconds: number,
|
|
152
|
+
streamInfo?: StreamInfo,
|
|
153
|
+
): Promise<FrameResult> {
|
|
154
|
+
const info = streamInfo ?? await getYouTubeStreamInfo(videoId);
|
|
155
|
+
if ("error" in info) return info;
|
|
156
|
+
return extractFrameFromStream(info.streamUrl, seconds);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function extractYouTubeFrames(
|
|
160
|
+
videoId: string,
|
|
161
|
+
timestamps: number[],
|
|
162
|
+
streamInfo?: StreamInfo,
|
|
163
|
+
): Promise<{ frames: VideoFrame[]; duration: number | null; error: string | null }> {
|
|
164
|
+
const info = streamInfo ?? await getYouTubeStreamInfo(videoId);
|
|
165
|
+
if ("error" in info) return { frames: [], duration: null, error: info.error };
|
|
166
|
+
const results = await Promise.all(timestamps.map(async (t) => {
|
|
167
|
+
const frame = await extractFrameFromStream(info.streamUrl, t);
|
|
168
|
+
if ("error" in frame) return { error: frame.error };
|
|
169
|
+
return { ...frame, timestamp: formatSeconds(t) };
|
|
170
|
+
}));
|
|
171
|
+
const frames = results.filter((f): f is VideoFrame => "data" in f);
|
|
172
|
+
const errorResult = results.find((f): f is { error: string } => "error" in f);
|
|
173
|
+
return { frames, duration: info.duration, error: frames.length === 0 && errorResult ? errorResult.error : null };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export async function fetchYouTubeThumbnail(videoId: string): Promise<{ data: string; mimeType: string } | null> {
|
|
177
|
+
try {
|
|
178
|
+
const res = await fetch(`https://img.youtube.com/vi/${videoId}/hqdefault.jpg`, {
|
|
179
|
+
signal: AbortSignal.timeout(5000),
|
|
180
|
+
});
|
|
181
|
+
if (!res.ok) return null;
|
|
182
|
+
const buffer = Buffer.from(await res.arrayBuffer());
|
|
183
|
+
if (buffer.length === 0) return null;
|
|
184
|
+
return { data: buffer.toString("base64"), mimeType: "image/jpeg" };
|
|
185
|
+
} catch {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async function tryGeminiWeb(
|
|
191
|
+
url: string,
|
|
192
|
+
prompt: string,
|
|
193
|
+
config: YouTubeConfig,
|
|
194
|
+
signal?: AbortSignal,
|
|
195
|
+
): Promise<ExtractedContent | null> {
|
|
196
|
+
try {
|
|
197
|
+
const cookies = await isGeminiWebAvailable();
|
|
198
|
+
if (!cookies) return null;
|
|
199
|
+
|
|
200
|
+
if (signal?.aborted) return null;
|
|
201
|
+
|
|
202
|
+
const text = await queryWithCookies(prompt, cookies, {
|
|
203
|
+
youtubeUrl: url,
|
|
204
|
+
model: config.preferredModel,
|
|
205
|
+
signal,
|
|
206
|
+
timeoutMs: 120000,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
url,
|
|
211
|
+
title: extractHeadingTitle(text) ?? "YouTube Video",
|
|
212
|
+
content: text,
|
|
213
|
+
error: null,
|
|
214
|
+
};
|
|
215
|
+
} catch {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function tryGeminiApi(
|
|
221
|
+
url: string,
|
|
222
|
+
prompt: string,
|
|
223
|
+
config: YouTubeConfig,
|
|
224
|
+
signal?: AbortSignal,
|
|
225
|
+
): Promise<ExtractedContent | null> {
|
|
226
|
+
try {
|
|
227
|
+
if (!isGeminiApiAvailable()) return null;
|
|
228
|
+
|
|
229
|
+
if (signal?.aborted) return null;
|
|
230
|
+
|
|
231
|
+
const text = await queryGeminiApiWithVideo(prompt, url, {
|
|
232
|
+
model: config.preferredModel,
|
|
233
|
+
signal,
|
|
234
|
+
timeoutMs: 120000,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
return {
|
|
238
|
+
url,
|
|
239
|
+
title: extractHeadingTitle(text) ?? "YouTube Video",
|
|
240
|
+
content: text,
|
|
241
|
+
error: null,
|
|
242
|
+
};
|
|
243
|
+
} catch {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async function tryPerplexity(
|
|
249
|
+
url: string,
|
|
250
|
+
prompt: string,
|
|
251
|
+
signal?: AbortSignal,
|
|
252
|
+
): Promise<ExtractedContent | null> {
|
|
253
|
+
try {
|
|
254
|
+
if (signal?.aborted) return null;
|
|
255
|
+
|
|
256
|
+
const perplexityQuery = prompt === YOUTUBE_PROMPT
|
|
257
|
+
? `Summarize this YouTube video in detail: ${url}`
|
|
258
|
+
: `${prompt} YouTube video: ${url}`;
|
|
259
|
+
|
|
260
|
+
const { answer } = await searchWithPerplexity(
|
|
261
|
+
perplexityQuery,
|
|
262
|
+
{ signal },
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
if (!answer) return null;
|
|
266
|
+
|
|
267
|
+
const content =
|
|
268
|
+
`# Video Summary (via Perplexity)\n\n${answer}\n\n` +
|
|
269
|
+
`*Full video understanding requires Gemini access. Set GEMINI_API_KEY or sign into Google in Chrome.*`;
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
url,
|
|
273
|
+
title: "Video Summary (via Perplexity)",
|
|
274
|
+
content,
|
|
275
|
+
error: null,
|
|
276
|
+
};
|
|
277
|
+
} catch {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
}
|