-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioStream.js
More file actions
250 lines (217 loc) · 9.16 KB
/
Copy pathAudioStream.js
File metadata and controls
250 lines (217 loc) · 9.16 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
248
249
250
const { exec } = require('child_process');
const ytdl = require('ytdl-core');
let PAUSE = 'echo \'{ "command": ["set_property", "pause", true] }\' | socat - /tmp/mpvsocket';
let RESUME = 'echo \'{ "command": ["set_property", "pause", false] }\' | socat - /tmp/mpvsocket';
module.exports = class AudioStream {
constructor() {
this.mpvs = []; // buffered queue
this.queue = []; // total queue
this.total = 0; // used for pausing the buffered element
this.isPlaying = false; // used to check if playing
this.current = 0; // used for pausing/playing
this.PIDs = []; // used for keeping track of PIDs
this.totalQueue = []; // used for the entire queue to display
this.lastSkipTime = 0; // used to rate limit the skip
this.currentPID = 0; // used to skip the current PID
}
newMPV(index, url) {
// Create MPV instance with no video and specified socket
let temp = exec(`mpv --cache=yes --no-video --force-window=no --cache-pause=yes --cache-pause-initial=yes --input-ipc-server=/tmp/mpvsocket${index} "${url}"`);
this.PIDs.push(temp.pid);
console.log(temp.pid, 'MPV process created');
return temp;
}
pause(index = -1) {
if (index == -1) {
console.log('pausing', this.current);
return exec(`${PAUSE}${this.current}`);
} else {
console.log('pausing index', index);
return exec(`${PAUSE}${index}`);
}
}
resume(index = -1) {
if (index == -1) {
console.log('resuming', this.current);
return exec(`${RESUME}${this.current}`);
} else {
console.log('resuming index', index);
return exec(`${RESUME}${index}`);
}
}
rewind() {
return exec(`echo '{ "command": ["seek", -10] }' | socat - /tmp/mpvsocket${this.current}`);
}
forward() {
return exec(`echo '{ "command": ["seek", 5] }' | socat - /tmp/mpvsocket${this.current}`);
}
togglePauseResume() {
return exec(`echo '{ "command": ["cycle", "pause"] }' | socat - /tmp/mpvsocket${this.current}`);
}
isName(str) {
let start_index = str.indexOf("}]},\"title\":{\"runs\":[{\"text\"");
if (start_index === -1) return true;
let isName = str.substring(start_index + 30);
isName = isName.substring(isName.indexOf('"'), isName.indexOf('"') + 24);
return isName.includes("navigationEndpoint");
}
async extractVideoInfo(url) {
if (url.includes("playlist")) {
console.log(url)
try {
let remainingOutput = await fetch(url).then(res => res.text());
let videoCount = 0;
while (remainingOutput.length > 0) {
console.log(remainingOutput.length)
try {
const ind = remainingOutput.indexOf("videoId");
const temp = remainingOutput.substring(ind, ind + 1500);
if (this.isName(temp)) throw new Error("Is a name pass");
let videoId = temp.substring(temp.indexOf('"') + 3, temp.indexOf('"') + 20);
videoId = videoId.substring(0, videoId.indexOf('"'));
let thumbnail = temp.substring(temp.indexOf("thumbnails\":[{\"url\"") + 21, temp.indexOf("thumbnails\":[{\"url\"") + 221);
thumbnail = thumbnail.substring(0, thumbnail.indexOf('"'));
let title = temp.substring(temp.indexOf("}]},\"title\":{\"runs\":[{\"text\"") + 30, temp.indexOf("}]},\"title\":{\"runs\":[{\"text\"") + 145);
title = title.substring(0, title.indexOf('"'));
remainingOutput = remainingOutput.substring(ind + 1500);
const mediaItem = { title, url: `https://www.youtube.com/watch?v=${videoId}`, thumbnail };
this.queue.push(mediaItem)
this.totalQueue.push(mediaItem)
}
catch {
remainingOutput = remainingOutput.substring(300);
}
}
if (this.mpvs.length < 2) {
this.buffer(this.queue.shift().url);
}
}
catch (error) {
console.log(error)
console.error("Error fetching video info:", error);
}
}
}
async addToQueue(url) {
try {
const info = await ytdl.getInfo(url);
// Extract the video title
const title = info.videoDetails.title;
// Extract the array of thumbnails
const thumbnails = info.videoDetails.thumbnails;
// Retrieve the URL of the largest thumbnail (usually the last one in the array)
const largestThumbnail = thumbnails[thumbnails.length - 1].url;
const mediaItem = { title, url, thumbnail: largestThumbnail };
this.queue.push(mediaItem);
console.log("Added to queue:", title);
} catch (error) {
console.error("Error fetching video info:", error);
}
}
// This is the function that handles placing the urls into the queue
queueUp(url) {
// if it is a playlist, run the function that gets each of the URLs
if (url.includes("playlist")) {
this.extractVideoInfo(url);
} else {
// Otherwise we want to check if mpv array is less than 2, if it is, buffer it
if (this.mpvs.length < 2) {
this.buffer(url);
}
else {
// otherwise just push it to the queue
this.addToQueue(url);
}
// add to total queue for displaying
this.totalQueue.push(url);
}
}
play() {
let tuple = this.mpvs.shift();
this.totalQueue.shift();
console.log('here', this.queue, this.totalQueue, tuple, 'tuple')
if (tuple != null) {
if (tuple[1]) {
this.resume(tuple[1]);
}
if (this.queue.length > 0) this.buffer(this.queue.shift().url);
if (!this.isPlaying) this.isPlaying = true;
this.current = tuple[1];
}
}
async extractVideoIds(url) {
const s = new Set();
try {
let temp = await fetch(url).then(res => res.text());
while (temp !== "") {
const ind = temp.indexOf('videoId');
if (temp.substring(ind, ind + 40).includes("videoId")) {
const videoIdStart = ind + 10;
const videoIdEnd = ind + 23;
const videoId = temp.substring(videoIdStart, videoIdEnd).replace(/["\[\],]/g, "");
s.add("https://www.youtube.com/watch?v=" + videoId);
}
temp = temp.substring(ind + 21);
}
} catch (error) {
console.error('Error:', error);
}
for (const video of Array.from(s)) {
this.queueUp(video);
}
}
// Function responsible for buffering the audio using yt-dlp command
// Function responsible for buffering the audio using yt-dlp command
async buffer(url) {
this.total += 1;
const socketPath = `/tmp/mpvsocket${this.total}`;
console.log(`Getting audio URL for ${url}...`);
// Get the audio URL first
exec(`yt-dlp -f bestaudio -g "${url}"`, async (error, stdout, stderr) => {
if (error) {
console.error(`Error getting audio URL: ${error.message}`);
return;
}
const audioURL = stdout.trim();
console.log(`Audio URL obtained: ${audioURL.substring(0, 30)}...`);
let mpv = this.newMPV(this.total, audioURL)
// Push to queue and pause initially
this.mpvs.push([mpv, this.total]);
// Add delay to ensure MPV has started
setTimeout(() => {
this.pause(this.total);
if (!this.isPlaying) this.play();
}, 1000);
mpv.stdout.on('data', (data) => {
// console.log(data.toString());
if (data.toString().includes("Exiting") || data.toString().includes("underrun")) {
exec(`rm -f ${socketPath}`);
console.log('MPV exited');
this.isPlaying = false;
this.PIDs.shift();
this.play();
}
});
});
}
skip() {
const currentTime = Date.now();
const elapsedTime = currentTime - this.lastSkipTime;
if (elapsedTime < (30 * 1000)) { // 30 seconds cooldown
return false;
} else {
this.lastSkipTime = currentTime; // Update last skip time
exec(`echo '{ "command": ["quit", "9"] }' | socat - /tmp/mpvsocket${this.current}`);
return true;
}
}
setVolume(volume) {
return exec(`echo '{ "command": ["set_property", "volume", ${volume}] }' | socat - /tmp/mpvsocket${this.current}`);
}
getTotal() {
return this.total;
}
getQueue() {
return this.totalQueue;
}
};