Files
play-dl-test/play-dl/YouTube/stream.ts

107 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-08-16 17:37:44 +05:30
import got from "got"
2021-08-16 15:19:31 +05:30
import { video_info } from "."
2021-08-17 21:16:58 +05:30
import { PassThrough } from 'stream'
import https from 'https'
2021-08-16 15:19:31 +05:30
interface StreamOptions {
2021-08-18 17:11:37 +05:30
filter : "bestaudio" | "bestvideo" | "live"
2021-08-16 15:19:31 +05:30
}
2021-08-18 17:11:37 +05:30
function parseAudioFormats(formats : any[]){
let result: any[] = []
2021-08-16 15:19:31 +05:30
formats.forEach((format) => {
let type = format.mimeType as string
if(type.startsWith('audio')){
2021-08-18 17:11:37 +05:30
format.codec = type.split('codecs="')[1].split('"')[0]
format.container = type.split('audio/')[1].split(';')[0]
result.push(format)
2021-08-16 15:19:31 +05:30
}
})
2021-08-18 17:11:37 +05:30
return result
2021-08-16 15:19:31 +05:30
}
2021-08-18 17:11:37 +05:30
function parseVideoFormats(formats : any[]){
let result: any[] = []
formats.forEach((format) => {
let type = format.mimeType as string
if(type.startsWith('audio')){
format.codec = type.split('codecs="')[1].split('"')[0]
format.container = type.split('audio/')[1].split(';')[0]
result.push(format)
}
})
return result
2021-08-16 15:19:31 +05:30
}
2021-08-17 21:16:58 +05:30
export async function stream(url : string, options? : StreamOptions): Promise<PassThrough>{
2021-08-16 15:19:31 +05:30
let info = await video_info(url)
let final: any[] = [];
2021-08-18 17:11:37 +05:30
if(info.video_details.live === true && options) {
options.filter = "live"
}
2021-08-16 15:19:31 +05:30
2021-08-18 17:11:37 +05:30
if(options?.filter){
switch(options.filter){
case "bestaudio":
let audioFormat = parseAudioFormats(info.format)
if(audioFormat.length === 0) await stream(url, { filter : "bestvideo" })
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
final.push(audioFormat[audioFormat.length - 1])
}
else{
final.push(opusFormats[opusFormats.length - 1])
2021-08-16 15:19:31 +05:30
}
2021-08-18 17:11:37 +05:30
break
case "bestvideo" :
let videoFormat = parseVideoFormats(info.format)
if(videoFormat.length === 0) throw new Error('Can\'t Find Video Formats ')
let qual_1080 = filterVideo(videoFormat, "1080p")
if(qual_1080.length === 0) {
let qual_720 = filterVideo(videoFormat, "720p")
if(qual_720.length === 0) final.push(videoFormat[0])
else final.push(qual_720)
break
}
else final.push(qual_1080)
break
2021-08-16 15:19:31 +05:30
}
}
2021-08-18 17:11:37 +05:30
if(final.length === 0) final.push(info.format[info.format.length - 1])
let piping_stream = got.stream(final[0].url, {
2021-08-17 11:39:04 +05:30
retry : 5,
2021-08-17 11:50:50 +05:30
headers: {
'Connection': 'keep-alive',
'Accept-Encoding': '',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
2021-08-17 12:52:49 +05:30
},
2021-08-17 14:14:29 +05:30
agent : {
2021-08-17 21:16:58 +05:30
https : new https.Agent({ keepAlive : true })
2021-08-17 14:14:29 +05:30
},
2021-08-17 12:52:49 +05:30
http2 : true
2021-08-17 11:39:04 +05:30
})
2021-08-17 21:16:58 +05:30
let playing_stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
2021-08-18 17:11:37 +05:30
piping_stream.pipe(playing_stream)
2021-08-17 21:16:58 +05:30
return playing_stream
2021-08-18 17:11:37 +05:30
}
function filterFormat(formats : any[], codec : string){
let result: any[] = []
formats.forEach((format) => {
if(format.codec === codec) result.push(format)
})
return result
}
function filterVideo(formats : any[], quality : string) {
let result: any[] = []
formats.forEach((format) => {
if(format.qualityLabel === quality) result.push(format)
})
return result
2021-08-16 15:19:31 +05:30
}