Skip to content

PabloB07/ytwrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

YouTube Wrapper Script (ytr_wrapper.rb)

A simple Ruby wrapper for yt-dlp that provides an easy-to-use interface for downloading YouTube videos and extracting video information.

Prerequisites

Before using this script, you need to have the following installed on your system:

1. Python 3

  • Windows: Download from python.org
  • macOS: brew install python3 or download from python.org
  • Linux: sudo apt install python3 (Ubuntu/Debian) or sudo yum install python3 (CentOS/RHEL)

2. yt-dlp

Install yt-dlp using pip:

pip3 install yt-dlp

3. Ruby

  • Windows: Download from rubyinstaller.org
  • macOS: brew install ruby or use the built-in Ruby
  • Linux: sudo apt install ruby (Ubuntu/Debian) or sudo yum install ruby (CentOS/RHEL)

Installation

  1. Clone or download the script to your desired directory
  2. Ensure the script has executable permissions (Linux/macOS):
    chmod +x ytr_wrapper.rb

Usage

Basic Usage

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")

Advanced Usage with Options Hash

# 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)

Getting Video Information

# 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 description

Method Reference

YtRWrapper.download(url, options_or_path = {}, format: "best", audio_only: false)

Downloads a video from the provided URL.

Parameters:

  • url (String): The YouTube video URL
  • options_or_path (Hash or String): Either an options hash or output path string
  • format (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
  }
)

YtRWrapper.get_info(url)

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']}"

Output Path Templates

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)

Format Selection

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

Error Handling

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

Example Script

#!/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}"

Troubleshooting

Common Issues

  1. "yt-dlp command not found"

    • Ensure yt-dlp is installed: pip3 install yt-dlp
    • Check if Python 3 is in your PATH
  2. Permission errors

    • Ensure the output directory is writable
    • On Linux/macOS, you might need to use sudo for system directories
  3. Network errors

    • Check your internet connection
    • Some videos might be region-blocked or private
  4. Format not available

    • Use YtRWrapper.get_info(url) to see available formats
    • Try using "best" format instead of specific resolution

Getting Help

For more information about yt-dlp options and formats, visit:

License

This wrapper script is provided as-is. Please respect YouTube's Terms of Service and applicable copyright laws when downloading content.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages