Skip to content

Instantly share code, notes, and snippets.

@davetroy
Last active December 17, 2015 00:39
Show Gist options
  • Save davetroy/5522715 to your computer and use it in GitHub Desktop.
Save davetroy/5522715 to your computer and use it in GitHub Desktop.
This simple ruby script will allow searching for and downloading video files from Youtube (as mp4 files) and then converting them to mp3 audio files, suitable for uploading to Soundcloud or a web server.
require 'rubygems'
require 'youtube_search' # https://github.com/grosser/youtube_search
require 'youtube_dl' # https://github.com/tonic20/youtube_dl
FFMPEG = "/usr/local/bin/ffmpeg"
res = YoutubeSearch.search('music|song|musicians|performance|instruments', :author => 'TEDxTalks', :per_page => 50, 'orderby' => 'viewCount')
res.each do |r|
puts "https://www.youtube.com/watch?v=#{r['video_id']} <#{r['title']}>"
youtube = YoutubeDl::YoutubeVideo.new("http://www.youtube.com/watch?v=#{r['video_id']}", :location => './video')
unless File.exist?("./audio/#{r['video_id']}.mp3")
puts youtube.download_video
`#{FFMPEG} -i ./video/#{r['video_id']}.mp4 ./audio/#{r['video_id']}.mp3`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment