How to download videos from youtube and convert them to mp3 using ruby?

3

I need a simple ruby way, which allows you to download videos from youtube, but I only need the audio of the video (mp3).

    
asked by anonymous 21.10.2014 / 14:53

1 answer

4

You can use youtube-dl to do this, and using Ruby would be simple.

system("youtube-dl -t --extract-audio --audio-format mp3 http://www.youtube.com/watch?v=#{id_do_video}")

If you want to download all videos from a channel, you can do the following:

require 'rubygems'
require 'active_support'
require 'httparty'

quantidade_maxima = 50
canal = 'nome_do_canal'

#1,51,101 são usado para paginação, como a api do youtube só permite que você traga apenas 50 vídeos por vez.
[1,51,101].each do |offset|
  feed = ActiveSupport::JSON.decode(HTTParty.get("https://gdata.youtube.com/feeds/api/videos?q=#{canal}&max-results=#{quantidade_maxima}&v=2&alt=jsonc&orderby=published&start-index=#{offset}").body)
  feed["data"]['items'].each do |video|
    system("youtube-dl youtube-dl -t --extract-audio --audio-format mp3 http://www.youtube.com/watch?v=#{video['id']}") rescue puts "erro"
  end
end

I hope I have helped.

    
21.10.2014 / 15:07