-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (146 loc) · 5.31 KB
/
Copy pathindex.js
File metadata and controls
196 lines (146 loc) · 5.31 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
var http = require('http');
var https = require('https');
var qs = require('querystring');
var url = require('url');
var fs = require('fs');
var path = require('path');
//下载m3u8视频
function down_m3u8_video(m3u8Url) {
var myURL = url.parse(m3u8Url);
myURL.href2 = myURL.href.replace(/^(http:\/\/|https:\/\/)/i, '');
myURL.href2Arr = myURL.href2.split('/');
myURL.pathnameArr = myURL.pathname.split('/');
console.log(myURL);
//.m3u8文件名字
let m3u8FileName = '';
for (let i = 0; i < myURL.pathnameArr.length; i++) {
let item = myURL.pathnameArr[i];
if (/\.m3u8/i.test(item)) {
m3u8FileName = item;
break;
}
}
//将 https://vip.ffzy-play3.com/20230329/10135_3e7ea150/2000k/hls/mixed.m3u8
//解析成 https://vip.ffzy-play3.com/20230329/10135_3e7ea150/2000k/hls
let urlPath = '';
urlPath += myURL.protocol + '//' + myURL.href2Arr.slice(0, myURL.href2Arr.length - 1).join('/');
// console.log(urlPath);
//m3u8相关文件保存地址
let saveFilePath = path.join(__dirname, 'public');
//下载m3u8文件
downloadFile(urlPath + '/' + m3u8FileName, path.join(saveFilePath, m3u8FileName)).then((m3u8FilePath) => {
console.log('m3u8FilePath:', m3u8FilePath)
//根据.m3u8文件里的ts文件地址,下载ts文件
getTsInM3u8(m3u8FilePath, function (tsUrl) {
let tsFileUrl = urlPath+'/'+tsUrl;
let tsFilePath = path.join(saveFilePath,tsUrl);
downloadFile(tsFileUrl,tsFilePath).then((res)=>{
console.log(res)
})
})
})
}
// down_m3u8_video('https://vip.ffzy-play3.com/20230329/10135_3e7ea150/2000k/hls/mixed.m3u8');//海贼第一集
//下载文件
function downloadFile(fileUrl, savePath) {
return new Promise(function (resolve, reject) {
var myURL = url.parse(fileUrl);
// console.log(myURL);
var requestMethod = 'GET'.toUpperCase();//请求方式
var requestPort = null;//端口号
if (myURL.port != null) {
requestPort = myURL.port;
} else {
if (myURL.protocol == 'https:') {
requestPort = 443;
} else if (myURL.protocol == 'http:') {
requestPort = 80;
}
}
var options = {
// hostname: 'www.baidu.com',
// path: '/getInfo',
// port: 80,
// method: 'GET',
// headers: {
// 'Content-Type': 'application/json',
// 'Content-Length': Buffer.byteLength(postData),
// }
};
options = JSON.parse(JSON.stringify(myURL));
options.method = requestMethod;
options.port = requestPort;
options.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0',
}
// console.log(options);
var req = null;
if (myURL.protocol == 'https:') {
req = https.request(options, responseCallback);
} else {
req = http.request(options, responseCallback);
}
function responseCallback(res) {
console.log(`request STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('binary');
var resData = '';
res.on('data', (chunk) => {
// console.log(`BODY: ${chunk}`);
resData += chunk;
});
res.on('end', () => {
// console.log('No more data in response.');
// console.log(resData)
//保存文件
saveFile(savePath, resData).then((filePath) => {
resolve(filePath)
})
});
}
req.on('error', (e) => {
console.error(`request error: ${e.message}`);
});
if (requestMethod == 'POST') {
// const postData = JSON.stringify({
// 'msg': 'Hello World!',
// });
// req.write(postData);
}
req.end();
})
}
//提取m3u8文件里的ts文件地址
function getTsInM3u8(filePath, callback) {
var fileData = fs.readFileSync(filePath, 'binary');
fileData = Buffer.from(fileData, 'binary').toString('utf8');//二进制转utf8
// console.log(fileData)
let arr = fileData.split(/[\r\n]/);
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (/(\.ts)/i.test(item)) {
// console.log(item)
callback(item)
}
}
// console.log(arr)
}
//保存文件
function saveFile(path, data) {
return new Promise((resolve, reject) => {
//创建可写流文件
let writerStream = fs.createWriteStream(path);
writerStream.write(data, 'binary');
writerStream.end();//标记文件末尾
writerStream.on('finish', function () {
//finish 写入文件结束
console.log('保存文件成功', path);
resolve(path)
});
writerStream.on('error', function (err) {
console.log('保存文件失败', path, err);
// console.log(err.stack);
reject(err)
});
})
}