From 062a0e78515bbd79607e61a5b380bb57675a919d Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Wed, 19 Jan 2022 09:29:30 +0530 Subject: [PATCH 1/2] Fixed some seek issues --- play-dl/YouTube/classes/SeekStream.ts | 2 +- play-dl/YouTube/classes/WebmSeeker.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/play-dl/YouTube/classes/SeekStream.ts b/play-dl/YouTube/classes/SeekStream.ts index 3785d0f..8e86be1 100644 --- a/play-dl/YouTube/classes/SeekStream.ts +++ b/play-dl/YouTube/classes/SeekStream.ts @@ -148,7 +148,7 @@ export class SeekStream { this.timer.reuse(); return this.seek(); } - const bytes = this.stream.seek(); + const bytes = this.stream.seek(this.content_length); if (bytes instanceof Error) { this.stream.emit('error', bytes); this.bytes_count = 0; diff --git a/play-dl/YouTube/classes/WebmSeeker.ts b/play-dl/YouTube/classes/WebmSeeker.ts index d928b7c..95e77de 100644 --- a/play-dl/YouTube/classes/WebmSeeker.ts +++ b/play-dl/YouTube/classes/WebmSeeker.ts @@ -75,7 +75,7 @@ export class WebmSeeker extends Duplex { _read() {} - seek(): Error | number { + seek(content_length: number): Error | number { let clusterlength = 0, position = 0; let time_left = (this.sec - this.time) * 1000 || 0; @@ -86,8 +86,7 @@ export class WebmSeeker extends Duplex { const data = this.header.segment.cues[i]; if (Math.floor((data.time as number) / 1000) === this.time) { position = data.position as number; - if (this.header.segment.cues.length > 1) - clusterlength = this.header.segment.cues[i + 1].position! - position - 1; + clusterlength = (this.header.segment.cues[i + 1]?.position || content_length) - position - 1; break; } else continue; } @@ -145,6 +144,7 @@ export class WebmSeeker extends Duplex { // stop parsing the header once we have found the correct cue if ( ebmlID.name === 'cueClusterPosition' && + this.header.segment.cues!.length > 2 && this.time === (this.header.segment.cues!.at(-2)!.time as number) / 1000 ) this.emit('headComplete'); From 54210d1c3fdbfa8171c132e2557d25a4ca0f675f Mon Sep 17 00:00:00 2001 From: killer069 <65385476+killer069@users.noreply.github.com> Date: Wed, 19 Jan 2022 09:40:50 +0530 Subject: [PATCH 2/2] Some improvements --- play-dl/YouTube/stream.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/play-dl/YouTube/stream.ts b/play-dl/YouTube/stream.ts index 52a9621..648e3b3 100644 --- a/play-dl/YouTube/stream.ts +++ b/play-dl/YouTube/stream.ts @@ -85,19 +85,18 @@ export async function stream_from_info( let type: StreamType = final[0].codec === 'opus' && final[0].container === 'webm' ? StreamType.WebmOpus : StreamType.Arbitrary; await request_stream(`https://${new URL(final[0].url).host}/generate_204`); - if (options.seek) { - if (type === StreamType.WebmOpus) { - if (options.seek >= info.video_details.durationInSec || options.seek <= 0) - throw new Error(`Seeking beyond limit. [ 1 - ${info.video_details.durationInSec - 1}]`); - return new SeekStream( - final[0].url, - info.video_details.durationInSec, - final[0].indexRange.end, - Number(final[0].contentLength), - info.video_details.url, - options - ); - } else throw new Error('Seek is only supported in Webm Opus Files.'); + if (type === StreamType.WebmOpus) { + options.seek ??= 0 + if (options.seek >= info.video_details.durationInSec || options.seek < 0) + throw new Error(`Seeking beyond limit. [ 0 - ${info.video_details.durationInSec - 1}]`); + return new SeekStream( + final[0].url, + info.video_details.durationInSec, + final[0].indexRange.end, + Number(final[0].contentLength), + info.video_details.url, + options + ); } else return new Stream( final[0].url,