-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
27 lines (23 loc) · 806 Bytes
/
server.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
const WebSocketServer = require('ws').Server
const websocketStream = require('websocket-stream')
const wss = new WebSocketServer({port: process.env.PORT || 3000})
const fs = require('fs')
const ytdl = require('ytdl-core');
const https = require('https');
wss.on('connection', async function (ws, req) {
const stream = websocketStream(ws)
let videoId = req.url.split('/')[1];
console.log('opened connection to '+ videoId);
let info = await ytdl.getInfo('https://www.youtube.com/watch?v='+videoId);
let url = '';
fs.writeFileSync('info.txt', JSON.stringify(info));
info.formats.forEach(format => {
if (format.itag == '249') {
url = format.url;
fileSize = format.contentLength;
}
});
https.get(url, function(data){
data.pipe(stream)
});
})