Download youtube videos using PEAR


Hits: 2124  

PEAR have rich library to access web based resources easily…

<?php

$sv = new SVTube();
$sv->download(“D7cm-yu-CP0”, “svnlabs.flv”)

?>

Class: SVTube.php
——————————

<?php

require_once ‘HTTP/Client.php’;
require_once ‘HTTP/Request.php’;

class SVTube {

var $req;
var $debug = false;
var $auth = false;

function download ($video_id, $video_filename) {
$url = “http://www.youtube.com/watch?v=”.$video_id;
$this->req =& new HTTP_Request($url);
$response = $this->req->sendRequest();

if (PEAR::isError($response)) {
echo $response->getMessage().”\n”;
} else {
$page = $this->req->getResponseBody();

preg_match(‘/\&t=([^”]*)”/si’, $page, $match);

$html=html_entity_decode(urldecode(str_replace(‘\x’, ‘%’, $match[1])),ENT_QUOTES, “UTF-8”);

$echo = explode(“&”, $html);

$url = “http://www.youtube.com/get_video?el=detailpage&t=”.$echo[0].”&fmt=5&asv=3&video_id=”.$video_id;

if ($this->debug)
return $url.”\n”;

$req =& new HTTP_Request($url,array (“allowRedirects”=>true, “maxRedirects”=> 99));
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->addHeader(“User-Agent”,”Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)”);
$response = $req->sendRequest();

$req->getResponseBody();

if (PEAR::isError($response)) {
//echo $response->getMessage().”\n”;
return “Error: Failed to open video file on YouTube\n”;
} else {
if ($o = fopen ($video_filename, “w”)) {
fwrite($o,$req->getResponseBody());
fclose ($o);
return “Download complete! File: “.$video_filename.”\n”;
} else {
return “Error: Failed to open video-file\n”;
}
}

}
}

}
?>

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch

4 thoughts on “Download youtube videos using PEAR”

  1. Hi there! This is my 1st comment here so I just wanted to give a quik shouut oout andd tell you I genuinely enjoy reading your posts.
    Can you suggest any othuer blogs/websites/forums that deal with the samee subjects?
    Thanks for your time!

  2. hello there and thank yoou for your info – I’ve definitely picked up anything
    new from rught here. I did however expertise a feww technical points using this
    web site, as I experienced to reload the site lots off times previous to I
    could get it to load correctly. I had been wondering if your
    hosting is OK? Not that I am complaining, but slow loading instances times
    will often affwct your placement in google and can damage your quality score iff ads and marketing with Adwords.
    Anyway I am adding this RSS to my email and could look out for a lot more of your respective
    interesting content. Make sre you update this agaain soon..

  3. Jusst wish to say your article is as astonishing.

    The clearness for your putt up is just cool and that i can assume you’re an expert in this subject.

    Welll together with your permission alllow me to snatch your RSS feed to stay updated with cominng near near post.

    Thankms one million and please keep up the rewarding
    work.

Leave a Reply

Your email address will not be published.

*