Play and download youtube videos in Unity using youtube-dl and Unity's VideoPlayer, and uses youtube-dl-server to fetch metadata from youtube-dl.
This package uses the scoped registry feature to import dependent
packages. Please add the following sections to the package manifest file
(Packages/manifest.json).
To the scopedRegistries section:
{
"name": "iBicha",
"url": "https://registry.npmjs.com",
"scopes": [ "com.ibicha" ]
}
To the dependencies section:
"com.ibicha.youtube-player": "1.3.3"
After changes, the manifest file should look like below:
{
"scopedRegistries": [
{
"name": "iBicha",
"url": "https://registry.npmjs.com",
"scopes": [ "com.ibicha" ]
}
],
"dependencies": {
"com.ibicha.youtube-player": "1.3.3",
...
- A minimalistic example:
public class SimpleYoutubeVideo : MonoBehaviour
{
void Start()
{
GetComponent<VideoPlayer>().PlayYoutubeVideoAsync("https://www.youtube.com/watch?v=1PuGuqpHQGo");
}
}
Or,
-
Add a
YoutubePlayercomponent on aGameObjectwith aVideoPlayer. Set the url in the inspector. TheYoutubePlayerwill follow thePlay On Awakesetting of the video player. You can also callYoutubePlayer.PlayVideoAsync. -
In addition, you can call
YoutubePlayer.DownloadVideoAsyncto download the video to a file instead. See theDownloadYoutubeVideoas an example. -
VideoPlayerProgressallows to display the progress of the video, as well as seeking. -
See
Assets\YoutubePlayer\Scenesfor more examples.
UnityYoutubePlayer uses youtube-dl for parsing webpages and getting a raw video url that Unity's VideoPlayer can play. To allow maximum platform compatibilty (e.g. mobile, desktop) and to be able to update the library without rebuilding the game, we're using youtube-dl-server web API.
The package uses a free instance of the server hosted on heroku (shared between everyone). For better reliability and performance, it is recommanded to host this on your own.
- Go to youtube-dl-server
- Click the
Deploy to Herokupurple button - Finish the setup on Heroku.
- Make
YoutubePlayeruse your newly hosted instance: To makeYoutubePlayeruse your instance of the server, make sure to set theServerUrlbefore making calls to youtube APIs, e.g.
YoutubeDl.ServerUrl = "http://your-self-hosted-server.com";
For reference, the default instance is
YoutubeDl.ServerUrl = "https://unity-youtube-dl-server.herokuapp.com";
And you can test it in the browser: https://unity-youtube-dl-server.herokuapp.com/v1/video?url=https://www.youtube.com/watch?v=1PuGuqpHQGo
For the version used with YoutubeExplode, see the legacy/youtube-explode branch. Please note that it has a different license than the current version.
