youtube-source

repository·main·Indexed 18 days ago

https://github.com/lavalink-devs/youtube-source

A robust YouTube source manager for Lavaplayer and Lavalink that utilizes multiple InnerTube clients for reliable track loading and metadata retrieval. It provides a common module for Lavaplayer v1, a v2 module for Lavaplayer 2.x with thumbnail support, and a dedicated Lavalink plugin. Key features include IP rotation, OAuth2 authentication, poToken support, remote cipher server integration, and a customizable Client interface for implementing custom InnerTube clients.

Tokens
5.9K
Snippets
17
Records
18
Agent score
14%

What's inside youtube-source

  1. Use a `poToken` (Proof of Origin Token)

    main

    A poToken is a JavaScript challenge response used to identify requests as originating from a browser rather than a bot. This is specifically useful for the WEB and WEBEMBEDDED clients.

    You can obtain a poToken using tools like youtube-trusted-session-generator.

    Note: You do not need to specify a poToken if you are already using OAuth.

    ### Lavaplayer
    ```java
    // Web is dev.lavalink.youtube.clients.Web
    Web.setPoTokenAndVisitorData("your po_token", "your visitor_data");
    plugins:
      youtube:
        pot:
          token: "paste your po_token here"
          visitorData: "paste your visitor_data here"
  2. Install the youtube-source v2 module

    main

    Use the v2 module if you are using Lavaplayer 2.x clients (such as Lavalink-Devs/Lavaplayer). This module provides additional support like thumbnail support within AudioTrackInfo and specialized clients suffixed with Thumbnail (e.g., WebWithThumbnail).

    repositories {
      maven(url = "https://maven.lavalink.dev/releases")
    }
    
    dependencies {
      implementation("dev.lavalink.youtube:v2:VERSION")
    }
  3. Install the youtube-source Lavalink plugin

    main

    To use youtube-source as a Lavalink plugin, add the dependency to your application.yml.

    For Lavalink v3:

    lavalink:
      plugins:
        - dependency: "dev.lavalink.youtube:youtube-plugin:VERSION"
          repository: "https://maven.lavalink.dev/releases"

    For Lavalink v4:

    lavalink:
      plugins:
        - dependency: "dev.lavalink.youtube:youtube-plugin:VERSION"
          snapshot: false
  4. Migrate from Lavaplayer's built-in YouTube source

    main

    The built-in YoutubeAudioSourceManager in Lavaplayer is deprecated. Use the youtube-source package as a direct replacement.

    Important: This source has a different class structure. Custom initializations (like an overridden YoutubeTrackDetailsLoader) are not compatible with this manager.

    To migrate, register the new manager manually instead of using AudioSourceManagers.registerRemoteSources if your version doesn't support exclusion.

    AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
    YoutubeAudioSourceManager ytSourceManager = new dev.lavalink.youtube.YoutubeAudioSourceManager();
    playerManager.registerSourceManager(ytSourceManager);

    If your Lavaplayer version supports excludeSources:

    AudioSourceManagers.registerRemoteSources(playerManager,
                                              com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager.class);
  5. Configure IP rotation for youtube-source

    main

    To support IP rotation, use YoutubeIpRotatorSetup with an AbstractRoutePlanner. Ensure you call .withMainDelegateFilter(youtube.getContextFilter()) to correctly apply the rotation to your manager's context.

    AbstractRoutePlanner routePlanner = new ...
    YoutubeIpRotatorSetup rotator = new YoutubeIpRotatorSetup(routePlanner);
    
    // 'youtube' is the variable holding your YoutubeAudioSourceManager instance.
    rotator.forConfiguration(youtube.getHttpInterfaceManager(), false)
        .withMainDelegateFilter(youtube.getContextFilter()) // IMPORTANT
        .setup();
  6. Configure a remote cipher server

    main

    To handle YouTube's frequent signature deciphering changes, you can use a remote cipher server (like yt-cipher) to process signatures via a REST API.

    ### Lavaplayer
    ```java
    YoutubeSourceOptions options = new YoutubeSourceOptions()
         .setRemoteCipher("http://localhost:8001", "your_secret_password", "user agent");
    YoutubeAudioSourceManager sourceManager = new YoutubeAudioSourceManager(options, ...);
    plugins:
      youtube:
        remoteCipher:
          url: "http://localhost:8001"
          password: "your_secret_password"
          userAgent: "your_service_name" # Optional
  7. Configure OAuth2 authentication

    main

    OAuth2 allows youtube-source to use your account credentials to appear as a normal user, helping to bypass bot detection.

    Warning: Use burner accounts only. Using your primary account carries a risk of termination. This method may also trigger rate limits in high-traffic environments.

    To use OAuth, you can either trigger an interactive flow to obtain a refresh token or provide an existing refresh token. If you are completing the flow for the first time, the source will output your refresh token to the terminal once successful. You may need to set the log level for dev.lavalink.youtube.http.YoutubeOauth2Handler to INFO to see this token.

    Note: You do not need to use poToken if you are using OAuth.

    ### Lavaplayer
    ```java
    YoutubeAudioSourceManager source = new YoutubeAudioSourceManager();
    // Triggers interactive OAuth flow
    source.useOauth2(null, false);
    
    // Uses existing refresh token and skips flow
    source.useOauth2("your refresh token", true);
    plugins:
      youtube:
        enabled: true
        oauth:
          enabled: true
          # refreshToken: "paste your refresh token here if applicable"
          # skipInitialization: true
  8. Install the youtube-source common module

    main

    Use the common module if you are using com.sedmelluq.discord.lavaplayer packages on major version 1. Add the Maven repository and the dev.lavalink.youtube:common:VERSION dependency to your Gradle configuration.

    repositories {
      maven(url = "https://maven.lavalink.dev/releases")
    }
    
    dependencies {
      implementation("dev.lavalink.youtube:common:VERSION")
    }
  9. Use PlayabilityStatus to handle video availability

    main

    The getPlayabilityStatus method in the Client interface parses a JsonBrowser object to determine if a video can be played. It maps YouTube's internal status to the PlayabilityStatus enum.

    Supported statuses:

    • OK: The video is playable.
    • NON_EMBEDDABLE: The video is playable but playback on other websites has been disabled by the owner.
    • REQUIRES_LOGIN: The video requires user authentication (e.g., private videos or age-restricted content).
    • PREMIERE_TRAILER: The video is a live premiere trailer.

    Note: If throwOnNotOk is true, the method may throw a FriendlyException for various error states (like UNPLAYABLE or CONTENT_CHECK_REQUIRED) instead of returning a status.

    enum PlayabilityStatus {
            OK,
            NON_EMBEDDABLE,
            REQUIRES_LOGIN,
            PREMIERE_TRAILER
        }
  10. Configure the youtube-source Lavalink plugin

    main

    When using the plugin, you must disable the built-in Lavalink YouTube source first.

    Basic configuration includes:

    • enabled: Enables/disables the source.
    • allowSearch: Enables/disables ytsearch: and ytmsearch: prefixes.
    • allowDirectVideoIds: If false, only complete URLs will be loaded.
    • allowDirectPlaylistIds: If false, only complete URLs will be loaded.
    • clients: An ordered list of clients used for track loading. The first successful client in the list will be used.
    lavalink:
      server:
        sources:
          youtube: false
    
    plugins:
      youtube:
        enabled: true
        allowSearch: true
        allowDirectVideoIds: true
        allowDirectPlaylistIds: true
        clients:
          - MUSIC
          - ANDROID_VR
          - WEB
          - WEBEMBEDDED 
  11. Configure advanced client options in Lavalink plugin

    main

    You can fine-tune specific client behaviors using clientOptions. This allows you to disable specific capabilities like playback, video loading, playlist loading, or searching for a particular client.

    Warning: Misconfiguration can prevent the source from working properly. Ensure the client names used in clientOptions match the identifiers in the Available Clients section.

    plugins:
      youtube:
        clientOptions:
          WEB:
            playback: false
            videoLoading: false
          WEBEMBEDDED:
            playlistLoading: false
            searching: false
  12. Use the youtube-source common module in Java

    main

    Instantiate YoutubeAudioSourceManager to manage YouTube tracks. You can optionally enable searching and specify a list of Client objects to use for track loading. You can also extend the Client interface using abstract classes like MusicClient, NonMusicClient, or StreamingNonMusicClient to support custom InnerTube clients.

    YoutubeAudioSourceManager youtube = new YoutubeAudioSourceManager();
    // Optionally, you may instantiate the source with a custom options, such as toggling use of searching, and clients.
    YoutubeAudioSourceManager youtube = new YoutubeAudioSourceManager(/*allowSearch:*/ true, new Client[] { new Music(), new Web(), new AndroidTestsuite() });