Digital Life Hacks

How to automatically download songs (mp3) from YouTube using Ruby Script

I asked a friend for some music suggestions, and received a long list of songs. Unsure what to do with it, I wrote a Ruby Script to lookup the songs on YouTube, and download the first link that shows up as an Mp3 file. You can do the same with any list of songs titles.

Pre-requisites:

  • Ruby
  • RubyGem Selenium-Webdriver

If you already have the above two, you can skip installation and head straight to the script.

Installation:

Ruby

For Windows Users, there’s a handy installer
For OS X ruby is pre-installed. Yay!
Linux users can check this out.

RubyGem Selenium-Webdriver

Now, to download the Selenium Webdriver gem (library), run
gem install selenium-webdriver

Script

Assuming you have a list of songs in a file, replace the song_list_filepath with the path to the list.
Just copy scrape_songs.rb, and run ruby scrape_songs.rb

The script runs the following steps:

  • Set Downloads folder to Default location.
  • Skip Download dialog box for audio files.
  • Set Page Timeout to 30 seconds.
  • Open new Firefox window with above profile settings.
  • Read song names from a file.
  • Look up the song name on Youtube, select first video link.
  • Enter video url in a Youtube-Mp3 Converter service.
  • Download the song.
  • Print song names that might have encountered errors.

To modify the script, you can use this easy cheatsheet for Selenium.


Ecouter, Carmen Rizzo
Bruises, Chairlift
Under The Gravel Skies, Charlotte Martin
Stopwatch Hearts (feat. Emily Haines), Delerium
Do You Have a Little Time, Dido
Pony (It's Ok), Erin McCarley
Pitter-Pat, Erin McCarley
I Drive Alone, Esthero
Rafe, Fauxliage


require 'selenium-webdriver'
require "cgi"
profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.folderList"] = 1
profile["browser.helperApps.neverAsk.saveToDisk"] = 'audio/mpeg3,audio/x-mpeg-3,video/mpeg,video/x-mpeg'
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.manage.timeouts.page_load = 30
#song_list_filepath = Path to file containing names of songs in each line. Foe example:
song_list_filepath = "/home/ubuntu/Desktop/list_of_songs_i_want.txt"
songs = File.read(song_list_filepath).split("\n")
songs.each do |song_name|
begin
query = CGI.escape(song_name)
url = "http://youtube.com/results?search_query="
page = driver.navigate.to (url + query)
ele = driver.find_elements(:xpath, '//a[contains(@href, "watch?")]')[0]
video_url = ele.attribute('href')
driver.navigate.to("http://peggo.co/")
ele = driver.find_element(id: 'search-box')
ele.send_keys(video_url)
# driver.find_element(id: 'swap').click unless driver.find_element(id:"id3-title").attribute('value').empty? # Uncomment to swap ID3 tags
driver.find_element(id: 'record-audio').click
# Uncomment for very slow internet speeds
# sleep 5
# driver.find_element(id: 'record-audio').click if driver.find_element(id: 'record-audio').attribute('class') != 'recording'
rescue
p e.message
p song_name
sleep 60
next
end
end
driver.quit

view raw

scrape_songs.rb

hosted with ❤ by GitHub

4 thoughts on “How to automatically download songs (mp3) from YouTube using Ruby Script

    1. There are a lot of services out there for YouTube to mp3 conversion. Most of them get the job done. However if you are intent on using this one, you’ll have to tweak the code above to suit your site.

      Like

  1. Well thats a good approach to do the work automatically and the best part is your code will do exactly how you want it to be done… gr8 work done with easy steps..

    Like

  2. i think we all know that there are many ways to do the job, But this one is to helps guys that study ruby, i wrote this comment cuse i’m tired readying brainless comments

    Like

Leave a comment