commit
9b9cea829e
@ -64,7 +64,7 @@ export class SeekStream {
|
|||||||
constructor(url: string, duration: number, contentLength: number, video_url: string, options: StreamOptions) {
|
constructor(url: string, duration: number, contentLength: number, video_url: string, options: StreamOptions) {
|
||||||
this.stream = new WebmSeeker({
|
this.stream = new WebmSeeker({
|
||||||
highWaterMark: 5 * 1000 * 1000,
|
highWaterMark: 5 * 1000 * 1000,
|
||||||
readableObjectMode: true,
|
readableObjectMode: true
|
||||||
});
|
});
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.quality = options.quality as number;
|
this.quality = options.quality as number;
|
||||||
|
|||||||
@ -71,18 +71,19 @@ export class WebmSeeker extends Duplex {
|
|||||||
|
|
||||||
_read() {}
|
_read() {}
|
||||||
|
|
||||||
seek(sec : number): Error | number{
|
seek(sec: number): Error | number {
|
||||||
let clusterlength = 0, position = 0;
|
let clusterlength = 0,
|
||||||
|
position = 0;
|
||||||
const time = Math.floor(sec / 10) * 10;
|
const time = Math.floor(sec / 10) * 10;
|
||||||
let time_left = (sec - time) * 1000 || 0;
|
let time_left = (sec - time) * 1000 || 0;
|
||||||
time_left = Math.round(time_left / 20) * 20;
|
time_left = Math.round(time_left / 20) * 20;
|
||||||
if (!this.header.segment.cues) return new Error('Failed to Parse Cues');
|
if (!this.header.segment.cues) return new Error('Failed to Parse Cues');
|
||||||
|
|
||||||
for (let i = 0; i < this.header.segment.cues.length; i++) {
|
for (let i = 0; i < this.header.segment.cues.length; i++) {
|
||||||
const data = this.header.segment.cues[i]
|
const data = this.header.segment.cues[i];
|
||||||
if (Math.floor((data.time as number) / 1000) === time) {
|
if (Math.floor((data.time as number) / 1000) === time) {
|
||||||
position = data.position as number;
|
position = data.position as number;
|
||||||
clusterlength = this.header.segment.cues[i + 1].position! - position - 1
|
clusterlength = this.header.segment.cues[i + 1].position! - position - 1;
|
||||||
break;
|
break;
|
||||||
} else continue;
|
} else continue;
|
||||||
}
|
}
|
||||||
@ -204,13 +205,14 @@ export class WebmSeeker extends Duplex {
|
|||||||
|
|
||||||
private getClosestBlock(): Error | undefined {
|
private getClosestBlock(): Error | undefined {
|
||||||
if (!this.chunk) return new Error('Chunk is missing');
|
if (!this.chunk) return new Error('Chunk is missing');
|
||||||
this.cursor = 0
|
this.cursor = 0;
|
||||||
let positionFound = false;
|
let positionFound = false;
|
||||||
while(!positionFound){
|
while (!positionFound && this.cursor < this.chunk.length) {
|
||||||
this.cursor = this.chunk.indexOf('a3', this.cursor, 'hex');
|
this.cursor = this.chunk.indexOf('a3', this.cursor, 'hex');
|
||||||
if (this.cursor === -1) return new Error('Failed to find nearest Block.');
|
if (this.cursor === -1) return new Error('Failed to find nearest Block.');
|
||||||
this.cursor ++;
|
this.cursor++;
|
||||||
if(!this.vint_value()) return new Error("Failed to find correct simpleBlock in first chunk")
|
if (!this.vint_value()) return new Error('Failed to find correct simpleBlock in first chunk');
|
||||||
|
if (this.cursor + this.data_length + this.data_length > this.chunk.length) continue;
|
||||||
const data = this.chunk.slice(
|
const data = this.chunk.slice(
|
||||||
this.cursor + this.data_size,
|
this.cursor + this.data_size,
|
||||||
this.cursor + this.data_size + this.data_length
|
this.cursor + this.data_size + this.data_length
|
||||||
@ -219,11 +221,11 @@ export class WebmSeeker extends Duplex {
|
|||||||
if (!track || track.trackType !== 2) return new Error('No audio Track in this webm file.');
|
if (!track || track.trackType !== 2) return new Error('No audio Track in this webm file.');
|
||||||
if ((data[0] & 0xf) === track.trackNumber) {
|
if ((data[0] & 0xf) === track.trackNumber) {
|
||||||
this.cursor += this.data_size + this.data_length;
|
this.cursor += this.data_size + this.data_length;
|
||||||
this.push(data.slice(4))
|
this.push(data.slice(4));
|
||||||
positionFound = true;
|
positionFound = true;
|
||||||
}
|
} else continue;
|
||||||
else continue;
|
|
||||||
}
|
}
|
||||||
|
if (!positionFound) return new Error('Failed to find nearest correct simple Block.');
|
||||||
this.seekfound = true;
|
this.seekfound = true;
|
||||||
return this.readTag();
|
return this.readTag();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,10 +75,7 @@ import { stream as yt_stream, StreamOptions, stream_from_info as yt_stream_info
|
|||||||
import { yt_search } from './YouTube/search';
|
import { yt_search } from './YouTube/search';
|
||||||
import { EventEmitter } from 'stream';
|
import { EventEmitter } from 'stream';
|
||||||
|
|
||||||
async function stream(
|
async function stream(url: string, options: { seek?: number } & StreamOptions): Promise<YouTubeStream>;
|
||||||
url: string,
|
|
||||||
options: { seek?: number } & StreamOptions
|
|
||||||
): Promise<YouTubeStream>;
|
|
||||||
async function stream(url: string, options?: StreamOptions): Promise<YouTubeStream | SoundCloudStream>;
|
async function stream(url: string, options?: StreamOptions): Promise<YouTubeStream | SoundCloudStream>;
|
||||||
/**
|
/**
|
||||||
* Creates a Stream [ YouTube or SoundCloud ] class from a url for playing.
|
* Creates a Stream [ YouTube or SoundCloud ] class from a url for playing.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user