Improve parameter of Stream

This commit is contained in:
cjh980402 2021-09-07 11:43:21 +09:00
parent 8e62b5c060
commit 3125a8b772
2 changed files with 5 additions and 13 deletions

View File

@ -115,21 +115,19 @@ export class Stream {
private url : string private url : string
private bytes_count : number; private bytes_count : number;
private per_sec_bytes : number; private per_sec_bytes : number;
private duration : number;
private timer : NodeJS.Timer | null private timer : NodeJS.Timer | null
private request : Request | null private request : Request | null
constructor(url : string, type : StreamType, duration : number){ constructor(url : string, type : StreamType, duration : number, contentLength : number){
this.url = url this.url = url
this.type = type this.type = type
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.bytes_count = 0 this.bytes_count = 0
this.per_sec_bytes = 0 this.per_sec_bytes = contentLength / duration
this.timer = null this.timer = null
this.request = null this.request = null
this.stream.on('close', () => { this.stream.on('close', () => {
this.cleanup() this.cleanup()
}) })
this.duration = duration
this.loop() this.loop()
} }
@ -141,7 +139,6 @@ export class Stream {
this.timer = null this.timer = null
this.url = '' this.url = ''
this.bytes_count = 0 this.bytes_count = 0
this.per_sec_bytes = 0
} }
private loop(){ private loop(){
@ -157,11 +154,6 @@ export class Stream {
}) })
this.request = stream this.request = stream
stream.pipe(this.stream, { end : false }) stream.pipe(this.stream, { end : false })
stream.once('data', () => {
if(this.per_sec_bytes === 0){
this.per_sec_bytes = Math.ceil((stream.downloadProgress.total as number)/this.duration)
}
})
stream.once('error', (err) => { stream.once('error', (err) => {
this.stream.emit('error', err) this.stream.emit('error', err)
@ -170,7 +162,7 @@ export class Stream {
stream.on('data', (chunk: any) => { stream.on('data', (chunk: any) => {
absolute_bytes += chunk.length absolute_bytes += chunk.length
this.bytes_count += chunk.length this.bytes_count += chunk.length
if(absolute_bytes > (this.per_sec_bytes * 300) && this.per_sec_bytes !== 0){ if(absolute_bytes > (this.per_sec_bytes * 300)){
stream.destroy() stream.destroy()
} }
}) })

View File

@ -71,7 +71,7 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
final.push(info.format[info.format.length - 1]) final.push(info.format[info.format.length - 1])
} }
return new Stream(final[0].url, type, info.video_details.durationInSec) return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength))
} }
export async function stream_from_info(info : InfoData): Promise<Stream | LiveStreaming>{ export async function stream_from_info(info : InfoData): Promise<Stream | LiveStreaming>{
@ -111,7 +111,7 @@ export async function stream_from_info(info : InfoData): Promise<Stream | LiveSt
final.push(info.format[info.format.length - 1]) final.push(info.format[info.format.length - 1])
} }
return new Stream(final[0].url, type, info.video_details.durationInSec) return new Stream(final[0].url, type, info.video_details.durationInSec, Number(final[0].contentLength))
} }
function filterFormat(formats : any[], codec : string){ function filterFormat(formats : any[], codec : string){