-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathytCoreDl.js
More file actions
193 lines (159 loc) · 4.58 KB
/
ytCoreDl.js
File metadata and controls
193 lines (159 loc) · 4.58 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
const ytdl = require('ytdl-core');
const express=require('express')
const fs=require('fs')
const app=express()
const port=5000;
const ffmpeg = require('fluent-ffmpeg');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/dl',(req,res)=>{
//return res.send(req.query.videolink)
// get video and write it into the res chunk by chunk
if(ytdl.validateURL(req.query.videolink)){
const options=extractOptions(req.query.options)
const mimeFromQuery=req.query.mime || 'mp4'
let videoStream=ytdl(req.query.videolink, options)
videoStream.on('info',(info)=>{
let nameFromQuery;
if(req.query.name){
// sanatize string make it ascii only
nameFromQuery=req.query.name.replace('|','').replace(/[^\x00-\x7F]/g, "");
}
else if(info.player_response.videoDetails.title){
// sanatize string make it ascii only
nameFromQuery=info.player_response.videoDetails.title.replace('|','').replace(/[^\x00-\x7F]/g, "");
}
else{
nameFromQuery='youtubevideo';
}
res.header('Content-Disposition', 'attachment; filename='+nameFromQuery+'.'+mimeFromQuery);
}).on('error',(error)=>{
res.status(500)
res.send(('download failed'))
})
videoStream.pipe(res);
}else{
res.status(404)
res.send('NOT A YOUTUBE URL')
}
})
app.get('/dlmp3',(req,res)=>{
if(ytdl.validateURL(req.query.videolink)){
const nameFromQuery=req.query.name || 'youtubevideo'
const mimeFromQuery=req.query.mime || 'mp3'
res.header('Content-Disposition', 'attachment; filename='+nameFromQuery+'.'+mimeFromQuery);
let options=extractOptions(req.query.options)
const url=req.query.videolink
stream = new ffmpeg(ytdl(url,options))
stream.on('error', function (err) {
res.status(500)
res.send('failed to load video')
})
.format('mp3').pipe(res)
}else{
res.status(404)
res.send('NOT A YOUTUBE URL')
}
})
extractOptions=(str)=>{
//create object from send query
let output={}
if(str){
optionArr=str.split(',')
optionArr.forEach(option=>{
let entry=option.split(':')
output[entry[0]]=entry[1]
})
}
return output
}
app.get('/info',(req,res)=>{
// send all info from api
if(ytdl.validateURL(req.query.videolink)){
let urlParts=req.query.videolink.split('/watch?v=')
if(urlParts[1]===undefined){
urlParts=req.query.videolink.split('outu.be/')
}
ytdl.getInfo(req.query.videolink,(error, info)=>{
if(error){
res.status(500)
res.send('could not get info')
}
res.send(JSON.stringify(info))
})
}else{
res.status(404)
res.send('NOT A YOUTUBE URL')
}
})
app.get('/simpleinfo',(req,res)=>{
// send only basic info needed for the front end to save some data
if(ytdl.validateURL(req.query.videolink)){
ytdl.getInfo(req.query.videolink,(error, info)=>{
if(error){
res.status(500)
res.send('could not get info')
}
const simpleInfo=
{
thumbnail:info.player_response.videoDetails.thumbnail.thumbnails[2].url || "",
title:info.title,
length:info.length_seconds,
formats: []
}
if(info.formats){
info.formats.map(format=>{
simpleInfo.formats.push(
{
type: format.type.split(';')[0],
videoOnly: format.audioBitrate===null,
quality: format.resolution==null? (format.audioBitrate+' bitrate') : format.resolution,
itag: format.itag,
url: format.url
})
})
}
res.send(simpleInfo)
})
}else{
res.status(404)
res.send('NOT A YOUTUBE URL')
}
})
app.get('/formatlist',(req,res)=>{
// send only updated formats since they the links fail after a while
if(ytdl.validateURL(req.query.videolink)){
ytdl.getInfo(req.query.videolink,(error, info)=>{
if(error){
res.status(500)
res.send('could not get info')
}
const simpleInfo=
{
formats: []
}
if(info.formats){
info.formats.map(format=>{
simpleInfo.formats.push(
{
type: format.type.split(';')[0],
videoOnly: format.audioBitrate===null,
quality: format.resolution==null? (format.audioBitrate+' bitrate') : format.resolution,
itag: format.itag,
url: format.url
})
})
}
res.send(simpleInfo)
})
}else{
res.status(404)
res.send('NOT A YOUTUBE URL')
}
})
app.listen(port,()=>{
console.log(`server running on port ${port}`)
})