diff --git a/docs/YouTube/README.md b/docs/YouTube/README.md index 42b22d6..247c684 100644 --- a/docs/YouTube/README.md +++ b/docs/YouTube/README.md @@ -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 diff --git a/examples/YouTube/play - cookies.js b/examples/YouTube/play - cookies.js index d1be96d..4500485 100644 --- a/examples/YouTube/play - cookies.js +++ b/examples/YouTube/play - cookies.js @@ -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, { diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 510e227..0e1e391 100644 --- a/play-dl/YouTube/classes/LiveStream.ts +++ b/play-dl/YouTube/classes/LiveStream.ts @@ -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() } }) diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 3d580a3..7de9d70 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -50,7 +50,7 @@ export async function stream(url : string, cookie? : string): Promise{ +export async function stream_from_info(info : InfoData, cookie? : string): Promise{ 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