-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstolen.js
More file actions
39 lines (32 loc) · 1.02 KB
/
Copy pathstolen.js
File metadata and controls
39 lines (32 loc) · 1.02 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
// sorry but taken from https://github.com/JamesKyburz/youtube-audio-stream
// due to outdated dependencies and didnt feel like bothering with fork
const ytdl = require("ytdl-core");
const FFmpeg = require("fluent-ffmpeg");
const { PassThrough } = require("stream");
const fs = require("fs");
export function streamify(uri, opt) {
opt = {
...opt,
videoFormat: "mp4",
quality: "lowest",
audioFormat: "mp3",
filter(format) {
return format.container === opt.videoFormat && format.audioBitrate;
},
};
const video = ytdl(uri, opt);
const { file, audioFormat } = opt;
const stream = file ? fs.createWriteStream(file) : new PassThrough();
const ffmpeg = new FFmpeg(video);
process.nextTick(() => {
const output = ffmpeg.format(audioFormat).pipe(stream);
ffmpeg.once("error", (error) => stream.emit("error", error));
output.once("error", (error) => {
video.end();
stream.emit("error", error);
});
});
stream.video = video;
stream.ffmpeg = ffmpeg;
return stream;
}