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

129 lines
4.2 KiB
TypeScript
Raw Normal View History

2021-09-17 14:36:32 +05:30
import { video_info } from '.';
import { LiveStreaming, Stream } from './classes/LiveStream';
import { Proxy } from './utils/request';
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 {
quality?: number;
cookie?: string;
proxy?: Proxy[];
2021-09-24 12:49:39 +05:30
}
2021-09-20 17:20:15 +05:30
export interface InfoData {
2021-09-17 14:36:32 +05:30
LiveStreamData: {
isLive: boolean;
dashManifestUrl: string;
hlsManifestUrl: string;
};
html5player: string;
format: any[];
video_details: any;
2021-08-16 15:19:31 +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-09-27 22:20:50 +05:30
export type YouTubeStream = Stream | LiveStreaming;
2021-09-29 20:23:16 +05:30
/**
* Stream command for YouTube
* @param url YouTube URL
* @param options lets you add quality, cookie, proxy support for stream
* @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> {
const info = await video_info(url, { cookie: options.cookie, proxy: options.proxy });
2021-09-17 14:36:32 +05:30
const final: any[] = [];
if (
info.LiveStreamData.isLive === true &&
info.LiveStreamData.hlsManifestUrl !== null &&
2021-09-27 22:20:50 +05:30
info.video_details.durationInSec === 0
2021-09-17 14:36:32 +05:30
) {
return new LiveStreaming(
info.LiveStreamData.dashManifestUrl,
info.format[info.format.length - 1].targetDurationSec,
info.video_details.url
);
2021-08-20 12:06:12 +05:30
}
2021-08-16 15:19:31 +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-09-24 12:49:39 +05:30
final.push(audioFormat[options.quality]);
let type: StreamType =
audioFormat[options.quality].codec === 'opus' && audioFormat[options.quality].container === 'webm'
? StreamType.WebmOpus
: StreamType.Arbitrary;
2021-09-17 14:36:32 +05:30
return new Stream(
final[0].url,
type,
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
options.cookie as string,
2021-09-29 20:23:16 +05:30
options
2021-09-17 14:36:32 +05:30
);
2021-08-20 12:06:12 +05:30
}
2021-09-29 20:23:16 +05:30
/**
* Stream command for YouTube using info from video_info function.
* @param info video_info data
* @param options lets you add quality, cookie, proxy support for stream
* @returns Stream class with type and stream for playing.
*/
2021-09-27 22:20:50 +05:30
export async function stream_from_info(info: InfoData, options: StreamOptions = {}): Promise<YouTubeStream> {
2021-09-17 14:36:32 +05:30
const final: any[] = [];
if (
info.LiveStreamData.isLive === true &&
info.LiveStreamData.hlsManifestUrl !== null &&
info.video_details.durationInSec === '0'
) {
return new LiveStreaming(
info.LiveStreamData.dashManifestUrl,
info.format[info.format.length - 1].targetDurationSec,
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-09-24 12:49:39 +05:30
final.push(audioFormat[options.quality]);
let type: StreamType =
audioFormat[options.quality].codec === 'opus' && audioFormat[options.quality].container === 'webm'
? StreamType.WebmOpus
: StreamType.Arbitrary;
2021-09-17 14:36:32 +05:30
return new Stream(
final[0].url,
type,
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
options.cookie as string,
2021-09-29 20:23:16 +05:30
options
2021-09-17 14:36:32 +05:30
);
2021-08-18 17:11:37 +05:30
}