PHP下载YouTube视频

7
我使用下面的脚本来下载YouTube视频,在本地主机中的XAMPP中运行良好,但是当我将其放在我的生产WEB服务器上时无法工作,我从https://github.com/jeckman/YouTube-Downloader/blob/master/getvideo.php获取了这段代码。
  <?php
    // YouTube Downloader PHP
    // based on youtube-dl in Python http://rg3.github.com/youtube-dl/
    // by Ricardo Garcia Gonzalez and others (details at url above)
    //
    // Takes a VideoID and outputs a list of formats in which the video can be
    // downloaded

    include_once('curl.php');

    if(isset($_REQUEST['videoid'])) {
        $my_id = $_REQUEST['videoid'];
    } else {
        echo '<p>No video id passed in</p>';
        exit;
    }

    if(isset($_REQUEST['type'])) {
        $my_type =  $_REQUEST['type'];
    } else {
        $my_type = 'redirect';
    }

    if(isset($_REQUEST['debug'])) {
        $debug = TRUE;
    } else {
        $debug = FALSE;
    }

    if ($my_type == 'Download') {
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Youtube Downloader</title>
        <meta name="keywords" content="Video downloader, download youtube, video download, youtube video, youtube downloader, download youtube FLV, download youtube MP4, download youtube 3GP, php video downloader" />
        <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
         <style type="text/css">
          body {
            padding-top: 40px;
            padding-bottom: 40px;
            background-color: #f5f5f5;
        }

          .download {
            max-width: 300px;
            padding: 19px 29px 29px;
            margin: 0 auto 20px;
            background-color: #fff;
            border: 1px solid #e5e5e5;
            -webkit-border-radius: 5px;
               -moz-border-radius: 5px;
                    border-radius: 5px;
            -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
               -moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
                    box-shadow: 0 1px 2px rgba(0,0,0,.05);
          }

          .download .download-heading {
            margin-bottom: 10px;
          }

          .mime, .itag {
            width: 75px;
            display: inline-block;
          }

          .itag {
            width: 15px;
          }

          .userscript {
            float: right;
            margin-top: 5px
          }
        </style>
        </head>
    <body>
        <div class="download">
        <h1 class="download-heading">Youtube Downloader Results</h1>
    <?php
    } // end of if for type=Download

    /* First get the video info page for this video id */
    $my_video_info = 'http://www.youtube.com/get_video_info?&video_id='. $my_id;
    $my_video_info = curlGet($my_video_info);

    /* TODO: Check return from curl for status code */

    parse_str($my_video_info);
    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);
        //if($debug) {
        //  echo '<pre>';
        //  print_r($my_formats_array);
        //  echo '</pre>';
        //}
    } else {
        echo '<p>No encoded format stream found.</p>';
        echo '<p>Here is what we got from YouTube:</p>';
        echo $my_video_info;
    }
    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;

    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]['expires'] = date("G:i:s T", $expire);
        $avail_formats[$i]['ipbits'] = $ipbits;
        $avail_formats[$i]['ip'] = $ip;
        $i++;
    }

    if ($debug) {
        echo '<p>These links will expire at '. $avail_formats[0]['expires'] .'</p>';
        echo '<p>The server was at IP address '. $avail_formats[0]['ip'] .' which is an '. $avail_formats[0]['ipbits'] .' bit IP address. ';
        echo 'Note that when 8 bit IP addresses are used, the download links may fail.</p>';
    }
    if ($my_type == 'Download') {
        echo '<ul>
                List of available formats for download:<br>
                <small>Right-click and choose "save as" or click "download" to use this server as proxy.</small>
            </ul>';

        /* now that we have the array, print the options */
        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 href="download.php?mime=' . $avail_formats[$i]['type'] .'&title='. urlencode($my_title) .'&token=' . base64_encode($avail_formats[$i]['url']) . '" class="dl">download</a>' .
                    ')</small></li>';
        }
        echo '</ul>';
    ?>

    <!-- @TODO: Prepend the base URI -->
    <a href="ytdl.user.js" class="userscript btn btn-mini" title="Install chrome extension to view a 'Download' link to this application on Youtube video pages.">
      Install Chrome Extension
    </a>

    </body>
    </html>

    <?php

    } else {

    /* In this else, the request didn't come from a form but from something else
     * like an RSS feed.
     * As a result, we just want to return the best format, which depends on what
     * the user provided in the url.
     * If they provided "format=best" we just use the largest.
     * If they provided "format=free" we provide the best non-flash version
     * If they provided "format=ipad" we pull the best MP4 version
     *
     * Thanks to the python based youtube-dl for info on the formats
     *                              http://rg3.github.com/youtube-dl/
     */

    $format =  $_REQUEST['format'];
    $target_formats = '';
    switch ($format) {
        case "best":
            /* largest formats first */
            $target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
            break;
        case "free":
            /* Here we include WebM but prefer it over FLV */
            $target_formats = array('38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13');
            break;
        case "ipad":
            /* here we leave out WebM video and FLV - looking for MP4 */
            $target_formats = array('37','22','18','17');
            break;
        default:
            /* If they passed in a number use it */
            if (is_numeric($format)) {
                $target_formats[] = $format;
            } else {
                $target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
            }
        break;
    }

    /* Now we need to find our best format in the list of available formats */
    $best_format = '';
    for ($i=0; $i < count($target_formats); $i++) {
        for ($j=0; $j < count ($avail_formats); $j++) {
            if($target_formats[$i] == $avail_formats[$j]['itag']) {
                //echo '<p>Target format found, it is '. $avail_formats[$j]['itag'] .'</p>';
                $best_format = $j;
                break 2;
            }
        }
    }

    //echo '<p>Out of loop, best_format is '. $best_format .'</p>';
    $redirect_url = $avail_formats[$best_format]['url'];
    $content_type = $avail_formats[$best_format]['type'];
    header("Location: $redirect_url");
    } // end of else for type not being Download
    ?>

2
检查您的主机上是否启用了CURL PHP扩展?同时启用PHP错误以便您可以看到精确的错误消息。 - Jay Bhatt
你给我们的信息太少了,我们无法进行更多的操作。 - user557846
1
@JayBhatt,curl php扩展已启用,我无法看到错误,因为它显示的是index.php的内容,而不是download.php的内容,在本地主机上是正常的。 Dagon,请检查github链接,我已完全安装它,在本地主机上运行正常。 - Kishor datta gupta
所以它正在运行错误的脚本? - Richard Deurwaarder
尝试这个:https://weibomiaopai.com/download-video-parser.php - justyy
1个回答

5
尝试在路由器上打开一个端口并将其重定向到您计算机的本地主机。您的ISP可能会阻止端口80,因此请将广播端口更改为Apache的8080端口。尝试通过Internet从外部访问您的计算机并下载YouTube视频。如果这不会给您带来任何问题,则您的Web主机可能正在阻止它。
我在我的Web主机上尝试了它,它工作得很好,您是否遇到任何错误?

2
解决了,问题是IP,使用代理就可以了。 - Kishor datta gupta
1
@kishordgupta.lilait.com,您如何使用代理?我也遇到了相同的问题。 - abdullacm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接