-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube-extract-script.js
More file actions
183 lines (160 loc) · 6.08 KB
/
youtube-extract-script.js
File metadata and controls
183 lines (160 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* 🎬 YouTube 专用提取脚本
*
* 直接在浏览器中调用 Innertube API,输出标准的 metadata + markdown 格式
*
* 输出结构:
* {
* metadata: { title, author, publisher, publishDate, thumbnail, description },
* markdown: "组装好的 Markdown 内容",
* extra: { videoId, duration, hasTranscript, transcriptLang, isAutoGenerated }
* }
*/
const YOUTUBE_EXTRACT_SCRIPT = `
(async function() {
const INNERTUBE_KEY = 'AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
try {
// 1️⃣ 从页面获取 videoId
const pr = window.ytInitialPlayerResponse;
const videoId = pr?.videoDetails?.videoId || new URLSearchParams(location.search).get('v');
if (!videoId) return { error: 'No video ID found' };
// 2️⃣ 调用 Innertube API 获取完整数据
const res = await fetch('https://www.youtube.com/youtubei/v1/player?key=' + INNERTUBE_KEY, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
videoId,
context: {
client: { clientName: 'WEB', clientVersion: '2.20231219.04.00', hl: 'en', gl: 'US' }
}
})
});
const data = await res.json();
if (data.playabilityStatus?.status !== 'OK') {
return { error: data.playabilityStatus?.reason || 'Video unavailable' };
}
const vd = data.videoDetails || {};
const mf = data.microformat?.playerMicroformatRenderer || {};
// 3️⃣ 提取标准 metadata(6 个字段)
const metadata = {
title: vd.title || '',
author: vd.author || '',
publisher: vd.author || '', // YouTube 的 publisher 就是 author
publishDate: mf.publishDate || mf.uploadDate || '',
thumbnail: 'https://img.youtube.com/vi/' + videoId + '/maxresdefault.jpg',
description: (vd.shortDescription || '').substring(0, 500)
};
// 4️⃣ 提取字幕
let transcript = '';
let transcriptLang = null;
let isAutoGenerated = false;
const captions = data.captions?.playerCaptionsTracklistRenderer?.captionTracks;
if (captions && captions.length > 0) {
// 优先选择:手动字幕 > ASR,英文 > 中文 > 其他
const langPriority = ['en', 'zh', 'zh-Hans', 'zh-Hant'];
const manualTracks = captions.filter(t => t.kind !== 'asr');
const asrTracks = captions.filter(t => t.kind === 'asr');
const tracksToSearch = manualTracks.length > 0 ? manualTracks : asrTracks;
let selectedTrack = null;
for (const lang of langPriority) {
selectedTrack = tracksToSearch.find(t => t.languageCode?.startsWith(lang));
if (selectedTrack) break;
}
if (!selectedTrack) selectedTrack = tracksToSearch[0];
if (selectedTrack) {
transcriptLang = selectedTrack.languageCode;
isAutoGenerated = selectedTrack.kind === 'asr';
// 获取字幕内容
const transcriptRes = await fetch(selectedTrack.baseUrl + '&fmt=json3');
const transcriptData = await transcriptRes.json();
for (const event of (transcriptData.events || [])) {
for (const seg of (event.segs || [])) {
if (seg.utf8) transcript += seg.utf8;
}
}
transcript = transcript.replace(/\\n/g, ' ').trim();
}
}
// 5️⃣ 提取章节(从描述中解析)
const chapters = [];
const desc = vd.shortDescription || '';
const chapterRegex = /\\(??(\\d+:)?(\\d+:\\d+)\\)??\\s*[-–]?\\s*(.+)/gm;
let match;
while ((match = chapterRegex.exec(desc)) !== null) {
const timeMatch = match[0].match(/\\d+:\\d+:\\d+|\\d+:\\d+/);
if (timeMatch) {
const time = timeMatch[0];
const title = match[0].replace(timeMatch[0], '').replace(/^[\\s\\-–()]+/, '').trim();
if (time && title && title.length < 100) {
chapters.push({ time, title });
}
}
}
// 6️⃣ 格式化时长
let duration = '';
const secs = parseInt(vd.lengthSeconds || '0', 10);
if (secs > 0) {
const h = Math.floor(secs / 3600);
const m = Math.floor((secs % 3600) / 60);
const s = secs % 60;
duration = h > 0
? h + ':' + String(m).padStart(2, '0') + ':' + String(s).padStart(2, '0')
: m + ':' + String(s).padStart(2, '0');
}
// 7️⃣ 组装 Markdown
let markdown = '## Original Metadata:\\n\\n';
markdown += '**Title:** ' + metadata.title + '\\n\\n';
const metaLines = [];
if (metadata.author) metaLines.push('**Channel:** ' + metadata.author);
if (metadata.publishDate) metaLines.push('**Date:** ' + metadata.publishDate);
if (duration) metaLines.push('**Duration:** ' + duration);
metaLines.push('**Source:** ' + location.href);
markdown += metaLines.join(' | ') + '\\n\\n';
// 过滤掉章节时间戳的描述
if (metadata.description) {
const coreDesc = metadata.description
.split('\\n')
.filter(line => !line.match(/^\\(?\\d+:\\d+/))
.join('\\n')
.trim();
if (coreDesc) {
markdown += '**Original_introduction:**\\n\\n' + coreDesc + '\\n\\n';
}
}
markdown += '---\\n\\n';
// 章节
if (chapters.length > 0) {
markdown += '## Chapters\\n\\n';
for (const ch of chapters) {
markdown += '- (' + ch.time + ') ' + ch.title + '\\n';
}
markdown += '\\n';
}
// 字幕
if (transcript) {
const type = isAutoGenerated ? 'Auto-generated' : 'Manual';
markdown += '## Transcript (' + type + ', ' + transcriptLang + ')\\n\\n';
markdown += transcript + '\\n';
} else {
markdown += '## Transcript\\n\\n*No transcript available*\\n';
}
// 8️⃣ 返回标准格式
return {
metadata,
markdown,
extra: {
videoId,
duration,
chapters,
hasTranscript: transcript.length > 0,
transcriptLang,
isAutoGenerated,
transcriptLength: transcript.length
}
};
} catch (e) {
return { error: e.message, stack: e.stack };
}
})()
`;
module.exports = { YOUTUBE_EXTRACT_SCRIPT };