Created
May 17, 2017 00:23
-
-
Save flanker/905e4b5b2415aaba250b369e1d452165 to your computer and use it in GitHub Desktop.
Parse YouTube video source file urls from URL id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download Videos from Youtube in Ruby | |
# By: Sheharyar Naseer | |
require 'open-uri' | |
require 'cgi' | |
def tube(id) | |
'http://youtube.com/get_video_info?video_id=' + id | |
end | |
def get_video_data(id) | |
CGI.parse open(tube(id)).read | |
end | |
def get_video_streams(id) | |
streams = get_video_data(id)['url_encoded_fmt_stream_map'].first.split(',') | |
streams.map do |s| | |
x = CGI.parse s | |
x.each do |k,v| | |
if k == 'type' | |
x[k] = v.first.split('; ') | |
else | |
x[k] = v.first | |
end | |
end | |
end | |
end | |
# Get Videos | |
video_id = "THE_VIDEO_ID" | |
streams = get_video_streams(video_id) | |
puts "### Total #{streams.count} streams available:\n\n" | |
streams.each_with_index do |s,i| | |
puts "Stream #{i+1}", | |
"-------------", | |
"Quality: #{s['quality']}", | |
"Type: #{s['type'].first}", | |
"URL: #{s['url']}\n\n" | |
end | |
puts "(Modify the code to get full urls)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment