Files
play-dl-test/play-dl/YouTube/classes/LiveStream.ts

235 lines
7.7 KiB
TypeScript
Raw Normal View History

2021-09-17 14:36:32 +05:30
import { PassThrough } from 'stream';
2021-09-13 00:31:52 +05:30
import { IncomingMessage } from 'http';
2021-09-29 20:23:16 +05:30
import { parseAudioFormats, StreamOptions, StreamType } from '../stream';
import { Proxy, request, request_stream } from '../utils/request';
2021-09-03 17:55:21 +05:30
import { video_info } from '..';
2021-08-20 12:06:12 +05:30
2021-09-17 14:36:32 +05:30
export interface FormatInterface {
url: string;
targetDurationSec: number;
maxDvrDurationSec: number;
2021-08-20 12:06:12 +05:30
}
2021-09-21 22:04:45 +05:30
export class LiveStreaming {
2021-09-24 12:49:39 +05:30
stream: PassThrough;
2021-09-17 14:36:32 +05:30
type: StreamType;
private base_url: string;
private url: string;
private interval: number;
private packet_count: number;
private timer: NodeJS.Timer | null;
private video_url: string;
private dash_timer: NodeJS.Timer | null;
private segments_urls: string[];
private request: IncomingMessage | null;
constructor(dash_url: string, target_interval: number, video_url: string) {
2021-09-24 12:49:39 +05:30
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
2021-09-17 14:36:32 +05:30
this.type = StreamType.Arbitrary;
this.url = dash_url;
this.base_url = '';
this.segments_urls = [];
this.packet_count = 0;
this.request = null;
this.timer = null;
this.video_url = video_url;
this.interval = target_interval * 1000 || 0;
2021-09-03 17:55:21 +05:30
this.dash_timer = setTimeout(() => {
2021-09-17 14:36:32 +05:30
this.dash_updater();
}, 1800000);
2021-09-21 22:04:45 +05:30
this.stream.on('close', () => {
2021-09-17 14:36:32 +05:30
this.cleanup();
});
2021-09-17 14:36:32 +05:30
this.start();
2021-08-20 12:06:12 +05:30
}
2021-09-17 14:36:32 +05:30
private async dash_updater() {
const info = await video_info(this.video_url);
if (
info.LiveStreamData.isLive === true &&
info.LiveStreamData.hlsManifestUrl !== null &&
2021-09-28 19:20:58 +05:30
info.video_details.durationInSec === 0
2021-09-17 14:36:32 +05:30
) {
this.url = info.LiveStreamData.dashManifestUrl;
2021-09-03 17:55:21 +05:30
}
this.dash_timer = setTimeout(() => {
2021-09-17 14:36:32 +05:30
this.dash_updater();
}, 1800000);
2021-09-03 17:55:21 +05:30
}
2021-09-17 14:36:32 +05:30
private async dash_getter() {
const response = await request(this.url);
const audioFormat = response
.split('<AdaptationSet id="0"')[1]
.split('</AdaptationSet>')[0]
.split('</Representation>');
if (audioFormat[audioFormat.length - 1] === '') audioFormat.pop();
this.base_url = audioFormat[audioFormat.length - 1].split('<BaseURL>')[1].split('</BaseURL>')[0];
const list = audioFormat[audioFormat.length - 1].split('<SegmentList>')[1].split('</SegmentList>')[0];
this.segments_urls = list.replace(new RegExp('<SegmentURL media="', 'g'), '').split('"/>');
if (this.segments_urls[this.segments_urls.length - 1] === '') this.segments_urls.pop();
2021-08-20 12:06:12 +05:30
}
2021-09-17 14:36:32 +05:30
private cleanup() {
clearTimeout(this.timer as NodeJS.Timer);
clearTimeout(this.dash_timer as NodeJS.Timer);
2021-09-21 22:04:45 +05:30
this.request?.unpipe(this.stream);
2021-09-17 14:36:32 +05:30
this.request?.destroy();
this.dash_timer = null;
this.video_url = '';
this.request = null;
this.timer = null;
this.url = '';
this.base_url = '';
this.segments_urls = [];
this.packet_count = 0;
this.interval = 0;
2021-08-20 12:06:12 +05:30
}
2021-09-17 14:36:32 +05:30
private async start() {
2021-09-21 22:04:45 +05:30
if (this.stream.destroyed) {
2021-09-17 14:36:32 +05:30
this.cleanup();
return;
2021-08-21 18:55:45 +05:30
}
2021-09-17 14:36:32 +05:30
await this.dash_getter();
if (this.segments_urls.length > 3) this.segments_urls.splice(0, this.segments_urls.length - 3);
if (this.packet_count === 0) this.packet_count = Number(this.segments_urls[0].split('sq/')[1].split('/')[0]);
for await (const segment of this.segments_urls) {
if (Number(segment.split('sq/')[1].split('/')[0]) !== this.packet_count) {
continue;
2021-08-27 15:08:23 +05:30
}
2021-09-17 14:36:32 +05:30
await new Promise(async (resolve, reject) => {
const stream = await request_stream(this.base_url + segment).catch((err: Error) => err);
if (stream instanceof Error) {
2021-09-21 22:04:45 +05:30
this.stream.emit('error', stream);
2021-09-17 14:36:32 +05:30
return;
2021-09-15 17:30:57 +05:30
}
2021-09-17 14:36:32 +05:30
this.request = stream;
2021-09-21 22:04:45 +05:30
stream.pipe(this.stream, { end: false });
stream.on('end', () => {
2021-09-17 14:36:32 +05:30
this.packet_count++;
resolve('');
});
stream.once('error', (err) => {
2021-09-21 22:04:45 +05:30
this.stream.emit('error', err);
2021-09-17 14:36:32 +05:30
});
});
2021-08-20 12:06:12 +05:30
}
2021-08-27 15:08:23 +05:30
this.timer = setTimeout(() => {
2021-09-17 14:36:32 +05:30
this.start();
}, this.interval);
2021-08-20 12:06:12 +05:30
}
}
2021-09-29 20:23:16 +05:30
/**
* Class for YouTube Stream
*/
2021-09-21 22:04:45 +05:30
export class Stream {
2021-09-24 12:49:39 +05:30
stream: PassThrough;
2021-09-17 14:36:32 +05:30
type: StreamType;
private url: string;
private bytes_count: number;
private per_sec_bytes: number;
private content_length: number;
private video_url: string;
private cookie: string;
private data_ended: boolean;
private playing_count: number;
private quality: number;
2021-09-29 20:23:16 +05:30
private proxy: Proxy[];
2021-09-17 14:36:32 +05:30
private request: IncomingMessage | null;
constructor(
url: string,
type: StreamType,
duration: number,
contentLength: number,
video_url: string,
cookie: string,
2021-09-29 20:23:16 +05:30
options: StreamOptions
2021-09-17 14:36:32 +05:30
) {
2021-09-21 22:04:45 +05:30
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
2021-09-17 14:36:32 +05:30
this.url = url;
2021-09-29 20:23:16 +05:30
this.quality = options.quality as number;
this.proxy = options.proxy || [];
2021-09-17 14:36:32 +05:30
this.type = type;
this.bytes_count = 0;
this.video_url = video_url;
this.cookie = cookie;
this.per_sec_bytes = Math.ceil(contentLength / duration);
this.content_length = contentLength;
this.request = null;
this.data_ended = false;
this.playing_count = 0;
2021-09-21 22:04:45 +05:30
this.stream.on('close', () => {
2021-09-17 14:36:32 +05:30
this.cleanup();
});
2021-09-21 22:04:45 +05:30
this.stream.on('pause', () => {
2021-09-10 10:14:44 +05:30
this.playing_count++;
2021-09-17 14:36:32 +05:30
if (this.data_ended) {
this.bytes_count = 0;
this.per_sec_bytes = 0;
this.cleanup();
2021-09-21 22:04:45 +05:30
this.stream.removeAllListeners('pause');
2021-09-17 14:36:32 +05:30
} else if (this.playing_count === 280) {
this.playing_count = 0;
this.loop();
2021-09-10 10:14:44 +05:30
}
2021-09-17 14:36:32 +05:30
});
this.loop();
2021-08-24 12:50:06 +05:30
}
2021-09-17 14:36:32 +05:30
private async retry() {
2021-09-29 20:23:16 +05:30
const info = await video_info(this.video_url, { cookie: this.cookie, proxy: this.proxy });
const audioFormat = parseAudioFormats(info.format);
this.url = audioFormat[this.quality].url;
2021-09-13 10:23:18 +05:30
}
2021-09-17 14:36:32 +05:30
private cleanup() {
2021-09-21 22:04:45 +05:30
this.request?.unpipe(this.stream);
2021-09-17 14:36:32 +05:30
this.request?.destroy();
this.request = null;
this.url = '';
2021-08-24 12:50:06 +05:30
}
2021-09-17 14:36:32 +05:30
private async loop() {
2021-09-21 22:04:45 +05:30
if (this.stream.destroyed) {
2021-09-17 14:36:32 +05:30
this.cleanup();
return;
2021-08-24 12:50:06 +05:30
}
2021-09-17 14:36:32 +05:30
const end: number = this.bytes_count + this.per_sec_bytes * 300;
const stream = await request_stream(this.url, {
headers: {
range: `bytes=${this.bytes_count}-${end >= this.content_length ? '' : end}`
2021-08-24 12:50:06 +05:30
}
2021-09-17 14:36:32 +05:30
}).catch((err: Error) => err);
if (stream instanceof Error) {
2021-09-21 22:04:45 +05:30
this.stream.emit('error', stream);
2021-09-17 14:36:32 +05:30
this.data_ended = true;
this.bytes_count = 0;
this.per_sec_bytes = 0;
this.cleanup();
return;
2021-09-15 12:24:56 +05:30
}
2021-09-17 14:36:32 +05:30
if (Number(stream.statusCode) >= 400) {
this.cleanup();
await this.retry();
this.loop();
return;
2021-09-13 10:23:18 +05:30
}
2021-09-17 14:36:32 +05:30
this.request = stream;
2021-09-21 22:04:45 +05:30
stream.pipe(this.stream, { end: false });
2021-08-31 17:28:37 +05:30
stream.once('error', async (err) => {
this.cleanup();
2021-09-28 20:56:10 +05:30
await this.retry();
this.loop();
2021-09-17 14:36:32 +05:30
});
2021-08-31 17:28:37 +05:30
2021-09-03 17:55:21 +05:30
stream.on('data', (chunk: any) => {
2021-09-17 14:36:32 +05:30
this.bytes_count += chunk.length;
});
2021-08-24 12:50:06 +05:30
2021-09-10 10:14:44 +05:30
stream.on('end', () => {
2021-09-17 14:36:32 +05:30
if (end >= this.content_length) this.data_ended = true;
});
}
2021-08-21 15:03:43 +05:30
}