• Stars
    star
    331
  • Rank 126,727 (Top 3 %)
  • Language
    C#
  • License
    The Unlicense
  • Created over 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Play YouTube videos in Unity

UnityYoutubePlayer

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.

Please note that WebGL is not supported because of CORS

Preview

Play youtube videos without this package

⚠️ https://unity-youtube-dl-server.herokuapp.com is a demo server that you were able to use in the past, but now is no longer available. This is due to Heroku getting rid of their free tier plan. See comment

That's right, you don't even need to install this package to play youtube videos in Unity.

Just change the url you want to play from

https://www.youtube.com/watch?v=1PuGuqpHQGo

To

https://unity-youtube-dl-server.herokuapp.com/watch?v=1PuGuqpHQGo&cli=yt-dlp

And use it directly in a VideoPlayer component. It will automatically redirect to the raw video format that can be played in Unity (in most cases)

But for a better reliability, you might want to host your own youtube-dl-server

If you are targetting desktop platforms, it is recommended that you use the package, since it will be faster, and does not need a server.

How To Install

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": "2.0.2"

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": "2.0.2",
    ...

Usage

  • A minimalistic example:
public class SimpleYoutubeVideo : MonoBehaviour
{
    void Start()
    {
        GetComponent<VideoPlayer>().PlayYoutubeVideoAsync("https://www.youtube.com/watch?v=1PuGuqpHQGo");
    }
}

Or,

  • Add a YoutubePlayer component on a GameObject with a VideoPlayer. Set the url in the inspector. The YoutubePlayer will follow the Play On Awake setting of the video player. You can also call YoutubePlayer.PlayVideoAsync.

  • In addition, you can call YoutubePlayer.DownloadVideoAsync to download the video to a file instead. See the DownloadYoutubeVideo as an example.

  • VideoPlayerProgress allows to display the progress of the video, as well as seeking.

  • See Assets\YoutubePlayer\Scenes for more examples.

Dependencies

UnityYoutubePlayer uses youtube-dl and yt-dlp 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 recommended to host this on your own.

Starting with 1.5.1, this package downloads and uses youtube-dl locally on Desktop platforms.

Starting with 1.7.0, this package downloads and uses yt-dlp locally on Desktop platforms, in addition to youtube-dl.

How do I host my own youtube-dl server?

  • Go to youtube-dl-server
  • Click the Deploy to Heroku purple button
  • Finish the setup on Heroku.
  • Make YoutubePlayer use your newly hosted instance: To make YoutubePlayer use your instance of the server, make sure to set the ServerUrl before 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&schema=url

or https://unity-youtube-dl-server.herokuapp.com/watch?v=1PuGuqpHQGo

Older version

For the version used with YoutubeExplode, see the legacy/youtube-explode branch. Please note that it has a different license than the current version.

More Repositories

1

playlet

The unofficial, privacy driven, YouTube client for Roku
BrighterScript
290
star
2

ImageEffectGraph

Image effects for post processing stack created with shader graph for Unity
C#
169
star
3

EmojiTexture

A Unity plugin to render Emojis ☺ ❀ πŸ† πŸ‘ to a texture
C#
60
star
4

youtube-dl-server

A youtube-dl web server, powered by youtube-dl
TypeScript
39
star
5

AwesomeToolbox

UI toolbox component for Unity
C#
36
star
6

TextureUpdater

Update textures at runtime in Unity
CMake
32
star
7

NativePluginBuilder

Native Plugin Builder - CMake based c/c++ build system for Unity.
C#
26
star
8

yt2alt

A cli tool to import your profile from YouTube to an alternative platform
JavaScript
25
star
9

ChromeAppBuilder

Chrome app builder is an editor extension for Unity to export games as google chrome apps.
C#
22
star
10

Xroutines

A mini framework to better use coroutines in Unity.
C#
21
star
11

FlatBuffersImporter

Google's FlatBuffers (.fbs files) importer for Unity.
C#
18
star
12

Unity-Simple-Fade-Camera

Fadein and out camera to color in using a simple shader
C#
15
star
13

sevakon

Game prototype
C#
13
star
14

UnityIniFiles

A very naive implementation to import .ini configuration files into Unity
C#
10
star
15

GifTexture

GifTexture for Unity - Play an animated gif file on a texture and render on any 3d object.
C#
7
star
16

MobileARTest

AR Foundation test
C#
5
star
17

UnityPackageTemplate

A template for creating Unity packages
C#
3
star
18

SpriteDepthShader

C#
2
star
19

express-route-autocorrect

Express middleware that autocorrects url routes to the closest match.
JavaScript
2
star
20

Tunisian-Red-Crescent---iOS

Tunisian-Red-Crescent---iOS
Swift
1
star
21

SkiaSharpUnity

SkiaSharp for Unity (UPM package)
1
star
22

Tunisian-Red-Crescent---Tizen

Tunisian-Red-Crescent---Tizen
JavaScript
1
star
23

Droidcon-Run

This is a simple runner game concept to show how easy it is to make a game and publish to Google Play Store. https://play.google.com/store/apps/details?id=com.TGD.DroidconRun
C#
1
star