A rewritten YouTube source manager for Lavaplayer.
A rewritten YouTube source manager for Lavaplayer.
A rewritten YouTube source manager for Lavaplayer.
This source aims to provide robustness by leveraging multiple InnerTube clients for requests. Where one client fails, another will try to load the request. Which clients are used is entirely configurable.
common module and usage of.v2 module and usage of.plugin module and usage of.youtube-source, as well as their advantages/disadvantages.youtube-source.poToken with youtube-source.youtube-source.plugin only)youtube-source plugin module.This module provides the base source manager, which can be used with any
com.sedmelluq.discord.lavaplayer packages still on major version 1.
Using in Gradle:
repositories {
// replace with https://maven.lavalink.dev/snapshots if you want to use a snapshot version.
maven(url = "https://maven.lavalink.dev/releases")
}
dependencies {
// Replace VERSION with the current version as shown by the Releases tab or a long commit hash `-SNAPSHOT` for snapshots.
implementation("dev.lavalink.youtube:common:VERSION")
}Example usage:
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() });You may also extend the Client interface to support additional InnerTube clients. There are a few abstract classes to
make this easier, notably, MusicClient (for music.youtube.com InnerTube clients), NonMusicClient (for youtube.com
innertube clients) and StreamingNonMusicClient (for clients that can be used to stream videos).
Support for IP rotation has been included, and can be achieved using the following:
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();This modules expands on common by providing additional support for
Lavaplayer 2.x clients, such as Lavalink-Devs/Lavaplayer.
Such features currently include thumbnail support within AudioTrackInfo.
Additional clients are included that provide access to this additional information.
These clients are suffixed with Thumbnail, such as WebWithThumbnail, AndroidWithThumbnail etc.
Using in Gradle:
repositories {
// replace with https://maven.lavalink.dev/snapshots if you want to use a snapshot version.
maven(url = "https://maven.lavalink.dev/releases")
}
dependencies {
// Replace VERSION with the current version as shown by the Releases tab or a long commit hash `-SNAPSHOT` for snapshots.
implementation("dev.lavalink.youtube:v2:VERSION")
}Example usage:
// same as the 'common' module but there are additional clients that provide video thumbnails in the returned metadata.
YoutubeAudioSourceManager youtube = new YoutubeAudioSourceManager(/*allowSearch:*/ true, new Client[] { new MusicWithThumbnail(), new WebWithThumbnail(), new AndroidTestsuiteWithThumbnail() });This module serves as the plugin for use with Lavalink.
To use this plugin with Lavalink, you must declare the dependency.
Using with Lavalink v3:
lavalink:
plugins:
# Replace VERSION with the current version as shown by the Releases tab or a long commit hash for snapshots.
- dependency: "dev.lavalink.youtube:youtube-plugin:VERSION"
repository: "https://maven.lavalink.dev/releases" # use https://maven.lavalink.dev/snapshots if you want to use a snapshot version.Using with Lavalink v4:
lavalink:
plugins:
# Replace VERSION with the current version as shown by the Releases tab or a long commit hash for snapshots.
- dependency: "dev.lavalink.youtube:youtube-plugin:VERSION"
snapshot: false # Set to true if you want to use a snapshot version.Configuring the plugin:
[!IMPORTANT] You must make sure to disable the built-in YouTube source like so:
lavalink:
server:
sources:
youtube: false[!NOTE] Existing options, such as
ratelimitandyoutubePlaylistLoadLimitwill be picked up automatically by the plugin, so these don't need changing.
……Currently, the following clients are available for use:
| Identifier | Opus Formats | OAuth | Age-restriction Support | Playback Support | Metadata Support | Additional Notes |
|---|---|---|---|---|---|---|
MUSIC |
No | No | No | No | Search | YouTube music search support via ytmsearch: prefix |
WEB |
Yes | No | No | Yes + Livestream | Video, Search, Playlist, Mix | |
MWEB |
Yes | No | No | Yes + Livestream | Video, Search, Playlist, Mix | |
WEBEMBEDDED |
Yes | No | Limited | Yes + Livestream | Video | |
ANDROID |
Yes | No | No | Yes + Livestream | Video, Search, Playlist, Mix | Heavily restricted, frequently dysfunctional |
ANDROID_MUSIC |
Yes | No | No | Yes | Video, Search, Mix | |
ANDROID_VR |
Yes | No | No | Yes + Livestream | Video, Search, Playlist, Mix | |
IOS |
No | No | No | Yes + Livestream | Video, Search, Playlist, Mix | |
TV |
Yes | Yes | With OAuth | Yes + Livestream | None | Playback requires sign-in |
TVHTML5_SIMPLY |
Yes | No | No | Yes + Livestream | Video, Search, Mix |
[!NOTE] Clients that do not return Opus formats will require transcoding. Livestreams do not yield Opus formats so will always require transcoding.
You may notice that some requests are flagged by YouTube, causing an error message asking you to sign in to confirm you're not a bot.
With OAuth integration, you can request that youtube-source use your account credentials to appear as a normal user, with varying degrees
of efficacy. You do not need to use poToken with OAuth.
[!WARNING] Similar to the
poTokenmethod, this is NOT a silver bullet solution, and worst case could get your account terminated! For this reason, it is advised that you use burner accounts and NOT your primary! This method may also trigger ratelimit errors if used in a high traffic environment. USE WITH CAUTION!
[!NOTE] You may need to set your log level for
dev.lavalink.youtube.http.YoutubeOauth2HandlertoINFO, to see additional information within your terminal regarding completing the OAuth flow.
[!NOTE] If you do not have a refresh token, then do not supply one. The source will output your refresh token into your terminal upon successfully completing the OAuth flow at least once. If you do not see your token, you may need to configure your logging (see above note).
You can instruct youtube-source to use OAuth with the following:
……Another option to use oauth is by using oauth access tokens that are managed from your client. In this case your bot/client provides LavaLink with the token to use when playing a track. To do this simply add the oauth access token to a track's userData field in a json format when updating the player to play a track like:
{
"oauth-token": "access token to use"
}poTokenA poToken, also known as a "Proof of Origin Token" is a way to identify what requests originate from.
In YouTube's case, this is sent as a JavaScript challenge that browsers must evaluate, and send back the resolved
string. Typically, this challenge would remain unsolved for bots as more often than not, they don't simulate an entire
browser environment, instead only evaluating the minimum amount of JS required to do its job. Therefore, it's a reasonable
assumption that if the challenge is not fulfilled, the request origin is a bot.
To obtain a poToken, you can use https://github.com/iv-org/youtube-trusted-session-generator, by running the Python script
or the docker image. Both methods will print a poToken after a successful run, which you can supply to youtube-source
to try and work around having automated requests blocked.
[!NOTE] A
poTokenis not a silver bullet, and currently it only applies to requests made via theWEB&WEBEMBEDDEDclient. You do not need to specify apoTokenif using OAuth, and vice versa.
Specifying the token is as simple as doing:
// 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"It becomes harder and harder to keep up with YouTube's cipher changes, as they become more frequent and complex. To help with this, you can use a remote cipher server to handle signature deciphering for you. You can use yt-cipher, which is a simple Deno server that exposes a REST API for deciphering signatures. Check out the repository for more information on how to set it up.
If you want to implement your own, you can follow the yt-cipher API specification.
YoutubeSourceOptions options = new YoutubeSourceOptions()
// The base URL of your remote cipher server & the password to authenticate with your remote cipher server, along with an identifier for metrics.
.setRemoteCipher("http://localhost:8001", "your_secret_password", "user agent");
YoutubeAudioSourceManager sourceManager = new YoutubeAudioSourcNo open issues yet, or sync has not completed.