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

Asked

Viewed 839 times

3

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

1 answer

5


you can use the 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.