From 3125a8b77282caeec133fa94b9fcc05d8977a318 Mon Sep 17 00:00:00 2001 From: cjh980402 <9804cjh@naver.com> Date: Tue, 7 Sep 2021 11:43:21 +0900 Subject: [PATCH 1/6] Improve parameter of Stream --- play-dl/YouTube/classes/LiveStream.ts | 14 +++----------- play-dl/YouTube/stream.ts | 4 ++-- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index 510e227..f4f6599 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 = 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..61e5384 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -71,7 +71,7 @@ export async function stream(url : string, cookie? : string): Promise{ @@ -111,7 +111,7 @@ export async function stream_from_info(info : InfoData): Promise Date: Tue, 7 Sep 2021 11:51:16 +0900 Subject: [PATCH 2/6] Add ceil operation --- play-dl/YouTube/classes/LiveStream.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/play-dl/YouTube/classes/LiveStream.ts b/play-dl/YouTube/classes/LiveStream.ts index f4f6599..0e1e391 100644 --- a/play-dl/YouTube/classes/LiveStream.ts +++ b/play-dl/YouTube/classes/LiveStream.ts @@ -122,7 +122,7 @@ export class Stream { this.type = type this.stream = new PassThrough({ highWaterMark : 10 * 1000 * 1000 }) this.bytes_count = 0 - this.per_sec_bytes = contentLength / duration + this.per_sec_bytes = Math.ceil(contentLength / duration) this.timer = null this.request = null this.stream.on('close', () => { From 3cd8d36aa784c5a4d160318ae90213b81d5ed4ca Mon Sep 17 00:00:00 2001 From: Killer069 <65385476+killer069@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:00:17 +0530 Subject: [PATCH 3/6] added cookie parameter in retry options --- play-dl/YouTube/stream.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 61e5384..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 Date: Tue, 7 Sep 2021 13:02:26 +0530 Subject: [PATCH 4/6] updated YouTube - docs --- docs/YouTube/README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/YouTube/README.md b/docs/YouTube/README.md index 42b22d6..37facff 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 From 8d30d5dc805cce30c6ae4418f6e6d2b52de162d1 Mon Sep 17 00:00:00 2001 From: Killer069 <65385476+killer069@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:02:59 +0530 Subject: [PATCH 5/6] YouTube - docs --- docs/YouTube/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/YouTube/README.md b/docs/YouTube/README.md index 37facff..247c684 100644 --- a/docs/YouTube/README.md +++ b/docs/YouTube/README.md @@ -57,7 +57,7 @@ let id = extractID(url) **[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. +**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. From 1efd6eacf12d30c5a284d9a55e1e515b63ee46d0 Mon Sep 17 00:00:00 2001 From: Killer069 <65385476+killer069@users.noreply.github.com> Date: Tue, 7 Sep 2021 13:04:47 +0530 Subject: [PATCH 6/6] updated YouTube cookies example --- examples/YouTube/play - cookies.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) 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, {