A simple Ruby wrapper for yt-dlp that provides an easy-to-use interface for downloading YouTube videos and extracting video information.
Before using this script, you need to have the following installed on your system:
- Windows: Download from python.org
- macOS:
brew install python3or download from python.org - Linux:
sudo apt install python3(Ubuntu/Debian) orsudo yum install python3(CentOS/RHEL)
Install yt-dlp using pip:
pip3 install yt-dlp- Windows: Download from rubyinstaller.org
- macOS:
brew install rubyor use the built-in Ruby - Linux:
sudo apt install ruby(Ubuntu/Debian) orsudo yum install ruby(CentOS/RHEL)
- Clone or download the script to your desired directory
- Ensure the script has executable permissions (Linux/macOS):
chmod +x ytr_wrapper.rb
require_relative 'ytr_wrapper'
# Simple download (best quality)
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID")
# Download to specific path
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID", "/path/to/output/video.%(ext)s")
# Download audio only (MP3)
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID", audio_only: true)
# Download with specific format
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID", format: "720p")# Using options hash (recommended for complex configurations)
options = {
output: "/downloads/my_video.%(ext)s",
format: "best[height<=1080]",
audio_only: false
}
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID", options)
# Audio-only download with custom path
audio_options = {
output: "/music/song.%(ext)s",
audio_only: true
}
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID", audio_options)# Get detailed information about a video
info = YtRWrapper.get_info("https://www.youtube.com/watch?v=VIDEO_ID")
puts info["title"] # Video title
puts info["duration"] # Duration in seconds
puts info["view_count"] # Number of views
puts info["uploader"] # Channel name
puts info["description"] # Video descriptionDownloads a video from the provided URL.
Parameters:
url(String): The YouTube video URLoptions_or_path(Hash or String): Either an options hash or output path stringformat(String): Video format/quality (default: "best")audio_only(Boolean): Whether to download audio only (default: false)
Options Hash Keys:
:output(String): Output file path template:format(String): Video format/quality:audio_only(Boolean): Download audio only
Returns: String - Path to the downloaded file
Example:
# Method 1: Using separate parameters
file_path = YtRWrapper.download(
"https://www.youtube.com/watch?v=VIDEO_ID",
"/downloads/video.%(ext)s",
format: "720p",
audio_only: false
)
# Method 2: Using options hash
file_path = YtRWrapper.download(
"https://www.youtube.com/watch?v=VIDEO_ID",
{
output: "/downloads/video.%(ext)s",
format: "720p",
audio_only: false
}
)Retrieves detailed information about a video without downloading it.
Parameters:
url(String): The YouTube video URL
Returns: Hash - JSON object containing video metadata
Example:
info = YtRWrapper.get_info("https://www.youtube.com/watch?v=VIDEO_ID")
puts "Title: #{info['title']}"
puts "Duration: #{info['duration']} seconds"
puts "Views: #{info['view_count']}"The wrapper supports yt-dlp's output templates:
%(title)s- Video title%(id)s- Video ID%(ext)s- File extension%(uploader)s- Channel name%(upload_date)s- Upload date
Example:
template = "/downloads/%(uploader)s/%(title)s.%(ext)s"
YtRWrapper.download("https://www.youtube.com/watch?v=VIDEO_ID", template)Common format options:
"best"- Best available quality (default)"worst"- Worst available quality"720p"- 720p quality"1080p"- 1080p quality"best[height<=720]"- Best quality up to 720p"best[ext=mp4]"- Best MP4 format
The wrapper raises StandardError exceptions when yt-dlp fails:
begin
YtRWrapper.download("https://invalid-url.com")
rescue StandardError => e
puts "Download failed: #{e.message}"
end#!/usr/bin/env ruby
require_relative 'ytr_wrapper'
# Download a video in best quality
puts "Downloading video..."
video_path = YtRWrapper.download(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"/tmp/downloaded_video.%(ext)s"
)
puts "Video downloaded to: #{video_path}"
# Get video information
puts "Getting video info..."
info = YtRWrapper.get_info("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
puts "Title: #{info['title']}"
puts "Duration: #{info['duration']} seconds"
puts "Channel: #{info['uploader']}"
# Download audio only
puts "Downloading audio..."
audio_path = YtRWrapper.download(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
"/tmp/audio.%(ext)s",
audio_only: true
)
puts "Audio downloaded to: #{audio_path}"-
"yt-dlp command not found"
- Ensure yt-dlp is installed:
pip3 install yt-dlp - Check if Python 3 is in your PATH
- Ensure yt-dlp is installed:
-
Permission errors
- Ensure the output directory is writable
- On Linux/macOS, you might need to use
sudofor system directories
-
Network errors
- Check your internet connection
- Some videos might be region-blocked or private
-
Format not available
- Use
YtRWrapper.get_info(url)to see available formats - Try using
"best"format instead of specific resolution
- Use
For more information about yt-dlp options and formats, visit:
This wrapper script is provided as-is. Please respect YouTube's Terms of Service and applicable copyright laws when downloading content.