Youtube video download URL

后端 未结 5 1958
隐瞒了意图╮
隐瞒了意图╮ 2021-01-29 18:22

I wrote a program that gets youtube video URL and downloads it
Up today I did this:
1. get video \"token\" from \"/get_video_info?video_id=ID\" like:

h         


        
相关标签:
5条回答
  • 2021-01-29 18:59

    Last time I was working on fixing one of the broken Chrome extensions to download YouTube video. I fixed it by altering the script part.

    (Javascript)

    var links = new String();
    var downlink = new String();
    var has22 = new Boolean();
    has22 = false;
    var Marked = false;
    
    var FMT_DATA = fmt_url_map;//This is html text that you have to grab. In case of extension it was readily available through:document.getElementsByTagName('script');
    
    var StrSplitter1 = '%2C', StrSplitter2 = '%26', StrSplitter3 = '%3D';
    if (FMT_DATA.indexOf(',') > -1) { //Found ,
        StrSplitter1 = ',';
        StrSplitter2 = (FMT_DATA.indexOf('&') > -1) ? '&' : '\\u0026';
        StrSplitter3 = '=';
    }
    
    var videoURL = new Array();
    var FMT_DATA_PACKET = new Array();
    var FMT_DATA_PACKET = FMT_DATA.split(StrSplitter1);
    
    for (var i = 0; i < FMT_DATA_PACKET.length; i++) {
        var FMT_DATA_FRAME = FMT_DATA_PACKET[i].split(StrSplitter2);
        var FMT_DATA_DUEO = new Array();
    
        for (var j = 0; j < FMT_DATA_FRAME.length; j++) {
            var pair = FMT_DATA_FRAME[j].split(StrSplitter3);
            if (pair.length == 2) {
                FMT_DATA_DUEO[pair[0]] = pair[1];
            }
        }
    
        var url = (FMT_DATA_DUEO['url']) ? FMT_DATA_DUEO['url'] : null;
    
        if (url == null) continue;
        url = unescape(unescape(url)).replace(/\\\//g, '/').replace(/\\u0026/g, '&');
        var itag = (FMT_DATA_DUEO['itag']) ? FMT_DATA_DUEO['itag'] : null;
        var itag = (FMT_DATA_DUEO['itag']) ? FMT_DATA_DUEO['itag'] : null;
        if (itag == null) continue;
        var signature = (FMT_DATA_DUEO['sig']) ? FMT_DATA_DUEO['sig'] : null;
        if (signature != null) {
            url = url + "&signature=" + signature;
        }
        if (url.toLowerCase().indexOf('http') == 0) { // validate URL 
    
            if (itag == '5') {
                links += '<a href="' + url + '&title=' + username + title + quality240 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v240p">FLV (240p)</span></a>';
            }
            if (itag == '18') {
                links += '<a href="' + url + '&title=' + username + title + quality360 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v360p">MP4 (360p)</span></a>';
            }
            if (itag == '35') {
                links += '<a href="' + url + '&title=' + username + title + quality480 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v480p">FLV (480p)</span></a>';
            }
            if (itag == '22') {
                links += '<a href="' + url + '&title=' + username + title + quality720 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v720p">MP4 HD (720p)</span></a>';
            }
            if (itag == '37') {
                links += ' <a href="' + url + '&title=' + username + title + quality1080 + '"style="text-decoration:none"><span class="yt-uix-button-menu-item" id="v1080p">MP4 HD (1080p)</span></a>';
            }
            if (itag == '38') {
                links += '<a href="' + url + '&title=' + username + title + quality4k + '"style="text-decoration:none"><span class="yt-uix-button-menu-item"  id="v4k">MP4 HD (4K)</span></a>';
            }
    
            FavVideo();
            videoURL[itag] = url;
            console.log(itag);
        }
    }
    

    You can get separate video link from videoURL[itag] array.

    The extension can be downloaded from here.

    I hope this would help someone. This is working solution (as of 06-Apr-2013)

    0 讨论(0)
  • 2021-01-29 19:00

    Actually I'm working on the similar project that downloading the video file from youtube. I find that the get_video might be blocked by Youtube. so instead of using get_video., I use the video info retrieved from get_video_info and extract it to get the video file url.

    Within the get_video_info, there are url_encoded_fmt_stream_map. After encoding it, you can find url and signature value of every video with different format. So the file url is like [url value]+'&signature='+[sig value].

    Additionally I find the following topic that using same method with mine. Hope it can help you.

    Can't Download from youtube

    If you are interested about how to downloading youtube video file, there is a small program written by me to demonstrate the process. You are free to use it.

    https://github.com/johnny0614/YoutubeVideoDownload

    0 讨论(0)
  • 2021-01-29 19:04

    After a lot of failed tries, this github repositories help me:

    https://github.com/rg3/youtube-dl

    Get the url only like:

    youtube-dl 'https://www.youtube.com/watch?v=bo_efYhYU2A' --get-url

    download an mp4 and save as a.mp4 like:

    youtube-dl 'https://www.youtube.com/watch?v=bo_efYhYU2A' -f mp4 -o a.mp4

    Good luck.

    0 讨论(0)
  • 2021-01-29 19:16

    You can get the stream directly by using only

    http://www.youtube.com/get_video_info?video_id=jN0nWjvzeNc

    I made a little script to stream youtube videos in PHP. See how the script get the video file.

    <?php 
    @set_time_limit(0); 
    $id = $_GET['id']; //The youtube video ID
    $type = $_GET['type']; //the MIME type of the video
    
    parse_str(file_get_contents('http://www.youtube.com/get_video_info?video_id='.$id),$info); 
    $streams = explode(',',$info['url_encoded_fmt_stream_map']); 
    
    foreach($streams as $stream){ 
        parse_str($stream,$real_stream); 
        $stype = $real_stream['type']; 
        if(strpos($real_stream['type'],';') !== false){ 
            $tmp = explode(';',$real_stream['type']); 
            $stype = $tmp[0]; 
            unset($tmp); 
        } 
        if($stype == $type && ($real_stream['quality'] == 'large' || $real_stream['quality'] == 'medium' || $real_stream['quality'] == 'small')){ 
            header('Content-type: '.$stype); 
            header('Transfer-encoding: chunked'); 
            @readfile($real_stream['url'].'&signature='.$real_stream['sig']); //Change here to do other things such as save the file to the filesystem etc.
            ob_flush(); 
            flush(); 
            break; 
        } 
    }
    ?>
    

    See the working demo here. I hope this can help you.

    0 讨论(0)
  • 2021-01-29 19:21

    Add &asv=2 to the end of the URL.

    0 讨论(0)
提交回复
热议问题