JanWerder / godot-yt-dlp

An implementation of yt-dlp for Godot 4.x

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo_light logo_dark

An implementation of yt-dlp for Godot 4.x that works on Linux, OSX and Windows.
This project provides a simple API for downloading videos from YouTube (and other websites).

ℹ️ Use v2.0.3 for Godot 3.x

Features

  • Automatic yt-dlp and ffmpeg setup (the latter only on Windows).
  • Downloading single videos.
  • Converting videos to audio.
  • Tracking download progress. (yet to be implemented)
  • Downloading playlists of videos. (yet to be implemented)
  • Searching YouTube videos. (yet to be implemented)

Installation

Clone the repository or download a release, place it into the addons/ folder in your project and enable the plugin. See the Godot Docs for a detailed guide.

If you're using Linux or exporting to Linux make sure that ffmpeg is installed on the system
Same goes for OSX (undocumented)

How to use

Setup

After enabling the plugin a YtDlp autoload singleton will be registered. In order to use it you must first call the setup() method that will download all relevant depedencies (up-to-date yt-dlp and ffmpeg if using Windows).

You can check if YtDlp is ready by using the is_setup() method. You can also connect or await the setup_completed signal to be notified when YtDlp is ready to download.

if not YtDlp.is_setup():
    YtDlp.setup()
    await YtDlp.setup_completed

Usage

To download a video use the download(url) method, it will create a Download object that can be complemented using method chaining and started with start() method.

You can connect or await the download_completed signal, to be notified of when the download is completed.

You can check the status of a Download using the get_status() method.

# Downloads a video as audio and stores it to "user://audio/ok_computer.mp3"
var download := YtDlp.download("https://youtu.be/Ya5Fv6VTLYM") \
        .set_destination("user://audio/") \
        .set_file_name("ok_computer") \
        .convert_to_audio(YtDlp.Audio.MP3) \
        .start()

assert(download.get_status() == YtDlp.Download.Status.DOWNLOADING)

await download.download_completed
print("Download completed !")

Examples

Downloading a video and playing it in using a VideoPlayer

Possible, but very ineffecient!

Currently the addon is downloading the video as a webm file and then converting it to an ogv file. For the example video this means a 4x file size increase and a conversion of several minutes.

Currently this is caused by a lack of video support in Godot. Please see https://docs.godotengine.org/en/stable/tutorials/animation/playing_videos.html#supported-playback-formats for more information. Godot is working on a better solution here: godotengine/godot-proposals#3286

var download := YtDlp.download("https://youtu.be/Ya5Fv6VTLYM") \
    .set_destination("user://video/") \
    .set_file_name("ok_computer") \
    .convert_to_video(YtDlp.Video.OGV) \
    .start()

await download.download_completed

var stream = VideoStreamTheora.new()
stream.file = "user://video/ok_computer.ogv"

%VideoStreamPlayer.stream = stream
%VideoStreamPlayer.play()

Downloading a video as audio and playing it using an AudioStreamPlayer

if not YtDlp.is_setup():
    YtDlp.setup()
    await YtDlp.setup_completed

var download := YtDlp.download("https://youtu.be/Ya5Fv6VTLYM") \
        .set_destination("user://audio/") \
        .set_file_name("ok_computer") \
        .convert_to_audio(YtDlp.Audio.MP3) \
        .start()

await download.download_completed

var stream = AudioStreamMP3.new()
var audio_file = FileAccess.open("user://audio/ok_computer.mp3", FileAccess.READ)

stream.data = audio_file.get_buffer(audio_file.get_length())
audio_file.close()

$AudioStreamPlayer.stream = stream
$AudioStreamPlayer.play()

Reference

YtDlp

Signals

setup_completed

Fired when the setup is completed and YtDlp is ready to use

Enums

Video
  • WEBM (default format)
  • MP4
Audio
  • MP3 (default format)
  • FLAC
  • AAC
  • VORBIS
  • OPUS
  • M4A
  • WAV

Methods

setup() -> void

Sets up the yt-dlp dependencies.

download(url: String) -> Download

Creates a new Download with the target url.

is_setup() -> bool

Returns true if YtDlp is ready to use, else returns false

Download

Signals

download_completed

Fired when the download is completed.

Enums

Status
  • READY
  • DOWNLOADING
  • COMPLETED

Methods

set_destination(destination: String) -> Download

Sets the destination directory of a download.

set_file_name(file_name: String) -> Download

Sets the file name of the downloaded file (without file extension).

set_video_format(format: YtDlp.Video) -> Download

Sets the format of the downloaded video.

convert_to_audio(format: YtDlp.Audio) -> Download

Marks the download to be converted to audio.

start() -> Download

Starts the download.

get_status() -> Status:

Returns the status of the download

Social

About

An implementation of yt-dlp for Godot 4.x

License:MIT License


Languages

Language:GDScript 100.0%