@chooks22/youtubei.js
TypeScript icon, indicating that this package has built-in type declarations

3.1.0 • Public • Published

YouTube.js

A full-featured wrapper around the InnerTube API, which is what YouTube itself uses.

Report Bug · Request Feature

Tests Latest version Codefactor Downloads Say thanks
Donate

Special thanks to:

SerpApi
Scrape Google and other search engines from a fast, easy and complete API.

Table of Contents
  1. About
  2. Getting Started
  3. Usage
  4. Implementing custom functionality
  5. Contributing
  6. Contributors
  7. Contact
  8. Disclaimer
  9. License

About

InnerTube is an API used across all YouTube clients, it was created to simplify1 the internal structure of the platform in a way that updates, tweaks, and experiments can be easily made. This library handles all the low-level communication with InnerTube, providing a simple, fast, and efficient way to interact with YouTube programmatically.

If you have any questions or need help, feel free to contact us on our chat server here.

Getting Started

Prerequisites

YouTube.js runs on Node.js, Deno, and modern browsers.

It requires a runtime with the following features:

  • fetch
    • On Node we use undici's fetch implementation which requires Node.js 16.8+. You may provide your fetch implementation if you need to use an older version. See providing your own fetch implementation for more information.
    • The Response object returned by fetch must thus be spec compliant and return a ReadableStream object if you want to use the VideoInfo#download method. (Implementations like node-fetch returns a non-standard Readable object.)
  • EventTarget and CustomEvent are required.

Installation

# NPM
npm install @chooks22/youtubei.js@latest

# Yarn
yarn add @chooks22/youtubei.js@latest

Usage

Create an InnerTube instance:

// const { Innertube } = require('@chooks22/youtubei.js');
import { Innertube } from '@chooks22/youtubei.js';
const youtube = await Innertube.create();

Deno Usage

Create an InnerTube instance:

import { Innertube } from 'https://github.com/Choooks22/YouTube.js/raw/better-youtube.js/mod.ts'
const youtube = await Innertube.create();

Browser Usage

To use YouTube.js in the browser you must proxy requests through your own server. You can see our simple reference implementation in Deno in examples/browser/proxy/deno.ts.

You may provide your own fetch implementation to be used by YouTube.js. Which we will use here to modify and send the requests through our proxy. See examples/browser/web for a simple example using Vite.

// Pre-bundled version for the web
import { Innertube } from '@chooks22/youtubei.js/browser.js';
await Innertube.create({
  fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
    // Modify the request
    // and send it to the proxy

    // fetch the URL
    return fetch(request, init);
  },
});

Streaming

YouTube.js supports streaming of videos in the browser by converting YouTube's streaming data into an MPEG-DASH manifest.

The example below uses dash.js to play the video.

import { Innertube } from '@chooks22/youtubei.js';
import dashjs from 'dashjs';

const youtube = await Innertube.create({/* setup - see above */});

// get the video info
const videoInfo = await youtube.getInfo('videoId');

// now convert to a dash manifest
// again - to be able to stream the video in the browser - we must proxy the requests through our own server
// to do this, we provide a method to transform the URLs before writing them to the manifest
const manifest = videoInfo.toDash((url) => {
  // modify the url
  // and return it
  return url;
});

const uri = 'data:application/dash+xml;charset=utf-8;base64,' + btoa(manifest);

const videoElement = document.getElementById('video_player');

const player = dashjs.MediaPlayer().create();
player.initialize(videoElement, uri, true);

Our browser example in examples/browser/web provides a fully working example.

Providing your own fetch implementation

You may provide your own fetch implementation to be used by YouTube.js. This can be useful in some cases to modify the requests before they are sent and transform the responses before they are returned (eg. for proxies).

// provide a fetch implementation
const yt = await Innertube.create({
  fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
    // make the request with your own fetch implementation
    // and return the response
    return new Response(
      /* ... */
    );
  },
});

Caching

To improve performance, you may wish to cache the transformed player instance which we use to decode the streaming urls.

Our cache uses the node:fs module in Node-like environments, Deno.writeFile in Deno, and indexedDB in browsers.

import { Innertube, UniversalCache } from '@chooks22/youtubei.js';
// By default, cache stores files in the OS temp directory (or indexedDB in browsers).
const yt = await Innertube.create({
  cache: new UniversalCache(),
});

