This repository has been archived by the owner on Jun 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
214 lines (188 loc) · 5.65 KB
/
index.js
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
const m3u8parser = require("m3u8-parser");
const fetch = require("node-fetch").default;
const { join } = require("path");
const { spawn, exec } = require("child_process");
const fs = require("fs");
const decryptionKey = "decryption key";
const masterPlaylist = "m3u8 url";
const outputFileName = "Andromeda.S05.E07.Attempting.Screed.mp4";
const baseUrl = masterPlaylist.slice(0, masterPlaylist.lastIndexOf("/")) + "/";
function fetchPlaylist(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(async (res) => {
if (res.ok) {
resolve(await res.text());
} else {
reject(res.statusText);
}
})
.catch(reject);
});
}
function parse(text) {
const parser = new m3u8parser.Parser();
parser.push(text);
parser.end();
return parser;
}
function printProgress(progress) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(progress);
}
const download = (url, dir, file) => {
return new Promise((resolve, reject) => {
const child = spawn("aria2c", [
"--auto-file-renaming=false",
"-c",
"-j16",
"-x16",
"-s16",
"-d",
dir,
"-o",
file,
url,
]);
child.stdout.on("data", (data) => {
printProgress(data.toString());
});
child.stderr.on("data", (data) => {
printProgress(data.toString());
});
child.on("error", (err) => printProgress(err.toString()));
child.on("message", (msg, _) => printProgress(msg));
child.on("exit", (code) => {
if (code !== 0) {
reject(`aria2c exited with code ${code}`);
}
resolve();
});
});
};
(async () => {
const masterManifestText = await fetchPlaylist(masterPlaylist);
const masterManifest = parse(masterManifestText);
const audioUrl =
baseUrl +
masterManifest.manifest.mediaGroups.AUDIO["default-audio-group"]["stream_0"]
.uri;
const videoGroup =
masterManifest.manifest.playlists[
masterManifest.manifest.playlists.length - 1
];
const videoUrl = baseUrl + videoGroup.uri;
// process audio
const audioManifestText = await fetchPlaylist(audioUrl);
const audioManifest = parse(audioManifestText);
// const audioKID =
// audioManifest.manifest.contentProtection["com.widevine.alpha"].attributes
// .keyId;
const audioSegmentUrl = baseUrl + audioManifest.manifest.segments[0].uri;
const encryptedAudioFilePath = join(
__dirname,
"temp",
audioManifest.manifest.segments[0].uri
);
const decryptedAudioFilePath = join(
__dirname,
"temp",
`${audioManifest.manifest.segments[0].uri.split(".mp4")[0]}_decrypted.mp4`
);
await download(
audioSegmentUrl,
join(__dirname, "temp"),
audioManifest.manifest.segments[0].uri
).catch(console.error);
const child = exec(
`mp4decrypt --key 1:${decryptionKey} "${encryptedAudioFilePath}" "${decryptedAudioFilePath}"`
);
child.on("error", (err) => {
console.error(err);
});
child.on("message", (msg, _) => {
console.log(msg);
});
child.on("exit", async (code) => {
if (code !== 0) {
console.error(`mp4decrypt (audio) exited with code ${code}`);
process.exit(code);
}
console.log(`Audio decryption complete`);
// process video
const videoManifestText = await fetchPlaylist(baseUrl + videoGroup.uri);
const videoManifest = parse(videoManifestText);
const videoSegmentUrl = baseUrl + videoManifest.manifest.segments[0].uri;
const encryptedVideoFilePath = join(
__dirname,
"temp",
videoManifest.manifest.segments[0].uri
);
const decryptedVideoFilePath = join(
__dirname,
"temp",
`${videoManifest.manifest.segments[0].uri.split(".mp4")[0]}_decrypted.mp4`
);
await download(
videoSegmentUrl,
join(__dirname, "temp"),
videoManifest.manifest.segments[0].uri
).catch(console.error);
const child2 = exec(
`mp4decrypt --key 1:${decryptionKey} "${encryptedVideoFilePath}" "${decryptedVideoFilePath}"`
);
child2.on("error", (err) => {
console.error(err);
});
child2.on("message", (msg, _) => {
console.log(msg);
});
child2.on("exit", async (code) => {
if (code !== 0) {
console.error(`mp4decrypt (video) exited with code ${code}`);
process.exit(code);
}
console.log(`Video decryption complete`);
const outputPath = join(__dirname, outputFileName);
// merge
const ffmpegChild = spawn("ffmpeg", [
"-i",
`${decryptedAudioFilePath}`,
"-i",
`${decryptedVideoFilePath}`,
"-c",
"copy",
`${outputPath}`,
]);
console.log(ffmpegChild.spawnargs.join(" "));
ffmpegChild.stdout.on("data", (data) => {
console.log(data.toString());
});
ffmpegChild.stderr.on("data", (data) => {
console.error(data.toString());
});
ffmpegChild.on("error", (err) => console.error(err));
ffmpegChild.on("message", (msg, _) => console.log(msg));
ffmpegChild.on("exit", (code) => {
if (code !== 0) {
console.error(`ffmpeg exited with code ${code}`);
process.exit(code);
}
console.log(`Download complete`);
fs.unlink(encryptedAudioFilePath, () =>
console.log("Audio temp file deleted")
);
fs.unlink(encryptedVideoFilePath, () =>
console.log("Video temp file deleted")
);
fs.unlink(decryptedAudioFilePath, () =>
console.log("decrypted audio temp file deleted")
);
fs.unlink(decryptedVideoFilePath, () =>
console.log("decrypted video temp file deleted")
);
});
});
});
})();