Using php to download youtube flvs.

update: I also just made a post on how to download vimeo videos with php.

Recently in a project I needed the ability to get the raw flv from a youtube video. This is harder than it looks. After a lot of searching online I realized that noone has posted a complete set of code to do this, or the ones that are online no longer work because youtube has updated it’s code. So here is how I accomplished this.

You can download the actual completed youtube scraper class here: http://1chris.com/wp-content/uploads/2011/08/youtubegrabber.class.php_.txt 

I used to have a tutorial on this page, but took it off because I would update the code and not the tutorial. If you want to understand it, just look through the raw code.

If you rename that file to be a .php file and open it and go to the very bottom of the file you will see some lines that look like this:

//$video_url = "http://www.youtube.com/watch?v=60og9gwKh1o";
//$filename = "test.flv";
//$youtube_video_grabber = new youtubegrabber($video_url, $filename);
//echo "<p>done downloading... saved as: {$filename}</p>";

If you uncomment those lines so they look like this:

$video_url = "http://www.youtube.com/watch?v=60og9gwKh1o";
$filename = "test.flv";
$youtube_video_grabber = new youtubegrabber($video_url, $filename);
echo "<p>done downloading... saved as: {$filename}</p>";

Then simply run that .php file, it will download a youtube video and save it in the same directory as the php file. It will call it test.flv. That is basically how you run this. All you do is set the $video_url variable to the url you use to view that youtube video in a browser, then set $filename to the name of the file you want to save it as, it will download that youtube video and save it as that file name.

If this script doesn’t work, first off check that php has write permissions to the directory it’s saving to. Also, you will need curl, so make sure you have that installed. You can also post your errors here and I will try to help as best as I can.

