-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideoExtractor.js
More file actions
247 lines (217 loc) · 6.46 KB
/
Copy pathvideoExtractor.js
File metadata and controls
247 lines (217 loc) · 6.46 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* 视频信息提取模块
* 支持 Bilibili 和 YouTube
*/
const axios = require('axios');
const cheerio = require('cheerio');
/**
* 检测视频平台类型
* @param {string} url - 视频链接
* @returns {string|null} - 平台类型或null
*/
function detectPlatform(url) {
try {
const urlObj = new URL(url);
const hostname = urlObj.hostname.toLowerCase();
if (hostname.includes('bilibili.com')) {
return 'bilibili';
}
if (hostname.includes('youtube.com') || hostname.includes('youtu.be')) {
return 'youtube';
}
return null;
} catch (error) {
return null;
}
}
/**
* 提取 Bilibili 视频信息
* @param {string} url - B站视频链接
* @returns {Promise<Object>} - 视频信息
*/
async function extractBilibiliInfo(url) {
try {
// 清理URL,移除查询参数
const cleanUrl = url.split('?')[0];
// 获取页面HTML
const response = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': 'https://www.bilibili.com'
},
timeout: 10000
});
const $ = cheerio.load(response.data);
// 尝试多种选择器获取标题
let title = '';
const titleSelectors = [
'h1.video-title',
'.video-title.van-ellipsis',
'.tit',
'[data-title]',
'h1.title'
];
for (const selector of titleSelectors) {
const element = $(selector).first();
if (element.length && element.text().trim()) {
title = element.text().trim();
break;
}
}
// 如果还是没找到,尝试从meta标签获取
if (!title) {
title = $('meta[property="og:title"]').attr('content') ||
$('meta[name="title"]').attr('content') ||
document.title;
}
// 清理标题(移除B站后缀)
title = title.replace(/_哔哩哔哩 \(゜-゜\)つロ 干杯~-bilibili/, '').trim();
title = title.replace(/_哔哩哔哩_bilibili/, '').trim();
// 提取BV号
const bvMatch = cleanUrl.match(/BV[a-zA-Z0-9]+/);
const bvid = bvMatch ? bvMatch[0] : null;
return {
platform: 'Bilibili',
url: cleanUrl,
title: title || '未知标题',
bvid: bvid,
success: true
};
} catch (error) {
// 如果网络请求失败,尝试从URL解析基本信息
const cleanUrl = url.split('?')[0];
const bvMatch = cleanUrl.match(/BV[a-zA-Z0-9]+/);
return {
platform: 'Bilibili',
url: cleanUrl,
title: `B站视频 ${bvMatch ? bvMatch[0] : ''}`,
bvid: bvMatch ? bvMatch[0] : null,
success: false,
error: error.message
};
}
}
/**
* 提取 YouTube 视频信息
* @param {string} url - YouTube视频链接
* @returns {Promise<Object>} - 视频信息
*/
async function extractYouTubeInfo(url) {
try {
// 清理URL,只保留视频ID部分
let cleanUrl = url;
const urlObj = new URL(url);
// 处理 youtu.be 短链接
if (urlObj.hostname.includes('youtu.be')) {
const videoId = urlObj.pathname.slice(1);
cleanUrl = `https://www.youtube.com/watch?v=${videoId}`;
} else {
// 只保留 v 参数
const videoId = urlObj.searchParams.get('v');
if (videoId) {
cleanUrl = `https://www.youtube.com/watch?v=${videoId}`;
}
}
// 获取页面HTML
const response = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': 'https://www.youtube.com'
},
timeout: 10000
});
const $ = cheerio.load(response.data);
// 尝试多种方式获取标题
let title = '';
// 从meta标签获取
title = $('meta[property="og:title"]').attr('content') ||
$('meta[name="title"]').attr('content');
// 从页面元素获取
if (!title) {
const titleSelectors = [
'h1.title',
'h1.ytd-watch-metadata',
'#title h1',
'.watch-title'
];
for (const selector of titleSelectors) {
const element = $(selector).first();
if (element.length && element.text().trim()) {
title = element.text().trim();
break;
}
}
}
// 清理标题
if (title) {
title = title.replace(/ - YouTube$/, '').trim();
}
// 提取视频ID
const videoIdMatch = cleanUrl.match(/[?&]v=([a-zA-Z0-9_-]+)/);
const videoId = videoIdMatch ? videoIdMatch[1] : null;
return {
platform: 'YouTube',
url: cleanUrl,
title: title || '未知标题',
videoId: videoId,
success: true
};
} catch (error) {
// 如果网络请求失败,尝试从URL解析基本信息
let cleanUrl = url;
try {
const urlObj = new URL(url);
if (urlObj.hostname.includes('youtu.be')) {
const videoId = urlObj.pathname.slice(1);
cleanUrl = `https://www.youtube.com/watch?v=${videoId}`;
} else {
const videoId = urlObj.searchParams.get('v');
if (videoId) {
cleanUrl = `https://www.youtube.com/watch?v=${videoId}`;
}
}
} catch (e) {}
const videoIdMatch = cleanUrl.match(/[?&]v=([a-zA-Z0-9_-]+)/);
const videoId = videoIdMatch ? videoIdMatch[1] : null;
return {
platform: 'YouTube',
url: cleanUrl,
title: `YouTube视频 ${videoId || ''}`,
videoId: videoId,
success: false,
error: error.message
};
}
}
/**
* 提取视频信息(通用入口)
* @param {string} url - 视频链接
* @returns {Promise<Object>} - 视频信息
*/
async function extractVideoInfo(url) {
const platform = detectPlatform(url);
if (!platform) {
return {
platform: 'unknown',
url: url,
title: '未知视频',
success: false,
error: '不支持的视频平台,目前仅支持 Bilibili 和 YouTube'
};
}
if (platform === 'bilibili') {
return await extractBilibiliInfo(url);
} else if (platform === 'youtube') {
return await extractYouTubeInfo(url);
}
}
module.exports = {
detectPlatform,
extractVideoInfo,
extractBilibiliInfo,
extractYouTubeInfo
};