ytdl
A Ruby wrapper for yt-dlp/youtube-dl with progress callbacks.
Installation
gem install ytdlgem 'ytdl'Make sure you have yt-dlp or youtube-dl installed and available in your PATH. If you have pip installed:
pip install yt-dlp
# or
pip install youtube-dlUsage
Minimal example:
YoutubeDL.download('https://www.youtube.com/watch?v=MmIWve5bUpU').callWith callbacks and options:
state = YoutubeDL.download('https://www.youtube.com/watch?v=MmIWve5bUpU', format: 'mp4')
.on_progress do |state:, line:|
puts "Progress: #{state.progress}%"
end
.on_error do |state:, line:|
puts "Error: #{state.error}"
end
.on_complete do |state:, line:|
puts "Complete: #{state.destination}"
end
.callYoutubeDL.download returns the state after yt-dlp has exited. If the download was successful, state.info_json is loaded into state.info and the info_json file deleted.
Configuration
YoutubeDL::Command.config.executable = 'youtube-dl' # if you're using youtube-dl instead of yt-dlp
YoutubeDL::Command.config.default_options = { some_option: true }Events
One event is emitted for each line printed by yt-dlp.
The full list of events types is:
-
unparsable: TheOutputParsercouldn't parse the line -
destination: Download destination announcement (stored instate.destination) -
info_json: Info JSON destination announcement (stored instate.info_json) -
progress: Download progress in percentage (stored instate.progress) -
error: An error message (stored instate.errorwithout theERROR:prefix) -
complete: The download is complete (Info JSON is parsed intostate.infoand deleted,state.destinationnow exists) -
unclear_exit_state:yt-dlpexited without error, but eitherdestinationorinfo_jsondoes not exist
Options
Options passed to YoutubeDL.download get transformed as follows:
-
some_option: truebecomes--some-option -
some_option: falsebecomes--no-some-option -
some_option: 'anything'becomes--some-option anything -
some_option: ['any', 'thing']becomes--some-option any --some-option thing -
some_option: nilcan be used to remove a default option