Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download Videos from Youtube in (Ruby || PHP)
<?php
// Download Videos from Youtube in PHP
// By: Sheharyar Naseer
$id = 'RllJtOw0USI'; // just in case
if (isset($_GET["id"]))
$id = $_GET["id"];
parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id), $video_data);
$streams = $video_data['url_encoded_fmt_stream_map'];
$streams = explode(',',$streams);
$counter = 1;
foreach ($streams as $streamdata) {
printf("Stream %d:<br/>----------------<br/><br/>", $counter);
parse_str($streamdata,$streamdata);
foreach ($streamdata as $key => $value) {
if ($key == "url") {
$value = urldecode($value);
printf("<strong>%s:</strong> <a href='%s'>video link</a><br/>", $key, $value);
} else {
printf("<strong>%s:</strong> %s<br/>", $key, $value);
}
}
$counter = $counter+1;
printf("<br/><br/>");
}
// start server and go to http://url/?id=video-id
?>
# 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 = "ekz-FY_MDGA"
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'][0..70]}......\n\n"
end
puts "(Modify the code to get full urls)"

Youtube Downloaders

Download Youtube Videos using Ruby or PHP

Just copy the appropriate script, provide the video_id and run. It will list download links for different qualities and streams.

:P

@KristupasSavickas

This comment has been minimized.

Copy link

commented May 14, 2016

Can I use the stream link to download the video somehow?

@rudrawar3

This comment has been minimized.

Copy link

commented Sep 4, 2016

Great Script !!
But i am able to download only small quality. medium and hd720 videos are not downloading instead it streams (play) once click on download. Please help if any modifications are required.

@hiennv0802

This comment has been minimized.

Copy link

commented Mar 6, 2017

Could i get the stream link of officer video? For example https://www.youtube.com/watch?v=dMK_npDG12Q
Please tell me how should i do, thanks!

@eoinahern

This comment has been minimized.

Copy link

commented Mar 15, 2018

this still work? looking to get the video file url

@phuchptty

This comment has been minimized.

Copy link

commented Jun 15, 2018

well, it can download video without copyright

@mohittiwari5

This comment has been minimized.

Copy link

commented Oct 18, 2018

Not Working with many Videos.
I am mentioning 2 video id:
1.fRh_vgS2dFE
2.OGGHKpV01cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.