Files
play-dl-test/play-dl/YouTube/stream.ts

109 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-12-27 12:17:24 +05:30
import { request_stream } from '../Request';
2021-11-18 13:06:55 +05:30
import { LiveStream, Stream } from './classes/LiveStream';
2021-12-13 09:45:34 +05:30
import { SeekStream } from './classes/SeekStream';
import { InfoData, StreamInfoData } from './utils/constants';
import { video_stream_info } from './utils/extractor';
2021-12-27 12:17:24 +05:30
import { URL } from 'node:url';
2021-08-16 15:19:31 +05:30
2021-09-17 14:36:32 +05:30
export enum StreamType {
2021-08-20 12:06:12 +05:30
Arbitrary = 'arbitrary',
2021-09-13 14:13:36 +09:00
Raw = 'raw',
OggOpus = 'ogg/opus',
WebmOpus = 'webm/opus',
2021-09-17 14:36:32 +05:30
Opus = 'opus'
2021-08-20 12:06:12 +05:30
}
2021-08-16 15:19:31 +05:30
2021-09-24 12:49:39 +05:30
export interface StreamOptions {
2021-12-14 15:01:10 +05:30
seekMode?: 'precise' | 'granular';
seek?: number;
2021-09-24 12:49:39 +05:30
quality?: number;
2021-12-26 15:34:31 +05:30
language?: string;
2021-11-01 15:32:51 +05:30
htmldata?: boolean;
2021-09-24 12:49:39 +05:30
}
2021-09-29 20:23:16 +05:30
/**
* Command to find audio formats from given format array
* @param formats Formats to search from
* @returns Audio Formats array
*/
export function parseAudioFormats(formats: any[]) {
2021-09-17 14:36:32 +05:30
const result: any[] = [];
2021-08-18 17:11:37 +05:30
formats.forEach((format) => {
2021-09-17 14:36:32 +05:30
const type = format.mimeType as string;
if (type.startsWith('audio')) {
format.codec = type.split('codecs="')[1].split('"')[0];
format.container = type.split('audio/')[1].split(';')[0];
result.push(format);
2021-08-18 17:11:37 +05:30
}
2021-09-17 14:36:32 +05:30
});
return result;
2021-08-16 15:19:31 +05:30
}
2021-09-29 20:23:16 +05:30
/**
* Type for YouTube Stream
*/
2021-12-13 09:45:34 +05:30
export type YouTubeStream = Stream | LiveStream | SeekStream;
2021-09-29 20:23:16 +05:30
/**
* Stream command for YouTube
* @param url YouTube URL
2021-12-07 10:50:42 +05:30
* @param options lets you add quality for stream
2021-09-29 20:23:16 +05:30
* @returns Stream class with type and stream for playing.
*/
2021-09-27 22:20:50 +05:30
export async function stream(url: string, options: StreamOptions = {}): Promise<YouTubeStream> {
2021-12-26 15:34:31 +05:30
const info = await video_stream_info(url, { htmldata: options.htmldata, language: options.language });
return await stream_from_info(info, options);
2021-08-20 12:06:12 +05:30
}
2021-09-29 20:23:16 +05:30
/**
2021-10-17 21:41:16 +02:00
* Stream command for YouTube using info from video_info or decipher_info function.
2021-09-29 20:23:16 +05:30
* @param info video_info data
2021-12-07 10:50:42 +05:30
* @param options lets you add quality for stream
2021-09-29 20:23:16 +05:30
* @returns Stream class with type and stream for playing.
*/
export async function stream_from_info(
info: InfoData | StreamInfoData,
options: StreamOptions = {}
): Promise<YouTubeStream> {
2021-09-17 14:36:32 +05:30
const final: any[] = [];
if (
info.LiveStreamData.isLive === true &&
2021-11-18 15:38:25 +05:30
info.LiveStreamData.dashManifestUrl !== null &&
2021-11-06 18:26:29 +05:30
info.video_details.durationInSec === 0
2021-09-17 14:36:32 +05:30
) {
2021-11-18 13:06:55 +05:30
return new LiveStream(
2021-09-17 14:36:32 +05:30
info.LiveStreamData.dashManifestUrl,
2021-11-18 15:38:25 +05:30
info.format[info.format.length - 1].targetDurationSec as number,
2021-09-17 14:36:32 +05:30
info.video_details.url
);
2021-08-16 15:19:31 +05:30
}
2021-08-20 12:06:12 +05:30
2021-09-17 14:36:32 +05:30
const audioFormat = parseAudioFormats(info.format);
2021-09-24 13:15:07 +05:30
if (typeof options.quality !== 'number') options.quality = audioFormat.length - 1;
2021-09-24 12:49:39 +05:30
else if (options.quality <= 0) options.quality = 0;
2021-09-24 13:15:07 +05:30
else if (options.quality >= audioFormat.length) options.quality = audioFormat.length - 1;
2021-10-09 16:18:05 +05:30
if (audioFormat.length !== 0) final.push(audioFormat[options.quality]);
else final.push(info.format[info.format.length - 1]);
2021-09-24 12:49:39 +05:30
let type: StreamType =
2021-10-09 16:18:05 +05:30
final[0].codec === 'opus' && final[0].container === 'webm' ? StreamType.WebmOpus : StreamType.Arbitrary;
2021-12-27 12:17:24 +05:30
await request_stream(`https://${new URL(final[0].url).host}/generate_204`)
2021-12-14 15:01:10 +05:30
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}]`);
2021-12-13 09:45:34 +05:30
return new SeekStream(
final[0].url,
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
options
2021-12-14 15:01:10 +05:30
);
} else throw new Error('Seek is only supported in Webm Opus Files.');
} else
return new Stream(
final[0].url,
type,
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
options
);
2021-08-18 17:11:37 +05:30
}