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

149 lines
4.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-20 12:06:12 +05:30
import { FormatInterface, LiveEnded, LiveStreaming } from "./classes/LiveStream"
2021-08-16 15:19:31 +05:30
2021-08-20 12:06:12 +05:30
enum StreamType{
Arbitrary = 'arbitrary',
Raw = 'raw',
OggOpus = 'ogg/opus',
WebmOpus = 'webm/opus',
Opus = 'opus',
}
2021-08-16 15:19:31 +05:30
interface StreamOptions {
2021-08-20 12:06:12 +05:30
smooth : boolean
2021-08-16 15:19:31 +05:30
}
2021-08-20 12:06:12 +05:30
interface InfoData{
LiveStreamData : {
isLive : boolean
dashManifestUrl : string
hlsManifestUrl : string
}
html5player : string
format : any[]
video_details : any
2021-08-16 15:19:31 +05:30
}
2021-08-20 12:06:12 +05:30
function parseAudioFormats(formats : any[]){
2021-08-18 17:11:37 +05:30
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-20 12:06:12 +05:30
export async function stream(url : string, options : StreamOptions = { smooth : false }): Promise<PassThrough>{
2021-08-16 15:19:31 +05:30
let info = await video_info(url)
let final: any[] = [];
2021-08-20 12:06:12 +05:30
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) {
return await live_stream(info as InfoData, options.smooth)
}
2021-08-16 15:19:31 +05:30
2021-08-20 12:06:12 +05:30
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
final.push(audioFormat[audioFormat.length - 1])
}
else{
final.push(opusFormats[opusFormats.length - 1])
2021-08-18 17:11:37 +05:30
}
2021-08-16 15:19:31 +05:30
2021-08-20 12:06:12 +05:30
if(final.length === 0) final.push(info.format[info.format.length - 1])
let piping_stream = got.stream(final[0].url, {
retry : 5,
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'
},
agent : {
https : new https.Agent({ keepAlive : true })
},
http2 : true
})
let playing_stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
piping_stream.pipe(playing_stream)
return playing_stream
}
export async function stream_from_info(info : InfoData, options : StreamOptions){
let final: any[] = [];
if(info.LiveStreamData.isLive === true) {
return await live_stream(info as InfoData, options.smooth)
2021-08-16 15:19:31 +05:30
}
2021-08-20 12:06:12 +05:30
let audioFormat = parseAudioFormats(info.format)
let opusFormats = filterFormat(audioFormat, "opus")
if(opusFormats.length === 0){
final.push(audioFormat[audioFormat.length - 1])
}
else{
final.push(opusFormats[opusFormats.length - 1])
}
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
}
2021-08-20 12:06:12 +05:30
export function stream_type(info:InfoData): StreamType{
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null) return StreamType.Arbitrary
else return StreamType.WebmOpus
}
async function live_stream(info : InfoData, smooth : boolean): Promise<PassThrough>{
let res_144 : FormatInterface = {
url : '',
targetDurationSec : 0,
maxDvrDurationSec : 0
}
info.format.forEach((format) => {
if(format.qualityLabel === '144p') res_144 = format
else return
2021-08-18 17:11:37 +05:30
})
2021-08-20 12:06:12 +05:30
let stream : LiveStreaming | LiveEnded
if(info.video_details.duration === '0') {
stream = new LiveStreaming((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1], smooth)
}
else {
stream = new LiveEnded((res_144.url.length !== 0) ? res_144 : info.format[info.format.length - 1])
}
return stream.stream
2021-08-16 15:19:31 +05:30
}