Some improvements in stream

Improve parameter of Stream
This commit is contained in:
Killer069 2021-09-07 13:06:04 +05:30 committed by GitHub
commit dd10155058
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 24 deletions

View File

@ -52,11 +52,16 @@ let id = extractID(url)
}) // This creates resource for playing
```
### stream_from_info(info : `infoData`)
### stream_from_info(info : `infoData`, cookie? : `string`)
*This is basic to create a youtube stream from a info [ from [video_info](https://github.com/play-dl/play-dl#video_infourl--string) function ].*
**[Cookies](https://github.com/play-dl/play-dl/discussions/34) are optional and are required for playing age restricted videos.**
**Note :** cookies are required for retrying purposes.
```js
let info = await video_info("url")
let source = await stream_from_info(info) // This will create a stream Class which contains type and stream to be played.
let source = await stream_from_info(info, cookie) // This will create a stream Class which contains type and stream to be played and also give cookies if retrying.
let resource = createAudioResource(source.stream, {
inputType : source.type
}) // This creates resource for playing

View File

@ -17,17 +17,13 @@ client.on('messageCreate', async message => {
})
let args = message.content.split('play ')[1].split(' ')[0]
let stream = await play.stream(args, {
cookie : COOKIE
})
let stream = await play.stream(args, COOKIE)
/*
OR if you want to get info about youtube link and then stream it
let yt_info = await play.video_info(args, {
cookie : COOKIE
})
let yt_info = await play.video_info(args, COOKIE)
console.log(yt_info.video_details.title)
let stream = await play.stream_from_info(yt_info)
let stream = await play.stream_from_info(yt_info, COOKIE)
*/
let resource = createAudioResource(stream.stream, {

View File

@ -115,21 +115,19 @@ export class Stream {
private url : string
private bytes_count : number;
private per_sec_bytes : number;
private duration : number;
private timer : NodeJS.Timer | 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.type = type
this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 })
this.bytes_count = 0
this.per_sec_bytes = 0
this.per_sec_bytes = Math.ceil(contentLength / duration)
this.timer = null
this.request = null
this.stream.on('close', () => {
this.cleanup()
})
this.duration = duration
this.loop()
}
@ -141,7 +139,6 @@ export class Stream {
this.timer = null
this.url = ''
this.bytes_count = 0
this.per_sec_bytes = 0
}
private loop(){
@ -157,11 +154,6 @@ export class Stream {
})
this.request = stream
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) => {
this.stream.emit('error', err)
@ -170,7 +162,7 @@ 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) && this.per_sec_bytes !== 0){
if(absolute_bytes > (this.per_sec_bytes * 300)){
stream.destroy()
}
})

View File

@ -50,7 +50,7 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
return 0
})
if(resp === 0){
return await stream(info.video_details.url)
return await stream(info.video_details.url, cookie)
}
else if(typeof resp !== "number") resp.destroy()
@ -71,10 +71,10 @@ export async function stream(url : string, cookie? : string): Promise<Stream | L
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, cookie? : string): Promise<Stream | LiveStreaming>{
let final: any[] = [];
let type : StreamType;
if(info.LiveStreamData.isLive === true && info.LiveStreamData.hlsManifestUrl !== null && info.video_details.durationInSec === '0') {
@ -90,7 +90,7 @@ export async function stream_from_info(info : InfoData): Promise<Stream | LiveSt
return 0
})
if(resp === 0){
return await stream(info.video_details.url)
return await stream(info.video_details.url, cookie)
}
else if(typeof resp !== "number") resp.destroy()
@ -111,7 +111,7 @@ export async function stream_from_info(info : InfoData): Promise<Stream | LiveSt
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){