-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.js
More file actions
50 lines (47 loc) · 1.22 KB
/
runner.js
File metadata and controls
50 lines (47 loc) · 1.22 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
import ytdl from "ytdl-core";
import ffmpeg from "fluent-ffmpeg";
import fs from "fs";
import ffmpegPath from "@ffmpeg-installer/ffmpeg";
ffmpeg.setFfmpegPath(ffmpegPath.path);
import { workerData, parentPort } from "worker_threads";
const getAudio = (video) =>
new Promise((resolve, reject) => {
var stream = ytdl(video?.url, { filter: "audioonly" });
var file = fs.createWriteStream(
`./${workerData?.dirname}/${video?.title
.split("/")
.join(" ")
.split(".")
.join(" ")}.mp3`
);
ffmpeg(stream)
.format("mp3")
.save(file)
.on("end", () => {
resolve({
status: "done",
name: video?.title,
filename: video?.title.split("/").join(" ").split(".").join(" "),
});
});
});
(async () => {
const job = workerData.jobs[workerData.thread_count];
for (let video of job) {
try {
parentPort.postMessage(
await getAudio({
...video,
url: "https://www.youtube.com/watch?v=" + video?.id,
})
);
} catch (e) {
parentPort.postMessage({
status: "error",
name: video?.title,
message: e.message,
});
}
}
parentPort.postMessage("end");
})();