forked from AlexR32/Roblox-Videoplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (58 loc) · 1.93 KB
/
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
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
const childProcess = require('child_process')
const ffmpeg = require('ffmpeg-static')
const ytdl = require('ytdl-core')
const express = require('express')
const app = express()
const port = process.env.PORT || 80
app.use(express.json())
function checkItag(info) {
for (i in info.formats) {
if (info.formats[i].itag === 247) {
return true
}
}
}
app.post('/youtube', async function(request, responce) {
if (!request.body && !request.body.videoId) return responce.sendStatus(404)
let body = request.body
let videoId = 'https://youtu.be/' + body.videoId
let validURL = ytdl.validateURL(videoId)
if (validURL) {
let info = await ytdl.getInfo(videoId)
if (checkItag(info)) {
console.log('downloading ' + videoId)
let video = ytdl(videoId, {quality: 247}) // only 720p webm
let audio = ytdl(videoId, {quality: 'highestaudio'})
const ffmpegProcess = childProcess.spawn(ffmpeg, [
'-loglevel', '8', '-hide_banner',
'-i', 'pipe:3',
'-i', 'pipe:4',
'-map', '0:v',
'-map', '1:a',
'-metadata','duration=' + info.videoDetails.lengthSeconds,
'-c:v', 'copy',
'-f', "webm",
'pipe:5',
], {
windowsHide: true,
stdio: [
'inherit', 'inherit', 'inherit',
'pipe', 'pipe', 'pipe',
],
})
video.pipe(ffmpegProcess.stdio[3])
audio.pipe(ffmpegProcess.stdio[4])
ffmpegProcess.stdio[5].pipe(responce)
ffmpegProcess.on('close', () => {
console.log('downloaded ' + videoId)
})
} else {
responce.sendStatus(404)
}
} else {
responce.sendStatus(404)
}
})
app.listen(port, () => {
console.log('server running')
})