// You may wish to make the cache persistent (on Node and Deno)
const yt = await Innertube.create({
  cache: new UniversalCache(
    // Enables persistent caching
    true,
    // Path to the cache directory will create the directory if it doesn't exist
    './.cache',
  ),
});

API

getInfo(video_id, client?)

Retrieves video info, including playback data and even layout elements such as menus, buttons, etc — all nicely parsed.

Returns: Promise.<VideoInfo>

Param Type Description
video_id string The id of the video
client? InnerTubeClient WEB, ANDROID, YTMUSIC, YTMUSIC_ANDROID or TV_EMBEDDED
Methods & Getters

  • <info>#like()

    • Likes the video.
  • <info>#dislike()

    • Dislikes the video.
  • <info>#removeLike()

    • Removes like/dislike.
  • <info>#getLiveChat()

    • Returns a LiveChat instance.
  • <info>#chooseFormat(options)

    • Used to choose streaming data formats.
  • <info>#toDash(url_transformer)

    • Converts streaming data to an MPEG-DASH manifest.
  • <info>#download(options)

  • <info>#filters

    • Returns filters that can be applied to the watch next feed.
  • <info>#selectFilter(name)

    • Applies the given filter to the watch next feed and returns a new instance of VideoInfo.
  • <info>#getWatchNextContinuation()

    • Retrieves the next batch of items for the watch next feed.
  • <info>#addToWatchHistory()

    • Adds the video to the watch history.
  • <info>#page

    • Returns original InnerTube response (sanitized).

getBasicInfo(video_id, client?)

Suitable for cases where you only need basic video metadata. Also, it is faster than getInfo().

Returns: Promise.<VideoInfo>

Param Type Description
video_id string The id of the video
client? InnerTubeClient WEB, ANDROID, YTMUSIC_ANDROID, YTMUSIC, TV_EMBEDDED

search(query, filters?)

Searches the given query on YouTube.

Returns: Promise.<Search>

Param Type Description
query string The search query
filters? SearchFilters Search filters
Methods & Getters

  • <search>#selectRefinementCard(SearchRefinementCard | string)

    • Applies given refinement card and returns a new Search instance.
  • <search>#refinement_card_queries

    • Returns available refinement cards, this is a simplified version of the refinement_cards object.
  • <search>#getContinuation()

    • Retrieves next batch of results.

getSearchSuggestions(query)

Retrieves search suggestions for given query.

Returns: Promise.<string[]>

Param Type Description
query string The search query

getComments(video_id, sort_by?)

Retrieves comments for given video.

Returns: Promise.<Comments>

Param Type Description
video_id string The video id
sort_by string Can be: TOP_COMMENTS or NEWEST_FIRST

See ./examples/comments for examples.

getHomeFeed()

Retrieves YouTube's home feed.

Returns: Promise.<FilterableFeed>

getLibrary()

Retrieves the account's library.

Returns: Promise.<Library>

Methods & Getters

  • <library>#history
  • <library>#watch_later
  • <library>#liked_videos
  • <library>#playlists
  • <library>#clips
  • <library>#page
    • Returns original InnerTube response (sanitized).

getHistory()

Retrieves watch history.

Returns: Promise.<History>

Methods & Getters

  • <history>#getContinuation()
    • Retrieves next batch of contents.

getTrending()

Retrieves trending content.

Returns: Promise.<TabbedFeed>

getSubscriptionsFeed()

Retrieves subscriptions feed.

Returns: Promise.<Feed>

getChannel(id)

Retrieves contents for a given channel.

Returns: Promise.<Channel>

Param Type Description
id string Channel id
Methods & Getters

  • <channel>#getVideos()
  • <channel>#getPlaylists()
  • <channel>#getHome()
  • <channel>#getCommunity()
  • <channel>#getChannels()
  • <channel>#getAbout()

See ./examples/channel for examples.

getNotifications()

Retrieves notifications.

Returns: Promise.<NotificationsMenu>

Methods & Getter

  • <notifications>#getContinuation()
    • Retrieves next batch of notifications.

getUnseenNotificationsCount()

Retrieves unseen notifications count.

Returns: Promise.<number>****

getPlaylist(id)

Retrieves playlist contents.

Returns: Promise.<Playlist>

Param Type Description
id string Playlist id
Methods & Getter

  • <playlist>#items
    • Returns the items of the playlist.

getStreamingData(video_id, options)

Returns deciphered streaming data.

Note: It is recommended to retrieve streaming data from a VideoInfo/TrackInfo object instead if you want to select formats manually, example:

const info = await yt.getBasicInfo('somevideoid');
const url = info.streaming_data?.formats[0].decipher(yt.session.player);
console.info('Playback url:', url);

Returns: Promise.<object>

Param Type Description
video_id string Video id
options FormatOptions Format options

download(video_id, options?)

Downloads a given video.

Returns: Promise.<ReadableStream<Uint8Array>>

Param Type Description
video_id string Video id
options DownloadOptions Download options

See ./examples/download for examples.

call(endpoint, args?)

Utility to call navigation endpoints.

Returns: Promise.<ActionsResponse | ParsedResponse>

Param Type Description
endpoint NavigationEndpoint The target endpoint
args? object Additional payload arguments

Implementing custom functionality

Something cool about YouTube.js is that it is completely modular and easy to tinker with. Almost all methods, classes, and utilities used internally are exposed and can be used to implement your own extensions without having to modify the library's source code.

For example, you may want to call an endpoint directly, that can be achieved with the Actions class:

// ...

const payload = {
  videoId: 'jLTOuvBTLxA',
  client: 'YTMUSIC', // InnerTube client, can be ANDROID, YTMUSIC, YTMUSIC_ANDROID, WEB or TV_EMBEDDED
  parse: true, // tells YouTube.js to parse the response, this is not sent to InnerTube.
};

const response = await yt.actions.execute('/player', payload);

console.info(response);

Or maybe there's an interesting NavigationEndpoint in a parsed response and we want to call it to see what happens:

// ...
const artist = await yt.music.getArtist('UC52ZqHVQz5OoGhvbWiRal6g');
const albums = artist.sections[1].as(MusicCarouselShelf);

// Say we have a button and want to “click” it
const button = albums.as(MusicCarouselShelf).header?.more_content;

if (button) {
  // To do that, we can call its navigation endpoint:
  const page = await button.endpoint.call(yt.actions, 'YTMUSIC', true);
  console.info(page);
}

Parser

If you're working on an extension for the library or just want to have nicely typed and sanitized InnerTube responses for a project then have a look at our powerful parser!

Example:

// See ./examples/parser

import { Parser } from '@chooks22/youtubei.js';
import {
  SectionList,
  SingleColumnBrowseResults,
  MusicVisualHeader,
  MusicImmersiveHeader,
  MusicCarouselShelf,
  MusicDescriptionShelf,
  MusicShelf,
} from '@chooks22/youtubei.js/classes'

import { readFileSync } from 'fs';

// Artist page response from YouTube Music
const data = readFileSync('./artist.json').toString();

const page = Parser.parseResponse(JSON.parse(data));

const header = page.header.item().as(MusicImmersiveHeader, MusicVisualHeader);

console.info('Header:', header);

// The parser encapsulates all arrays in a proxy object.
// A proxy intercepts access to the actual data, allowing
// the parser to add type safety and many utility methods
// that make working with InnerTube much easier.
const tab = page.contents.item().as(SingleColumnBrowseResults).tabs.get({
  selected: false,
});

if (!tab) {
  throw new Error('Target tab not found');
}

if (!tab.content) {
  throw new Error('Target tab appears to be empty');
}

const sections = tab.content?.as(SectionList).contents.array().as(
  MusicCarouselShelf,
  MusicDescriptionShelf,
  MusicShelf,
);

console.info('Sections:', sections);

Detailed documentation can be found here.

Contributing

Contributions, issues, and feature requests are welcome. Feel free to check the issues page and our guidelines if you want to contribute.

Contributors

Contact

LuanRT - @lrt_nooneknows - luan.lrt4@gmail.com

Project Link: https://github.com/LuanRT/YouTube.js

Disclaimer

This project is not affiliated with, endorsed, or sponsored by YouTube or any of its affiliates or subsidiaries. All trademarks, logos, and brand names are the property of their respective owners and are used only to directly describe the services being provided, as such, any usage of trademarks to refer to such services is considered nominative use.

Should you have any questions or concerns please contact me directly via email.

License

Distributed under the MIT License.

(back to top)

  1. https://gizmodo.com/how-project-innertube-helped-pull-youtube-out-of-the-gu-1704946491

Package Sidebar

Install

npm i @chooks22/youtubei.js

Weekly Downloads

21

Version

3.1.0

License

MIT

Unpacked Size

3.3 MB

Total Files

1171

Last publish

Collaborators

  • chooks22