youtube video downloaded successfully on local, but on server(online) only 0 byte flv is downloading

I'm using php tube class to download youtube video. Video is automatically downloaded in a folder by submitting youtube video url.

everything is working fine on local (XAMPP), but when I upload this to server(online), THEN ONLY 0 byte flv file is saved.

I also debug the code both side( local and global ).

I get the url like this( on local)

http://www.youtube.com/get_video?video_id=1jNWCFwqPvM&=vjVQa1PpcFMyYCECyfmYENWklpwXGyhVgJpSFPgNAEc%3D

whereas on server I got link like this

http://www.youtube.com/ get_video?video_id=1jNWCFwqPvM&t=vjVQa1PpcFNS3SPxIXi8hy-NF4qZFFrScDXCjGLLrLc%3D

when I copy the link getting from local and paste on browser then a download box appear on browser, whereas when I copy the link getting from server and paste on browser then only a blank page appears

  1. please solve my problem, where is the error?
  2. how to know that pear is installed or not on server?
  3. on server side sometimes I get link like

    http://www.youtube.com/get_video?video_id=1jNWCFwqPvM&t=vjVQa1PpcFNS3SPxIXi8hy-NF4qZFFrScDXCjGLLrLc%3D

but sometimes I get only

http://www.youtube.com/get_video?video_id=&t=

How to solve these?

UPDATE:

I debug all the code and found that file_get_contents() return string(0) on server whereas on local it return some integer value. allow_url_fopen is onmy server

this is index.php

include_once('functions.php');  
$url =  $_POST['url']; // here is url of youtube video
$pattern = getPatternFromUrl($url); 
$flv_path = GrabFlvFromYoutube($pattern );
echo "File Successfully Downloaded at:".$flv_path."<br/>";

these functions are in functions.php

function getPatternFromUrl($url)
{
$url = $url.'&';
$pattern = '/v=(.+?)&+/';
preg_match($pattern, $url, $matches);
//echo $matches[1]; die;
return ($matches[1]);
}

and

function GrabFlvFromYoutube( $pattern )
{
require_once ("phptube.php");
$tube = new PHPTube ();
$flv_http_path = $tube->download($pattern) ;
    echo "<br/>Complete URL:".$flv_http_path;
set_time_limit(0);

$data = file_get_contents($flv_http_path);  
//var_dump(file_get_contents($flv_http_path));  
$new_flv_path = dirname(_FILE_).'/flvs/'.$pattern.'-'.time().'.flv' ;

echo "<br />File uploaed:";
    file_put_contents($new_flv_path, $data);    

return $new_flv_path ;
}

below function in phptube.php

class PHPTube {

var $cookies;
var $mgr;
var $req; 
var $debug = true;
var $auth = false;

function PHPTube () {}
    function download ($video_id) {

    $url = "http://www.youtube.com/watch?v=".$video_id;     
$this->req =& new HTTP_Request($url);
$response = $this->req->sendRequest();
if (PEAR::isError($response)) {
    $response->getMessage()."\n";
} else {    
        $page = $this->req->getResponseBody();  
    $vpattern = '/v=(.*?)&/';
    //$vpattern ='/video_id:(.*?)/';
    preg_match($vpattern,$page,$mv);
    //preg_match('&"video_id": "(.*?)"&', $page, $mv); 
    echo "<br />Video ID:".$v_id = $mv[1]; 
    //$tpattern='/"t": "(.*?)"/';
    $tpattern = '/&t=(.*?)&/';
    preg_match($tpattern,$page,$tickets);
    echo "<br />Token ID:".$token = $tickets[1];
    $curl = "video_id=";
    $curl .= $v_id;
    $curl .= "&t=";         
    $curl.= $token;
    //echo "<br />Query String:".$curl; die;
    $url = "http://www.youtube.com/get_video?".$curl;  
    if ($this->debug)
     return $url;
     }
   }
 }  

Answers


To download/copy youtube video to your server... u can do that through SSH.

1) Download the latest version of the youtube-dl script (here: http://rg3.github.io/youtube-dl/ Save it as youtube-dl.py and upload it to wherever you want on your server.

2) To download a video, login to your server via ssh (using putty or whatever), navigate to the directory where you saved the youtube-dl script, and download the video by entering a command like:

Code:

python youtube-dl.py -o youroutputfile.mp4 http://www.youtubevideourl.com/

where the filename you specify after the -o part is the filename you want to save the video as, and the youtubevideourl part would be something like http://www.youtube.com/watch?v=_DHEK9xJXvs ie. the actual video page url from Youtube. The script will fetch the youtube page, extract the video location and save the video. If it stops working, that means that Youtube has changed their page formatting and you have to download a new version of youtube-dl from the site.

This is a great script because since it runs from the command line, it's very easy to incorporate it into your own scripts for doing batch downloads.


Did you at least try the user agent idea I mentioned in your last question? I'll paste it here again if you didn't.

ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');

And you're going to be pasting that at the top of your script file. You can also change this in your php.ini file under the user_agent setting.

The only thing I can imagine that would be different between your server and your local machine is that the server is identifying itself differently.

The other thing here is that YouTube could be blocking your server/web host from requesting videos. If you're on a shared host and somebody tried to do this stuff before, Google could have cracked down on it and blacklisted the IP of your host. Downloading FLVs from YouTube is against the terms of service, so they may very well have done this.

Hope this helps (again).


I think it's something to do with the ip of the machine that attempts to parse their sourcecode or grab a link. On a local machine, since you pull the page, the link will have your ip in the ip param, if it's done on a remote server, that servers ip will be in the ip param and you'll be trying to download from your machine where the ips don't match.

I'm also facing a similiar situation where it's fine on my dev machine but once I put it live it's all gone to pot. I'm trying to figure out how to get it to work, atm the easiest/only thing I can think of is doing on the client with javascript.

EDIT:

My idea of requesting the youtube page using ajax on the client will not work as you can't request from another domain.

The only other way I can think of to get it to work, is let the server get the download link which will be valid for the server's ip, then proxy the file download using readfile() and some header()'s to the client. The big problem with this is that you will absolutely smash your bandwidth streaming downloads to users.

I'd like to know if anyone has any better ideas?


Need Your Help

Laravel 4 : trying to get variable from same page

php ajax laravel

I'm new to Laravel and I'm getting an error which I think has more to do with logic than anything else but I can't quite seem to grasp how to overcome it.