Changes requested by cjh980402

This commit is contained in:
killer069 2021-09-08 21:19:55 +05:30
parent 81043ef096
commit 7dcf3d09d1
2 changed files with 8 additions and 17 deletions

View File

@ -115,7 +115,7 @@ export class Stream {
private url : string
private bytes_count : number;
private per_sec_bytes : number;
private timer : NodeJS.Timer | null
private content_length : number
private request : Request | null
constructor(url : string, type : StreamType, duration : number, contentLength : number){
this.url = url
@ -123,7 +123,7 @@ export class Stream {
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.bytes_count = 0
this.per_sec_bytes = Math.ceil(contentLength / duration)
this.timer = null
this.content_length = contentLength
this.request = null
this.stream.on('close', () => {
this.cleanup()
@ -132,13 +132,12 @@ export class Stream {
}
private cleanup(){
clearTimeout(this.timer as NodeJS.Timer)
this.request?.unpipe(this.stream)
this.request?.destroy()
this.request = null
this.timer = null
this.url = ''
this.bytes_count = 0
this.per_sec_bytes = 0
}
private loop(){
@ -146,10 +145,10 @@ export class Stream {
this.cleanup()
return
}
let absolute_bytes : number = 0
let end : number = this.bytes_count + this.per_sec_bytes * 300;
let stream = got.stream(this.url, {
headers : {
"range" : `bytes=${this.bytes_count}-`
"range" : `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
}
})
this.request = stream
@ -160,20 +159,12 @@ export class Stream {
})
stream.on('data', (chunk: any) => {
absolute_bytes += chunk.length
this.bytes_count += chunk.length
if(absolute_bytes > (this.per_sec_bytes * 300)){
stream.destroy()
}
})
stream.on('end', () => {
this.cleanup()
if(end < this.content_length) this.loop()
else this.cleanup()
})
this.timer = setTimeout(() => {
this.request?.unpipe(this.stream)
this.loop()
}, 280 * 1000)
}
}

View File

@ -52,7 +52,7 @@ export async function video_basic_info(url : string, cookie? : string){
let player_response = JSON.parse(body.split("var ytInitialPlayerResponse = ")[1].split("}};")[0] + '}}')
let initial_response = JSON.parse(body.split("var ytInitialData = ")[1].split("}};")[0] + '}}')
let badge = initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer?.owner?.videoOwnerRenderer?.badges && initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer?.owner?.videoOwnerRenderer?.badges[0]
if(player_response.playabilityStatus.status !== 'OK') throw new Error(`While getting info from url\n${player_response.playabilityStatus.reason || player_response.playabilityStatus.messages[0]}`)
if(player_response.playabilityStatus.status !== 'OK') throw new Error(`While getting info from url\n${player_response.playabilityStatus.errorScreen.playerErrorMessageRenderer?.reason.simpleText ?? player_response.playabilityStatus.errorScreen.playerKavRenderer?.reason.simpleText}`)
let html5player = `https://www.youtube.com${body.split('"jsUrl":"')[1].split('"')[0]}`
let format = []
let vid = player_response.videoDetails