Skip to content

Instantly share code, notes, and snippets.

@Madh93
Last active December 16, 2016 07:23
Show Gist options
  • Save Madh93/00e9f0423a2913c9d1fdb7975a805db0 to your computer and use it in GitHub Desktop.
Save Madh93/00e9f0423a2913c9d1fdb7975a805db0 to your computer and use it in GitHub Desktop.
Search and download MP3 from YouTube

yt-to-mp3.rb

Search and download MP3 from YouTube

Requirements

  • Google API Key
  • Ruby (and 'yt' gem)
  • Youtube-dl (and ffmpeg or avconv)
Usage

Usage: ruby yt-to-mp3.rb name

Examples:

ruby yt-to-mp3.rb welcome to the jungle guns n roses
ruby yt-to-mp3.rb out of time a man mano negra
require 'yt'
# Edit!
DEVELOPER_KEY = ''
Yt.configure do |config|
config.api_key = DEVELOPER_KEY
end
# Get videos
videos = Yt::Collections::Videos.new
# S&D: Search and Download
id = videos.where(q: ARGV.join(' '), order: 'relevance').first.id
system("youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=#{id}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment