mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-26 08:08:54 +00:00
* refactor: remove dependancies removes node-forge and uuid in favor of Web APIs * refactor!: commonjs to es6 To aid with #93 I will make all my changes in TypeScript instead. This is the first step into making that happen. Used: https://github.com/wessberg/cjstoesm * refactor!: NToken and Signature TS files Bring this PR up to speed with #93 * feat: cross platform cache (WIP) this is untested! should remove idb as dependecy. * feat: EventEmitter polyfill * refactor: remove events * feat: HTTPClient based on Fetch API (WIP) * refactor!: parsers refactor (WIP) Initial TS support for parsers as per #93 This adds several type safety checks to the parser which'll help to ensure valid data is returned by the parser. * refactor!: parsers refactor (WIP) Bring more in line with the existing implementations & make less verbose * refactor!: parser refactor I was overcomplicating things, this is much simpler and compatible with the existing JS API * fix: some missed parsers while refactoring * fix: better type inferance for parseResponse * feat(TS): typesafe YTNode casts * feat: more type safety in YTNode and Parser * refactor: VideoInfo download with fetch & TS (WIP) Again, this also does some work for #93 * fix: LiveChat in VideoInfo * refactor!: more typesafety in parser * refactor!: VideoInfo almost completed * refactor!: player and session refactors - Remove the Player class' dependance on Session. - Add additional context to the Session. * refactor!: move auth logic to Session (WIP) * refactor: TS port for Actions and Innertube My fingers hurt from typing out all those types :-P * refactor: NavigationEndpoint TS this is still a WIP and should be improved. NavigationEndpoint should probably be refactored further. * refactor!: VideoInfo compiles without errors * chore: delete old player * fix: import errors It compiles and runs!! * fix: Utils import fixes * fix: several runtime errors * fix: video streaming * chore: remove console.log debugging Whoops, forgot to remove these before I pushed the previous commit * chore: remove old unused dependencies * fix: typescript errors Now emitting declarations and source maps * refactor: TS feed * chore: delete old Feed * refactor: move streamToIterable into Utils * refactor: AccountManager TS * refactor: FilterableFeed to TS * refactor: InteractionManager to TS * refactor: PlaylistManager to TS * refactor: TabbedFeed to TS * refactor: Music to TS (WIP) more work to be done, see TODO comments * fix: getting the tests to pass (6/12) YouTube.js Tests Search ✓ Should search on YouTube (1152 ms) ✕ Should search on YouTube Music (705 ms) ✕ Should retrieve YouTube search suggestions (722 ms) ✓ Should retrieve YouTube Music search suggestions (233 ms) Comments ✓ Should retrieve comments (585 ms) ✕ Should retrieve next batch of comments (221 ms) ✕ Should retrieve comment replies (1 ms) General ✕ Should retrieve playlist with YouTube (732 ms) ✓ Should retrieve home feed (838 ms) ✓ Should retrieve trending content (543 ms) ✓ Should retrieve video info (639 ms) ✕ Should download video (5 ms) * fix: tests (7/12) YouTube.js Tests Search ✓ Should search on YouTube (1984 ms) ✕ Should search on YouTube Music (1139 ms) ✕ Should retrieve YouTube search suggestions (1433 ms) ✓ Should retrieve YouTube Music search suggestions (529 ms) Comments ✓ Should retrieve comments (324 ms) ✓ Should retrieve next batch of comments (395 ms) ✕ Should retrieve comment replies General ✕ Should retrieve playlist with YouTube (653 ms) ✓ Should retrieve home feed (1085 ms) ✓ Should retrieve trending content (513 ms) ✓ Should retrieve video info (921 ms) ✕ Should download video (3 ms) * fix: download tests (8/12) YouTube.js Tests Search ✓ Should search on YouTube (1293 ms) ✕ Should search on YouTube Music (927 ms) ✕ Should retrieve YouTube search suggestions (1250 ms) ✓ Should retrieve YouTube Music search suggestions (258 ms) Comments ✓ Should retrieve comments (803 ms) ✓ Should retrieve next batch of comments (511 ms) ✕ Should retrieve comment replies General ✕ Should retrieve playlist with YouTube (528 ms) ✓ Should retrieve home feed (1047 ms) ✓ Should retrieve trending content (548 ms) ✓ Should retrieve video info (825 ms) ✓ Should download video (1779 ms) * fix: tests (9/12) YouTube.js Tests Search ✓ Should search on YouTube (1276 ms) ✕ Should search on YouTube Music (955 ms) ✓ Should retrieve YouTube search suggestions (661 ms) ✓ Should retrieve YouTube Music search suggestions (491 ms) Comments ✓ Should retrieve comments (624 ms) ✓ Should retrieve next batch of comments (353 ms) ✕ Should retrieve comment replies General ✕ Should retrieve playlist with YouTube (672 ms) ✓ Should retrieve home feed (1277 ms) ✓ Should retrieve trending content (999 ms) ✓ Should retrieve video info (1106 ms) ✓ Should download video (2514 ms) * feat: key based type validation for parsers * fix: comments tests pass (10/12) YouTube.js Tests Search ✓ Should search on YouTube (938 ms) ✕ Should search on YouTube Music (850 ms) ✓ Should retrieve YouTube search suggestions (528 ms) ✓ Should retrieve YouTube Music search suggestions (224 ms) Comments ✓ Should retrieve comments (518 ms) ✓ Should retrieve next batch of comments (337 ms) ✓ Should retrieve comment replies (358 ms) General ✕ Should retrieve playlist with YouTube (466 ms) ✓ Should retrieve home feed (1051 ms) ✓ Should retrieve trending content (623 ms) ✓ Should retrieve video info (863 ms) ✓ Should download video (2656 ms) * refactor: type safety checks removing @ts-ignore * fix: playlist tests pass (11/12) YouTube.js Tests Search ✓ Should search on YouTube (991 ms) ✕ Should search on YouTube Music (924 ms) ✓ Should retrieve YouTube search suggestions (606 ms) ✓ Should retrieve YouTube Music search suggestions (225 ms) Comments ✓ Should retrieve comments (393 ms) ✓ Should retrieve next batch of comments (284 ms) ✓ Should retrieve comment replies (252 ms) General ✓ Should retrieve playlist with YouTube (578 ms) ✓ Should retrieve home feed (1148 ms) ✓ Should retrieve trending content (541 ms) ✓ Should retrieve video info (799 ms) ✓ Should download video (1419 ms) * fix: all tests pass for node 🎉 YouTube.js Tests Search ✓ Should search on YouTube (1053 ms) ✓ Should search on YouTube Music (761 ms) ✓ Should retrieve YouTube search suggestions (453 ms) ✓ Should retrieve YouTube Music search suggestions (221 ms) Comments ✓ Should retrieve comments (627 ms) ✓ Should retrieve next batch of comments (412 ms) ✓ Should retrieve comment replies (268 ms) General ✓ Should retrieve playlist with YouTube (565 ms) ✓ Should retrieve home feed (775 ms) ✓ Should retrieve trending content (498 ms) ✓ Should retrieve video info (875 ms) ✓ Should download video (1364 ms) * build: working Deno bundle Still need to test whether this bundle works in the browser * docs: update deno example to download video * refactor: MusicResponsiveListItem to TS * docs: TSDoc for Parser helpers * docs: Parser documentation for TS * docs: add note about parseItem and parseArray * test: remove browser tests since they're identical * feat: browser support and proxy example * fix: PlaylistManager TS after merge * feat: in-browser video streaming * refactor: cleanup the Dash example * feat: allow custom fetch implementations * feat: fetch debugger * fix: OAuth login * refactor: remove file extensions from imports * refactor: build scripts * fix: CustomEvent on node * fix: LiveChat * fix: linting * fix: liniting in build-parser-json * chore: update test workflow * fix: NToken errors after lint fixes * fix: codacy complaints * docs: update to reflect changes Definitly needs more work but its a start * refactor: cleanup imports/exports * fix: browser example - Remove user-agent before making request. - Fix cache on browsers * fix: cache on node * fix: stupid mistake * refactor: Session#signIn to wait untill success This also splits the 'auth' event up into 3 distinct events: - 'auth' -> fired on success - 'auth-pending' -> fired when pending authentication - 'auth-error' -> fired when an error occurred * refactor: freeze Constants * refactor: cleanup HTTPClient Request * refactor: debugFetch readability * chore: lint * refactor: replace jsdoc with tsdoc eslint plugin remove @param annotations without descriptions * fix: bunch of liniting warnings * refactor: better inference on YTNode#is As suggested by @MasterOfBob777 * fix: linting warnings * revert: undici import * refactor: rename `list_type` to `item_type`
528 lines
19 KiB
TypeScript
528 lines
19 KiB
TypeScript
import { getStringBetweenStrings, InnertubeError, streamToIterable } from '../../utils/Utils';
|
|
import Parser, { ParsedResponse } from '../index';
|
|
import LiveChat from '../classes/LiveChat';
|
|
import Constants from '../../utils/Constants';
|
|
import Actions, { AxioslikeResponse } from '../../core/Actions';
|
|
import Player from '../../core/Player';
|
|
import TwoColumnWatchNextResults from '../classes/TwoColumnWatchNextResults';
|
|
import VideoPrimaryInfo from '../classes/VideoPrimaryInfo';
|
|
import MerchandiseShelf from '../classes/MerchandiseShelf';
|
|
import VideoSecondaryInfo from '../classes/VideoSecondaryInfo';
|
|
import RelatedChipCloud from '../classes/RelatedChipCloud';
|
|
import ChipCloud from '../classes/ChipCloud';
|
|
import ItemSection from '../classes/ItemSection';
|
|
import PlayerOverlay from '../classes/PlayerOverlay';
|
|
import ToggleButton from '../classes/ToggleButton';
|
|
import CommentsEntryPointHeader from '../classes/comments/CommentsEntryPointHeader';
|
|
import ContinuationItem from '../classes/ContinuationItem';
|
|
import LiveChatWrap from './LiveChat';
|
|
import CompactVideo from '../classes/CompactVideo';
|
|
import CompactMix from '../classes/CompactMix';
|
|
import PlayerMicroformat from '../classes/PlayerMicroformat';
|
|
import Format from '../classes/misc/Format';
|
|
import { create } from 'xmlbuilder2';
|
|
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
|
|
|
|
export type URLTransformer = (url: URL) => URL;
|
|
|
|
export interface FormatOptions {
|
|
/**
|
|
* Video quality; 360p, 720p, 1080p, etc... also accepts 'best' and 'bestefficiency'.
|
|
*/
|
|
quality?: string;
|
|
/**
|
|
* Download type, can be: video, audio or video+audio
|
|
*/
|
|
type?: 'video' | 'audio' | 'video+audio';
|
|
/**
|
|
* File format, use 'any' to download any format
|
|
*/
|
|
format?: string;
|
|
}
|
|
|
|
export interface DownloadOptions extends FormatOptions {
|
|
/**
|
|
* Download range, indicates which bytes should be downloaded.
|
|
*/
|
|
range?: {
|
|
start: number;
|
|
end: number;
|
|
}
|
|
}
|
|
|
|
class VideoInfo {
|
|
#page: [ParsedResponse, ParsedResponse?];
|
|
#actions;
|
|
#player;
|
|
#cpn;
|
|
#watch_next_continuation;
|
|
basic_info;
|
|
streaming_data;
|
|
playability_status;
|
|
annotations;
|
|
storyboards;
|
|
endscreen;
|
|
captions;
|
|
cards;
|
|
primary_info;
|
|
secondary_info;
|
|
merchandise;
|
|
related_chip_cloud;
|
|
watch_next_feed;
|
|
player_overlays;
|
|
comments_entry_point_header;
|
|
livechat;
|
|
/**
|
|
* @param data - API response.
|
|
* @param cpn - Client Playback Nonce
|
|
*/
|
|
constructor(data: [AxioslikeResponse, AxioslikeResponse?], actions: Actions, player: Player, cpn: string) {
|
|
this.#actions = actions;
|
|
this.#player = player;
|
|
this.#cpn = cpn;
|
|
const info = Parser.parseResponse(data[0].data);
|
|
const next = data?.[1]?.data ? Parser.parseResponse(data[1].data) : undefined;
|
|
this.#page = [ info, next ];
|
|
if (info.playability_status?.status === 'ERROR')
|
|
throw new InnertubeError('This video is unavailable', info.playability_status);
|
|
if (!info.microformat?.is(PlayerMicroformat))
|
|
throw new InnertubeError('Invalid microformat', info.microformat);
|
|
this.basic_info = { // This type is inferred so no need for an explicit type
|
|
...info.video_details,
|
|
...{
|
|
/**
|
|
* Microformat is a bit redundant, so only
|
|
* a few things there are interesting to us.
|
|
*/
|
|
embed: info.microformat?.embed,
|
|
channel: info.microformat?.channel,
|
|
is_unlisted: info.microformat?.is_unlisted,
|
|
is_family_safe: info.microformat?.is_family_safe,
|
|
has_ypc_metadata: info.microformat?.has_ypc_metadata
|
|
},
|
|
like_count: undefined as number | undefined,
|
|
is_liked: undefined as boolean | undefined,
|
|
is_disliked: undefined as boolean | undefined
|
|
};
|
|
this.streaming_data = info.streaming_data;
|
|
this.playability_status = info.playability_status;
|
|
this.annotations = info.annotations;
|
|
this.storyboards = info.storyboards;
|
|
this.endscreen = info.endscreen;
|
|
this.captions = info.captions;
|
|
this.cards = info.cards;
|
|
const two_col = next?.contents.item().as(TwoColumnWatchNextResults);
|
|
const results = two_col?.results;
|
|
const secondary_results = two_col?.secondary_results;
|
|
if (results && secondary_results) {
|
|
this.primary_info = results.get({ type: 'VideoPrimaryInfo' })?.as(VideoPrimaryInfo);
|
|
this.secondary_info = results.get({ type: 'VideoSecondaryInfo' })?.as(VideoSecondaryInfo);
|
|
this.merchandise = results.get({ type: 'MerchandiseShelf' })?.as(MerchandiseShelf);
|
|
this.related_chip_cloud = secondary_results?.get({ type: 'RelatedChipCloud' })?.as(RelatedChipCloud)?.content.item().as(ChipCloud);
|
|
this.watch_next_feed = secondary_results?.get({ type: 'ItemSection' })?.as(ItemSection)?.contents;
|
|
if (this.watch_next_feed && Array.isArray(this.watch_next_feed))
|
|
this.#watch_next_continuation = this.watch_next_feed?.pop()?.as(ContinuationItem);
|
|
this.player_overlays = next?.player_overlays.item().as(PlayerOverlay);
|
|
this.basic_info.like_count = this.primary_info?.menu?.top_level_buttons?.get({ icon_type: 'LIKE' })?.as(ToggleButton)?.like_count;
|
|
this.basic_info.is_liked = this.primary_info?.menu?.top_level_buttons?.get({ icon_type: 'LIKE' })?.as(ToggleButton)?.is_toggled;
|
|
this.basic_info.is_disliked = this.primary_info?.menu?.top_level_buttons?.get({ icon_type: 'DISLIKE' })?.as(ToggleButton)?.is_toggled;
|
|
const comments_entry_point = results.get({ target_id: 'comments-entry-point' })?.as(ItemSection);
|
|
this.comments_entry_point_header = comments_entry_point?.contents?.get({ type: 'CommentsEntryPointHeader' })?.as(CommentsEntryPointHeader);
|
|
this.livechat = next?.contents_memo.getType(LiveChat)?.[0];
|
|
}
|
|
}
|
|
/**
|
|
* Applies given filter to the watch next feed.
|
|
*/
|
|
async selectFilter(name: string) {
|
|
if (!this.filters.includes(name))
|
|
throw new InnertubeError('Invalid filter', { available_filters: this.filters });
|
|
const filter = this.related_chip_cloud?.chips?.get({ text: name });
|
|
if (filter?.is_selected)
|
|
return this;
|
|
const response = await filter?.endpoint?.call(this.#actions, undefined, true);
|
|
const data = response?.on_response_received_endpoints?.get({ target_id: 'watch-next-feed' });
|
|
this.watch_next_feed = data?.contents;
|
|
return this;
|
|
}
|
|
/**
|
|
* Retrieves watch next feed continuation.
|
|
*/
|
|
async getWatchNextContinuation() {
|
|
const response = await this.#watch_next_continuation?.endpoint.call(this.#actions, undefined, true);
|
|
const data = response?.on_response_received_endpoints?.get({ type: 'appendContinuationItemsAction' });
|
|
this.watch_next_feed = data?.contents;
|
|
this.#watch_next_continuation = this.watch_next_feed?.pop()?.as(ContinuationItem);
|
|
return this.watch_next_feed?.filterType(CompactVideo, CompactMix);
|
|
}
|
|
/**
|
|
* Likes the video.
|
|
*
|
|
*/
|
|
async like() {
|
|
const button = this.primary_info?.menu?.top_level_buttons?.get({ button_id: 'TOGGLE_BUTTON_ID_TYPE_LIKE' })?.as(ToggleButton);
|
|
if (!button)
|
|
throw new InnertubeError('Like button not found', { video_id: this.basic_info.id });
|
|
if (button.is_toggled)
|
|
throw new InnertubeError('This video is already liked', { video_id: this.basic_info.id });
|
|
const response = await button.endpoint.call(this.#actions);
|
|
return response;
|
|
}
|
|
/**
|
|
* Dislikes the video.
|
|
*
|
|
*/
|
|
async dislike() {
|
|
const button = this.primary_info?.menu?.top_level_buttons?.get({ button_id: 'TOGGLE_BUTTON_ID_TYPE_DISLIKE' })?.as(ToggleButton);
|
|
if (!button)
|
|
throw new InnertubeError('Dislike button not found', { video_id: this.basic_info.id });
|
|
if (button.is_toggled)
|
|
throw new InnertubeError('This video is already disliked', { video_id: this.basic_info.id });
|
|
const response = await button.endpoint.call(this.#actions);
|
|
return response;
|
|
}
|
|
/**
|
|
* Removes like/dislike.
|
|
*
|
|
*/
|
|
async removeLike() {
|
|
const button = this.primary_info?.menu?.top_level_buttons?.get({ is_toggled: true })?.as(ToggleButton);
|
|
if (!button)
|
|
throw new InnertubeError('This video is not liked/disliked', { video_id: this.basic_info.id });
|
|
const response = await button.toggled_endpoint.call(this.#actions);
|
|
return response;
|
|
}
|
|
/**
|
|
* Retrieves Live Chat if available.
|
|
*/
|
|
getLiveChat() {
|
|
if (!this.livechat)
|
|
throw new InnertubeError('Live Chat is not available', { video_id: this.basic_info.id });
|
|
return new LiveChatWrap(this);
|
|
}
|
|
get filters() {
|
|
return this.related_chip_cloud?.chips?.map((chip) => chip.text.toString()) || [];
|
|
}
|
|
get actions() {
|
|
return this.#actions;
|
|
}
|
|
get page() {
|
|
return this.#page;
|
|
}
|
|
/**
|
|
* Get songs used in the video.
|
|
*/
|
|
// TODO: this seems to be broken with the new UI, further investigation needed
|
|
get music_tracks() {
|
|
/*
|
|
Const metadata = this.secondary_info?.metadata;
|
|
if (!metadata)
|
|
return [];
|
|
const songs = [];
|
|
let current_song: Record<string, Text[]> = {};
|
|
let is_music_section = false;
|
|
for (let i = 0; i < metadata.rows.length; i++) {
|
|
const row = metadata.rows[i];
|
|
if (row.is(MetadataRowHeader)) {
|
|
if (row.content.toString().toLowerCase().startsWith('music')) {
|
|
is_music_section = true;
|
|
i++; // Skip the learn more link
|
|
}
|
|
continue;
|
|
}
|
|
if (!is_music_section)
|
|
continue;
|
|
if (row.is(MetadataRow))
|
|
current_song[row.title.toString().toLowerCase().replace(/ /g, '_')] = row.contents;
|
|
// TODO: this makes no sense, we continue above when
|
|
if (row.has_divider_line) {
|
|
songs.push(current_song);
|
|
current_song = {};
|
|
}
|
|
|
|
}
|
|
if (is_music_section)
|
|
songs.push(current_song);
|
|
return songs;
|
|
*/
|
|
return [];
|
|
}
|
|
chooseFormat(options: FormatOptions) {
|
|
if (!this.streaming_data)
|
|
throw new InnertubeError('Streaming data not available', { video_id: this.basic_info.id });
|
|
const formats = [
|
|
...(this.streaming_data.formats || []),
|
|
...(this.streaming_data.adaptive_formats || [])
|
|
];
|
|
const requires_audio = options.type ? options.type.includes('audio') : true;
|
|
const requires_video = options.type ? options.type.includes('video') : true;
|
|
const quality = options.quality || '360p';
|
|
let best_width = -1;
|
|
const is_best = [ 'best', 'bestefficiency' ].includes(quality);
|
|
const use_most_efficient = quality !== 'best';
|
|
let candidates = formats.filter((format) => {
|
|
if (requires_audio && !format.has_audio)
|
|
return false;
|
|
if (requires_video && !format.has_video)
|
|
return false;
|
|
if (options.format !== 'any' && !format.mime_type.includes(options.format || 'mp4'))
|
|
return false;
|
|
if (!is_best && format.quality_label !== quality)
|
|
return false;
|
|
if (best_width < format.width)
|
|
best_width = format.width;
|
|
return true;
|
|
});
|
|
if (candidates.length === 0) {
|
|
throw new InnertubeError('No matching formats found', {
|
|
options
|
|
});
|
|
}
|
|
if (is_best && requires_video)
|
|
candidates = candidates.filter((format) => format.width === best_width);
|
|
if (requires_audio && !requires_video) {
|
|
const audio_only = candidates.filter((format) => !format.has_video);
|
|
if (audio_only.length > 0) {
|
|
candidates = audio_only;
|
|
}
|
|
}
|
|
if (use_most_efficient) {
|
|
// Sort by bitrate (lower is better)
|
|
candidates.sort((a, b) => a.bitrate - b.bitrate);
|
|
} else {
|
|
// Sort by bitrate (higher is better)
|
|
candidates.sort((a, b) => b.bitrate - a.bitrate);
|
|
}
|
|
return candidates[0];
|
|
}
|
|
|
|
toDash(url_transformer: URLTransformer = (url) => url) {
|
|
if (!this.streaming_data)
|
|
throw new InnertubeError('Streaming data not available', { video_id: this.basic_info.id });
|
|
|
|
const { adaptive_formats } = this.streaming_data;
|
|
|
|
const length = adaptive_formats[0].approx_duration_ms / 1000;
|
|
|
|
const root = create({ version: '1.0', encoding: 'UTF-8' });
|
|
|
|
const period = root
|
|
.ele('MPD', {
|
|
xmlns: 'urn:mpeg:dash:schema:mpd:2011',
|
|
minBufferTime: 'PT1.500S',
|
|
profiles: 'urn:mpeg:dash:profile:isoff-main:2011',
|
|
type: 'static',
|
|
mediaPresentationDuration: `PT${length}S`
|
|
})
|
|
.att('xmlns', 'xsi', 'http://www.w3.org/2001/XMLSchema-instance')
|
|
.att('xsi', 'schemaLocation', 'urn:mpeg:dash:schema:mpd:2011 http://standards.iso.org/ittf/PubliclyAvailableStandards/MPEG-DASH_schema_files/DASH-MPD.xsd')
|
|
.ele('Period');
|
|
|
|
this.#generateAdaptationSet(period, adaptive_formats, url_transformer);
|
|
|
|
return root.end({ prettyPrint: true });
|
|
}
|
|
|
|
#generateAdaptationSet(period: XMLBuilder, formats: Format[], url_transformer: URLTransformer) {
|
|
const mimeTypes: string[] = [];
|
|
const mimeObjects: Format[][] = [ [] ];
|
|
|
|
formats.forEach((videoFormat) => {
|
|
if (!videoFormat.index_range || !videoFormat.init_range) {
|
|
return;
|
|
}
|
|
const mimeType = videoFormat.mime_type;
|
|
const mimeTypeIndex = mimeTypes.indexOf(mimeType);
|
|
if (mimeTypeIndex > -1) {
|
|
mimeObjects[mimeTypeIndex].push(videoFormat);
|
|
} else {
|
|
mimeTypes.push(mimeType);
|
|
mimeObjects.push([]);
|
|
mimeObjects[mimeTypes.length - 1].push(videoFormat);
|
|
}
|
|
});
|
|
|
|
for (let i = 0; i < mimeTypes.length; i++) {
|
|
const set = period
|
|
.ele('AdaptationSet', {
|
|
id: i,
|
|
mimeType: mimeTypes[i].split(';')[0],
|
|
startWithSAP: '1',
|
|
subsegmentAlignment: 'true',
|
|
scanType: !mimeTypes[i].includes('audio') ? 'progressive' : undefined
|
|
});
|
|
mimeObjects[i].forEach((format) => {
|
|
if (format.has_video) {
|
|
this.#generateRepresentationVideo(set, format, url_transformer);
|
|
} else {
|
|
this.#generateRepresentationAudio(set, format, url_transformer);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
#generateRepresentationVideo(set: XMLBuilder, format: Format, url_transformer: URLTransformer) {
|
|
const codecs = getStringBetweenStrings(format.mime_type, 'codecs="', '"');
|
|
if (!format.index_range || !format.init_range)
|
|
throw new InnertubeError('Index and init ranges not available', { format });
|
|
|
|
const url = new URL(format.decipher(this.#player));
|
|
url.searchParams.set('cpn', this.#cpn);
|
|
|
|
set
|
|
.ele('Representation', {
|
|
id: format.itag,
|
|
codecs,
|
|
bandwidth: format.bitrate,
|
|
width: format.width,
|
|
height: format.height,
|
|
maxPlayoutRate: '1',
|
|
frameRate: format.fps
|
|
})
|
|
.ele('BaseURL')
|
|
.txt(url_transformer(url).toString())
|
|
.up()
|
|
.ele('SegmentBase', {
|
|
indexRange: `${format.index_range.start}-${format.index_range.end}`
|
|
})
|
|
.ele('Initialization', {
|
|
range: `${format.init_range.start}-${format.init_range.end}`
|
|
});
|
|
}
|
|
|
|
#generateRepresentationAudio(set: XMLBuilder, format: Format, url_transformer: URLTransformer) {
|
|
const codecs = getStringBetweenStrings(format.mime_type, 'codecs="', '"');
|
|
if (!format.index_range || !format.init_range)
|
|
throw new InnertubeError('Index and init ranges not available', { format });
|
|
|
|
const url = new URL(format.decipher(this.#player));
|
|
url.searchParams.set('cpn', this.#cpn);
|
|
|
|
set
|
|
.ele('Representation', {
|
|
id: format.itag,
|
|
codecs,
|
|
bandwidth: format.bitrate
|
|
})
|
|
.ele('AudioChannelConfiguration', {
|
|
schemeIdUri: 'urn:mpeg:dash:23003:3:audio_channel_configuration:2011',
|
|
value: format.audio_channels || '2'
|
|
})
|
|
.up()
|
|
.ele('BaseURL')
|
|
.txt(url_transformer(url).toString())
|
|
.up()
|
|
.ele('SegmentBase', {
|
|
indexRange: `${format.index_range.start}-${format.index_range.end}`
|
|
})
|
|
.ele('Initialization', {
|
|
range: `${format.init_range.start}-${format.init_range.end}`
|
|
});
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param options - download options.
|
|
*/
|
|
async download(options: DownloadOptions = {}) {
|
|
if (this.playability_status?.status === 'UNPLAYABLE')
|
|
throw new InnertubeError('Video is unplayable', { video: this, error_type: 'UNPLAYABLE' });
|
|
if (this.playability_status?.status === 'LOGIN_REQUIRED')
|
|
throw new InnertubeError('Video is login required', { video: this, error_type: 'LOGIN_REQUIRED' });
|
|
if (!this.streaming_data)
|
|
throw new InnertubeError('Streaming data not available.', { video: this, error_type: 'NO_STREAMING_DATA' });
|
|
const opts: DownloadOptions = {
|
|
quality: '360p',
|
|
type: 'video+audio',
|
|
format: 'mp4',
|
|
range: undefined,
|
|
...options
|
|
};
|
|
|
|
const format = this.chooseFormat(opts);
|
|
const format_url = format.decipher(this.#player);
|
|
|
|
// If we're not downloading the video in chunks, we just use fetch once.
|
|
if (opts.type === 'video+audio' && !options.range) {
|
|
const response = await this.#actions.session.http.fetch_function(`${format_url}&cpn=${this.#cpn}`, {
|
|
method: 'GET',
|
|
headers: Constants.STREAM_HEADERS,
|
|
redirect: 'follow'
|
|
});
|
|
|
|
// Throw if the response is not 2xx
|
|
if (!response.ok)
|
|
throw new InnertubeError('The server responded with a non 2xx status code', { video: this, error_type: 'FETCH_FAILED', response });
|
|
|
|
const body = response.body;
|
|
|
|
if (!body)
|
|
throw new InnertubeError('Could not get ReadableStream from fetch Response.', { video: this, error_type: 'FETCH_FAILED', response });
|
|
|
|
return body;
|
|
}
|
|
// We need to download in chunks.
|
|
|
|
const chunk_size = 1048576 * 10; // 10MB
|
|
let chunk_start = (options.range ? options.range.start : 0);
|
|
let chunk_end = (options.range ? options.range.end : chunk_size);
|
|
let must_end = false;
|
|
|
|
let cancel: AbortController;
|
|
const readableStream = new ReadableStream<Uint8Array>({
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
start() {},
|
|
pull: async (controller) => {
|
|
if (must_end) {
|
|
controller.close();
|
|
return;
|
|
}
|
|
if ((chunk_end >= format.content_length) || options.range) {
|
|
must_end = true;
|
|
}
|
|
return new Promise(async (resolve, reject) => {
|
|
try {
|
|
cancel = new AbortController();
|
|
const response = await this.#actions.session.http.fetch_function(`${format_url}&cpn=${this.#cpn}&range=${chunk_start}-${chunk_end || ''}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
...Constants.STREAM_HEADERS
|
|
// XXX: use YouTube's range parameter instead of a Range header.
|
|
// Range: `bytes=${chunk_start}-${chunk_end}`
|
|
},
|
|
signal: cancel.signal
|
|
});
|
|
const body = response.body;
|
|
if (!body) {
|
|
throw new InnertubeError('Could not get ReadableStream from fetch Response.', { video: this, error_type: 'FETCH_FAILED', response });
|
|
}
|
|
|
|
for await (const chunk of streamToIterable(body)) {
|
|
controller.enqueue(chunk);
|
|
}
|
|
|
|
chunk_start = chunk_end + 1;
|
|
chunk_end += chunk_size;
|
|
resolve();
|
|
return;
|
|
} catch (e: any) {
|
|
reject(e);
|
|
}
|
|
});
|
|
},
|
|
async cancel(reason) {
|
|
cancel.abort(reason);
|
|
}
|
|
}, {
|
|
highWaterMark: 1, // TODO: better value?
|
|
size(chunk) {
|
|
return chunk.byteLength;
|
|
}
|
|
});
|
|
|
|
return readableStream;
|
|
|
|
}
|
|
}
|
|
export default VideoInfo;
|