Quality added

This commit is contained in:
killer069
2021-09-24 12:49:39 +05:30
parent be840ce18b
commit f304d6bcbd
8 changed files with 92 additions and 88 deletions

View File

@@ -11,7 +11,7 @@ export interface FormatInterface {
}
export class LiveStreaming {
stream : PassThrough;
stream: PassThrough;
type: StreamType;
private base_url: string;
private url: string;
@@ -23,7 +23,7 @@ export class LiveStreaming {
private segments_urls: string[];
private request: IncomingMessage | null;
constructor(dash_url: string, target_interval: number, video_url: string) {
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 })
this.stream = new PassThrough({ highWaterMark: 10 * 1000 * 1000 });
this.type = StreamType.Arbitrary;
this.url = dash_url;
this.base_url = '';
@@ -121,7 +121,7 @@ export class LiveStreaming {
}
export class Stream {
stream : PassThrough;
stream: PassThrough;
type: StreamType;
private url: string;
private bytes_count: number;

View File

@@ -9,6 +9,11 @@ export enum StreamType {
Opus = 'opus'
}
export interface StreamOptions {
quality?: number;
cookie?: string;
}
export interface InfoData {
LiveStreamData: {
isLive: boolean;
@@ -33,10 +38,9 @@ function parseAudioFormats(formats: any[]) {
return result;
}
export async function stream(url: string, cookie?: string): Promise<Stream | LiveStreaming> {
const info = await video_info(url, cookie);
export async function stream(url: string, options: StreamOptions = {}): Promise<Stream | LiveStreaming> {
const info = await video_info(url, options.cookie);
const final: any[] = [];
let type: StreamType;
if (
info.LiveStreamData.isLive === true &&
info.LiveStreamData.hlsManifestUrl !== null &&
@@ -50,33 +54,26 @@ export async function stream(url: string, cookie?: string): Promise<Stream | Liv
}
const audioFormat = parseAudioFormats(info.format);
const opusFormats = filterFormat(audioFormat, 'opus');
if (opusFormats.length === 0) {
type = StreamType.Arbitrary;
if (audioFormat.length === 0) {
final.push(info.format[info.format.length - 1]);
} else {
final.push(audioFormat[audioFormat.length - 1]);
}
} else {
type = StreamType.WebmOpus;
final.push(opusFormats[opusFormats.length - 1]);
}
if (!options.quality) options.quality = audioFormat.length - 1;
else if (options.quality <= 0) options.quality = 0;
else if (options.quality > audioFormat.length) options.quality = audioFormat.length - 1;
final.push(audioFormat[options.quality]);
let type: StreamType =
audioFormat[options.quality].codec === 'opus' && audioFormat[options.quality].container === 'webm'
? StreamType.WebmOpus
: StreamType.Arbitrary;
return new Stream(
final[0].url,
type,
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
cookie as string
options.cookie as string
);
}
export async function stream_from_info(info: InfoData, cookie?: string): Promise<Stream | LiveStreaming> {
export async function stream_from_info(info: InfoData, options: StreamOptions = {}): Promise<Stream | LiveStreaming> {
const final: any[] = [];
let type: StreamType;
if (
info.LiveStreamData.isLive === true &&
info.LiveStreamData.hlsManifestUrl !== null &&
@@ -90,34 +87,20 @@ export async function stream_from_info(info: InfoData, cookie?: string): Promise
}
const audioFormat = parseAudioFormats(info.format);
const opusFormats = filterFormat(audioFormat, 'opus');
if (opusFormats.length === 0) {
type = StreamType.Arbitrary;
if (audioFormat.length === 0) {
final.push(info.format[info.format.length - 1]);
} else {
final.push(audioFormat[audioFormat.length - 1]);
}
} else {
type = StreamType.WebmOpus;
final.push(opusFormats[opusFormats.length - 1]);
}
if (!options.quality) options.quality = audioFormat.length - 1;
else if (options.quality <= 0) options.quality = 0;
else if (options.quality > audioFormat.length) options.quality = audioFormat.length - 1;
final.push(audioFormat[options.quality]);
let type: StreamType =
audioFormat[options.quality].codec === 'opus' && audioFormat[options.quality].container === 'webm'
? StreamType.WebmOpus
: StreamType.Arbitrary;
return new Stream(
final[0].url,
type,
info.video_details.durationInSec,
Number(final[0].contentLength),
info.video_details.url,
cookie as string
options.cookie as string
);
}
function filterFormat(formats: any[], codec: string) {
const result: any[] = [];
formats.forEach((format) => {
if (format.codec === codec) result.push(format);
});
return result;
}

View File

@@ -66,10 +66,13 @@ export async function video_basic_info(url: string, cookie?: string) {
initial_response.contents.twoColumnWatchNextResults.results.results.contents[1]?.videoSecondaryInfoRenderer
?.owner?.videoOwnerRenderer?.badges[0];
const html5player = `https://www.youtube.com${body.split('"jsUrl":"')[1].split('"')[0]}`;
const related: any[] = []
initial_response.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results.forEach((res: any) => {
if(res.compactVideoRenderer) related.push(`https://www.youtube.com/watch?v=${res.compactVideoRenderer.videoId}`)
})
const related: any[] = [];
initial_response.contents.twoColumnWatchNextResults.secondaryResults.secondaryResults.results.forEach(
(res: any) => {
if (res.compactVideoRenderer)
related.push(`https://www.youtube.com/watch?v=${res.compactVideoRenderer.videoId}`);
}
);
const format = [];
const vid = player_response.videoDetails;
const microformat = player_response.microformat.playerMicroformatRenderer;
@@ -106,7 +109,7 @@ export async function video_basic_info(url: string, cookie?: string) {
html5player,
format,
video_details,
related_videos : related
related_videos: related
};
}