用php动态下载Youtube视频

时间:2014-12-21 08:29:34

标签: php youtube

  

我使用下面的代码在php中下载youtube视频,它正常工作   我们分配静态视频ID但是当我分配动态ID时很好   $_REQUEST['id']它给我的错误就像

Notice: Undefined variable: my_formats_array in G:\wamp\www\YtubeAPI\google-api-php-client-master\download.php on line 18

ytube_video.php

<?php
 // The YouTube ID
 $key = isset($_REQUEST['id']) ? $_REQUEST['id'] : "juZN67BA_5w";

 // Get all info for video
 $output = file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$key);
 // Parse data to eg.(&id=var)
 parse_str($output);
 // Get Quality map of video an set array

if(isset($url_encoded_fmt_stream_map)) {
 $my_formats_array = explode(',',$url_encoded_fmt_stream_map);
} 
 // Set Array & Vars
 $avail_formats[] = '';
 $i = 0;
 // Break up array to create download links to quality
 foreach($my_formats_array as $format) {
 parse_str($format);
echo "<pre>";
print_r($format);
echo "</pre>";
echo "<a download='". $title .".mp4' href=".$avail_formats[$i]['url'] = urldecode($url).">". $title .".mp4</a> Quality - ".$quality."<br>"; $i++; 
} 
?>

1 个答案:

答案 0 :(得分:3)

  

我得到了答案,这是动态工作的新更新代码

<?php
 // The YouTube ID
 $key = isset($_REQUEST['id']) ? $_REQUEST['id'] : "juZN67BA_5w";
 // Get all info for video
 $output = file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$key);
 // Parse data to eg.(&id=var)
 parse_str($output);
 echo '<p><img src="'. $thumbnail_url .'" border="0" hspace="2" vspace="2"></p>';
 $my_title = $title;

 if(isset($url_encoded_fmt_stream_map)) {
    /* Now get the url_encoded_fmt_stream_map, and explode on comma */
    $my_formats_array = explode(',',$url_encoded_fmt_stream_map);
 } else {
    echo '<p>No encoded format stream found.</p>';
    echo '<p>Here is what we got from YouTube:</p>';
    echo $output;
 }

 if (count($my_formats_array) == 0) {
    echo '<p>No format stream map found - was the video id correct?</p>';
    exit;
 }

/* create an array of available download formats */
 $avail_formats[] = '';
 $i = 0;
 $ipbits = $ip = $itag = $sig = $quality = '';
 $expire = time(); 

 foreach($my_formats_array as $format) {
    parse_str($format);
    $avail_formats[$i]['itag'] = $itag;
    $avail_formats[$i]['quality'] = $quality;
    $type = explode(';',$type);
    $avail_formats[$i]['type'] = $type[0];
    $avail_formats[$i]['url'] = urldecode($url) . '&signature=' .$sig;
    parse_str(urldecode($url));
    $avail_formats[$i]['expire'] = date("G:i:s T", $expire);
    $avail_formats[$i]['ipbits'] = $ipbits;
    $avail_formats[$i]['ip'] = $ip;
    $i++;
 }
 for ($i = 0; $i < count($avail_formats); $i++) {
    echo '<li>' .
            '<span class="itag">' . $avail_formats[$i]['itag'] . '</span> '.
            '<a href="' . $avail_formats[$i]['url'] . '" class="mime">' . $avail_formats[$i]['type'] . '</a> ' .
            '<small>(' .  $avail_formats[$i]['quality'].'</a>' .
            ')</small>
          </li>';
 }
?>