PHP获取1080p质量或更高质量的youtube视频

我已经尝试了以下代码来获得具有所有可能品质的youtube,但它只获得720作为最高质量,即使视频支持更高的品质

function get_youtube($code)
{
    $data = file_get_contents('http://youtube.com/get_video_info?video_id=' . $code . '&el=vevo&fmt=18&asv=2&hd=1');

    parse_str($data , $details);

    foreach (explode(',' , $details['url_encoded_fmt_stream_map']) AS $quality) {
        parse_str($quality , $video);

        echo '<pre>';
        print_r($video);
        echo '</pre>';
    }



}


get_youtube('kObNpTFPV5c');

这是输出

Array
(
    [url] => http://r6---sn-uxaxjvhxbt2u-j5pe.googlevideo.com/videoplayback?fexp=9407704%2C9408501%2C9408710%2C9413129%2C9414764%2C9416126%2C9417683%2C9417707%2C9418199%2C9418401%2C9420453%2C9421263%2C9422596%2C9422618%2C9423291%2C9423485%2C9423510%2C9423517%2C9423662%2C9423790%2C9423850&initcwndbps=77500&mm=31&ipbits=0&mn=sn-uxaxjvhxbt2u-j5pe&id=o-AHdjJTLxcSNrbHE2YvslYfAJ98q_9Y_6R6WrAEWWkVLp&key=yt6&source=youtube&dur=187.315&mv=m&ms=au&ip=197.34.194.196&mime=video%2Fmp4&pl=18&ratebypass=yes&mt=1446977625&signature=64AAB1E44B70947580FEF4C4C8C6480D8CF6C79E.7024922C204CCCEF585244ACE62D4DAE5334328E&lmt=1428044311139785&itag=22&upn=8DPUpy5oAuE&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&expire=1446999313&sver=3
    [quality] => hd720
    [fallback_host] => tc.v13.cache3.googlevideo.com
    [type] => video/mp4; codecs="avc1.64001F, mp4a.40.2"
    [itag] => 22
)
Array
(
    [url] => http://r6---sn-uxaxjvhxbt2u-j5pe.googlevideo.com/videoplayback?fexp=9407704%2C9408501%2C9408710%2C9413129%2C9414764%2C9416126%2C9417683%2C9417707%2C9418199%2C9418401%2C9420453%2C9421263%2C9422596%2C9422618%2C9423291%2C9423485%2C9423510%2C9423517%2C9423662%2C9423790%2C9423850&initcwndbps=77500&mm=31&ipbits=0&mn=sn-uxaxjvhxbt2u-j5pe&id=o-AHdjJTLxcSNrbHE2YvslYfAJ98q_9Y_6R6WrAEWWkVLp&key=yt6&source=youtube&dur=0.000&mv=m&ms=au&ip=197.34.194.196&mime=video%2Fwebm&pl=18&ratebypass=yes&mt=1446977625&signature=9E7F214529C2B4A62F60D509DFED84DB80382B64.E1DC16FEA47C1C12BB1F784DD4B8A666D743BA93&lmt=1392630424551396&itag=43&upn=8DPUpy5oAuE&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&expire=1446999313&sver=3
    [quality] => medium
    [fallback_host] => tc.v23.cache5.googlevideo.com
    [type] => video/webm; codecs="vp8.0, vorbis"
    [itag] => 43
)
Array
(
    [url] => http://r6---sn-uxaxjvhxbt2u-j5pe.googlevideo.com/videoplayback?fexp=9407704%2C9408501%2C9408710%2C9413129%2C9414764%2C9416126%2C9417683%2C9417707%2C9418199%2C9418401%2C9420453%2C9421263%2C9422596%2C9422618%2C9423291%2C9423485%2C9423510%2C9423517%2C9423662%2C9423790%2C9423850&initcwndbps=77500&mm=31&ipbits=0&mn=sn-uxaxjvhxbt2u-j5pe&id=o-AHdjJTLxcSNrbHE2YvslYfAJ98q_9Y_6R6WrAEWWkVLp&key=yt6&source=youtube&dur=187.315&mv=m&ms=au&ip=197.34.194.196&mime=video%2Fmp4&pl=18&ratebypass=yes&mt=1446977625&signature=9DBDA774B8DC24AEA511068C15A08A3627A8749B.81C8E8E09D42ABF76767107A14D826C2667111C2&lmt=1428044297913452&itag=18&upn=8DPUpy5oAuE&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cupn%2Cexpire&expire=1446999313&sver=3
    [quality] => medium
    [fallback_host] => tc.v12.cache8.googlevideo.com
    [type] => video/mp4; codecs="avc1.42001E, mp4a.40.2"
    [itag] => 18
)
Array
(
    [url] => http://r6---sn-uxaxjvhxbt2u-j5pe.googlevideo.com/videoplayback?initcwndbps=77500&mm=31&ipbits=0&fexp=9407704%2C9408501%2C9408710%2C9413129%2C9414764%2C9416126%2C9417683%2C9417707%2C9418199%2C9418401%2C9420453%2C9421263%2C9422596%2C9422618%2C9423291%2C9423485%2C9423510%2C9423517%2C9423662%2C9423790%2C9423850&id=o-AHdjJTLxcSNrbHE2YvslYfAJ98q_9Y_6R6WrAEWWkVLp&mt=1446977625&dur=187.298&mv=m&ms=au&ip=197.34.194.196&lmt=1392829058541109&upn=8DPUpy5oAuE&expire=1446999313&sver=3&mn=sn-uxaxjvhxbt2u-j5pe&key=yt6&source=youtube&mime=video%2Fx-flv&pl=18&signature=40557D072DA23780D096D078F30D7BFD1C9D0F2F.502E2780000521A6CDDF48B27EB123CD4BECAAEB&itag=5&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Csource%2Cupn%2Cexpire
    [quality] => small
    [fallback_host] => tc.v22.cache2.googlevideo.com
    [type] => video/x-flv
    [itag] => 5
)
Array
(
    [url] => http://r6---sn-uxaxjvhxbt2u-j5pe.googlevideo.com/videoplayback?initcwndbps=77500&mm=31&ipbits=0&fexp=9407704%2C9408501%2C9408710%2C9413129%2C9414764%2C9416126%2C9417683%2C9417707%2C9418199%2C9418401%2C9420453%2C9421263%2C9422596%2C9422618%2C9423291%2C9423485%2C9423510%2C9423517%2C9423662%2C9423790%2C9423850&id=o-AHdjJTLxcSNrbHE2YvslYfAJ98q_9Y_6R6WrAEWWkVLp&mt=1446977625&dur=187.477&mv=m&ms=au&ip=197.34.194.196&lmt=1392829550794432&upn=8DPUpy5oAuE&expire=1446999313&sver=3&mn=sn-uxaxjvhxbt2u-j5pe&key=yt6&source=youtube&mime=video%2F3gpp&pl=18&signature=9DA671B517F7D670FEFADB85E1144BCAB8661A87.E35E11206B883D3B086FEBED74255FFAE699C8D4&itag=36&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Csource%2Cupn%2Cexpire
    [quality] => small
    [fallback_host] => tc.v12.cache6.googlevideo.com
    [type] => video/3gpp; codecs="mp4v.20.3, mp4a.40.2"
    [itag] => 36
)
Array
(
    [url] => http://r6---sn-uxaxjvhxbt2u-j5pe.googlevideo.com/videoplayback?initcwndbps=77500&mm=31&ipbits=0&fexp=9407704%2C9408501%2C9408710%2C9413129%2C9414764%2C9416126%2C9417683%2C9417707%2C9418199%2C9418401%2C9420453%2C9421263%2C9422596%2C9422618%2C9423291%2C9423485%2C9423510%2C9423517%2C9423662%2C9423790%2C9423850&id=o-AHdjJTLxcSNrbHE2YvslYfAJ98q_9Y_6R6WrAEWWkVLp&mt=1446977625&dur=187.663&mv=m&ms=au&ip=197.34.194.196&lmt=1392829435542191&upn=8DPUpy5oAuE&expire=1446999313&sver=3&mn=sn-uxaxjvhxbt2u-j5pe&key=yt6&source=youtube&mime=video%2F3gpp&pl=18&signature=A679E5F82FC42BCF571B379532A4567BF38053.07E9D8174A2A8EFCFC717E485F01A9DC60FC51BC&itag=17&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Csource%2Cupn%2Cexpire
    [quality] => small
    [fallback_host] => tc.v17.cache4.googlevideo.com
    [type] => video/3gpp; codecs="mp4v.20.3, mp4a.40.2"
    [itag] => 17
)

在github上几乎尝试了每个关于php和youtube的库,但是没有人和我一起工作

最佳答案 您可以获得1080p质量或更高质量的YouTube视频

function get_youtube($code)
{
    $data = file_get_contents('http://youtube.com/get_video_info?video_id=' . $code . '&el=vevo&fmt=18&asv=2&hd=1');

    parse_str($data , $details);
    $my_formats_array = explode(',' , $details['adaptive_fmts']);
    $avail_formats[] = '';
    $i = 0;
    $ipbits = $ip = $itag = $sig = $quality_label = '';
    $expire = time();

    foreach ($my_formats_array as $format) {
         parse_str($format);
         $avail_formats[$i]['itag'] = $itag;
         $avail_formats[$i]['quality'] = $quality_label;
         $type = explode(';', $type);
         $avail_formats[$i]['type'] = $type[0];
         $avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
         parse_str(urldecode($url));
         $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
         $avail_formats[$i]['ipbits'] = $ipbits;
         $avail_formats[$i]['ip'] = $ip;
         $i++;
     }
     var_dump($avail_formats);
}
get_youtube('kObNpTFPV5c');
点赞