Update: Feb. 1 2012 – youtube recently changed it’s code again. I updated my code also just now. It should work again. As always let me know if it doesn’t seem to work.

  1. Great job you did here man.
    I was wondering if there could be anyway to get just the audio part using a ffmpeg library or something else. I’m not quite sure cause the link for the download is full of parameters.

    Anyway, thanks a lot for the code.

  2. Albert Kühner says:

    Great. How can I download the MP4 file instead?

    • Naveen Gautam says:

      Really awesome work dude!! but I want video in mp4 format. Then can you help me for this?
      Thanks in advance.

  3. Tom says:

    This script is working great but it has the same problem as the most similar script have.
    The video cant be downloaded if the script is installed on a remote server. So it workd only if the script runs under localhost.
    Any help to make it run remotely?

    • Ibrahim says:

      You must enable curl option followlocation and your php must have be running under safe_mode. I works on all servers.

  4. Kh30pS says:

    hey.. u forget a ” } ” at function get_flv_urls() ; Can u tell me the right place where to put it? Thank you

  5. kishan says:

    output of class says can’t open file ??
    im stuck plz help

  6. john says:

    Hiya,
    Does this code still work? If so, I’d like to learn how to apply it. I got here from sajithmr’s website, and basically what I was doing with that script was sending a youtube video to a page on localhost that contained sajithmir’s code. I was doing this in steps in a template made in Zennoposter, and I’m not yet sure if it was the code that was at fault or if it was me.

    I’d like to learn how to apply it but before I go through yet another huge (and probably failed) effort at trying to apply a piece of php to a webpage, I’d like to make sure I’m dealing with something that’s still working.

    If it is, I would like to ask you where on earth in this script do I insert the actual youtube webpage. I’ve asked other people before about php and how it seems to declare things without actually declaring them, and it’s driving me absolutely bonkers, and yet they tend to direct me to a book on how to learn php, but I can’t do that because I go to those books and also can’t understand them. What do I mean by this, well, if you notice right at the beginning of your script, you say:

    function curl_get_url($youtube_url){

    Would the youtube webpage go where $youtube_url is?

    Would it be

    function curl_get_url(youtube.com/watch?=ehowfhdsfuoh){

    And the second question would be, how would I actually run this script? If you notice, before I mentioned my bad experience with Zennoposter, which is a great program but I’m a noob with awful luck. If I may rant one second about that luck, I’ll tell you I tried running a program https://github.com/rg3/youtube-dl/downloads but either because I’ve installed the wrong python in my Vista, or my Vista doesn’t like me writing a path in its environment variables so that ZP could actually access the damn program it failed, I tried sathimir’s script by sending a youtube webpage to the script in a Zennoposter step but failed, and then I tried googleneedle.com but it can’t be automated because IE9 just won’t let you deactivate the prompt window.

    So would I just set up a webpage in localhost with your php on it? Then open the browser and it would fetch the youtube webpage?

    In order to automate this, would I do the following- get this part of what you said:

    function get_youtube_video_id($youtube_url){
    $thearray = explode(“watch?v=”, $youtube_url);
    return $thearray[1];
    }

    And think to myself, Ok, the youtube_url means youtube.com, and at watch?v= he’s referring to the different webpages? Then I’d create a template in ZennoPoster that would create one array for every video I want to download. And that would create a function for every video I’d want to download. I would then fetch the functions and input them into the script. But I’m at a loss as to where the inputting would actually occur.

    Ok, hold to that thought while I try to look for an easier way out. At the very beginning you say to just go directly to the class. Let’s hope this works for me, if not I’m screwed. What I mean is, perhaps all I need to do is copy the webpage youtubewebpagegrabber_class.php is on, and create a webpage of it in my localhost Xampp installation. And then merely run a youtube webpage through your script. And by ‘run’ I mean just substitute part of the script with the url I want to download. But the question would be, what part of the script would I change, where here would I insert my desired youtube page? At the beginning where it says var $youtube_video_url;?

    I’m so angry at php right now. A ton of times I’ve tried to understand people talking about it, but I can’t get past the simple notions. I also have reading and complexity issues as you can see. When I’m faced by scripts like this that talk about things as if they’ve already been declared, it drives me mad.

    youtube_video_url = $youtube_video_url;

    $this->final_flv_filename = $final_flv_filename;

    $this->youtube_video_id = $this->get_youtube_video_id();

    $this->cookies_path = “cookies.txt”;
    $clear_cookies = $this->clear_cookies();

    $this->curl_headers = array(
    “Accept-Language: en-us”,
    “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15″,
    “Connection: Keep-Alive”,
    “Cache-Control: no-cache”
    );

    // load the youtube page to “set” any cookies that youtube might look for
    $this->youtube_page_html = $this->curl_get_url($this->youtube_video_url);

    Anyway, thanks for any help you can give me, I appreciate it.

  7. Daniel says:

    Great code, dude :)
    I was looking for this for couple of days and finally found this awesome stuff. Thanks.

  8. Hp says:

    Still working? I tried. But file size is zero.

  9. Gaspare says:

    Hi, thank you for the script!
    It work for me fine, but it is possible to run it in background?
    I tried to write this exec line but it not work:
    exec (“new youtubegrabber($url, $filename, 0) > /dev/null 2>&1 &”);

    Thank you for suggestion!

  10. cruzy says:

    So great !!! Good job Chris and thanks a lot !!!!!! It work perfectly !!
    Cheers from France !

  11. Jordi says:

    Tried with severals videos and only got a 0 size file

  12. winracer says:

    You Have Done a Great job!!!!!!!!!!!!!!!!!!! I love it!!!!!!!!!!!!!!!!!!

  13. Ibrahim says:

    2012 update: Working just perfect

    Make sure your server is not running under safe_mode and CURL_FOLLOWLOCATION is not issuing warning, and in fact check if it is working or not, as on some server it is enabled but doesn’t follow next location.

    Thanks chris

  14. jenkins says:

    Can someone edit it,so that it simply gives a link to the .flv file from a url,that was submitted as $_POST['url']; ? Thanks :)

  15. marshall lee says:

    great job, i am looking for this whole day ,thx..i am just wondering how you know the rule to get the flv address? from china hainan

  16. Rinek says:

    Hi! Thank you for great script!
    It is possible also grab videos under age-restricted? Now they have 0byte size.
    Thank you !

  1. [...] time I wrote the program in PHP and used a script from a blog I found after looooong research, ZendGdata and FFMpeg. The videos are downloaded in FLV format and [...]

Leave a Reply