Using get_video on YouTube to download a video

15,563

Solution 1

Sorry, that is not possible anymore. They limit the token to the IP that got it.

Here's a workaround by using the get_headers() function, which gives you an array with the link to the video. I don't know anything about ios, so hopefully you can rewrite this PHP code yourself.

<?php
if(empty($_GET['id'])) {
    echo "No id found!";
}

else {

    function url_exists($url) {
        if(file_get_contents($url, FALSE, NULL, 0, 0) === false) return false;
        return true;
    }

    $id = $_GET['id'];

    $page = @file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$id);

    preg_match('/token=(.*?)&thumbnail_url=/', $page, $token);

    $token = urldecode($token[1]);

    $get = $title->video_details;

    $url_array = array ("http://youtube.com/get_video?video_id=".$id."&t=".$token,
    "http://youtube.com/get_video?video_id=".$id."&t=".$token."&fmt=18");

    if(url_exists($url_array[1]) === true) {
        $file = get_headers($url_array[1]);
    }

    elseif(url_exists($url_array[0]) === true) {
        $file = get_headers($url_array[0]);
    }

    $url = trim($file[19],"Location: ");

    echo '<a href="'.$url.'">Download video</a>';
}
?>

Solution 2

I use this and it rocks: http://rg3.github.com/youtube-dl/

Just copy a YouTube URL from your browser and execute this command with the YouTube URL as the only argument. It will figure out how to find the best quality video and download it for you.

Solution 3

Great! I needed a way to grab a whole playlist of videos.

In Linux, this is what I used:

y=http://www.youtube.com; f="http://gdata.youtube.com/feeds/api/playlists/PLeHqhPDNAZY_3377_DpzRSMh9MA9UbIEN?start-index=26"; for i in $(curl -s $f |grep -o "url='$y/watch?v=[^']'");do d=$(echo $i|sed "s|url\='$y/watch?v=(.)&.*'|\1|"); youtube-dl --restrict-filenames "$y/watch?v=$d"; done

You have to find the playlist ID from a common Youtube URL like: https://www.youtube.com/playlist?list=PLeHqhPDNAZY_3377_DpzRSMh9MA9UbIEN

Also, this technique uses gdata API, limiting 25 records per page.
Hence the ?start-index=26 parameter (to get page 2 in my example)

This could use some cleaning, and extra logic to iterate thru all sets of 25, too.

Credits:

https://stackoverflow.com/a/8761493/1069375

http://www.commandlinefu.com/commands/view/3154/download-youtube-playlist (which itself didn't quite work)

Share:
15,563
JonasG
Author by

JonasG

iOS app developer.

Updated on June 08, 2022

Comments

  • JonasG
    JonasG almost 2 years

    I am trying to get the video URL of any YouTube video like this:

    Open

    http://youtube.com/get_video_info?video_id=VIDEOID
    

    then take the account_playback_token token value and open this URL:

    http://www.youtube.com/get_video?video_id=VIDEOID&t=TOKEN&fmt=18&asv=2
    

    This should open a page with just the video or start a download of the video. But nothing happens, Safari's activity window says 'Not found', so there is something wrong with the URL. I want to integrate this into a iPad app, and the javascript method to get the video URL I use in the iPhone version of the app isn't working, so I need another solution.

    YouTube changes all the time, and I think the URL is just outdated. Please help :)

    Edit: It seems like the get_video method doesn't work anymore. I'd really appreciate if anybody could tell me another way to find the video URL.

    Thank you, I really need help.

  • JonasG
    JonasG over 12 years
    But you are using get_url too? And what is get_headers(), is it part of the url or a function? Could you explain your code in words? I would really like to give you the bounty, I just need a little more help. Thank you
  • JonasG
    JonasG over 12 years
    I hust looked at it again and then I totally got it :D but why doesn't it give links to all formats in every resolution (in 720p it gives mp4 and x-flv, in 480p it gives the links for flv and x-flv ...) Is there a way of getting the links for mp4 video in all resolutions? thanks
  • Gepsens
    Gepsens over 12 years
    Wow so they put a real video location in the header ? sounds like a major flaw...
  • JonasG
    JonasG over 12 years
    It is a lot more code in objc because URL parsing is way harder there, but you gave me the right idea. Bounty awarded!