php - php 中的 Youtube 视频下载器失败 - 禁止

标签 php video youtube youtube-data-api

我来自孟加拉国。我有一个 youtube 下载器 php 脚本,但它无法下载一些视频。当我单击下载按钮时,它的下载视频播放并向我显示文本失败 - 禁止。

我无法下载与以下视频相同的内容。大部分视频没有下载。

下载网址为:http://mcv12.masudtools.xyz/

比如这个视频:

https://www.youtube.com/watch?v=DL5-XssXkxo



我也有错误日志:

[13-Jul-2017 12:51:44 UTC] PHP Notice: Undefined index: adaptive_fmts in /home/masudtoo/public_html/mcv12/VideoYoutubeProvider.php on line 126 [13-Jul-2017 12:51:45 UTC] PHP Notice: Undefined index: type in /home/masudtoo/public_html/mcv12/VideoYoutubeProvider.php on line 85 [13-Jul-2017 12:51:45 UTC] PHP Notice: Undefined offset: 1 in /home/masudtoo/public_html/mcv12/VideoYoutubeProvider.php on line 88



126行是:
$adaptive_fmts = $this->fullInfo['adaptive_fmts'];

85行是:
$type = explode(';', $item['type']);

88行是:
$ext = $baseType[1];

完整的 VideoYoutubeProvider.php 代码:
class VideoYoutubeProvider extends VideoAbstractProvider
{

public $fullInfo = null;

/**
 * Get provider name
 * @return string
 */
public function getProviderName()
{
    return "Youtube video";
}

/**
 * Checks video url for belonging to this provider
 *
 * @param string $url video url for check
 * @return boolean returning true if service available this video url
 */
static public function checkUrl($url)
{
    return !!preg_match("/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/i", $url);
}

/**
 * Return base video information
 *
 * @return BaseVideoInfo
 * @throws VideoDownloaderBaseInfoException
 */
public function getBaseInfo()
{
    $fullInfo = $this->getFullInfo();
    $baseInfo = new BaseVideoInfo();
    $baseInfo->name = $fullInfo['title'];
    $videoId = $fullInfo['video_id'];
    $preview = new VideoPreviewUrl();
    $preview->width = 1280;
    $preview->height = 720;
    $preview->name = 'maxresdefault';
    $preview->url = "https://img.youtube.com/vi/$videoId/hqdefault.jpg";
    $baseInfo->previewUrls = [$preview];
    $baseInfo->mainPreviewUrl = $preview;
    $html = $fullInfo['html'];
    if (preg_match('/<p id="eow-description"[^>]+>(.+)<\/p>/', $html, $matches)) {
        $baseInfo->description = strip_tags(str_replace('<br />', "\n", $matches[1]));
    }
    return $baseInfo;
}

/**
 * Return array of download information for video
 *
 * @return VideoDownloadInfo[]
 * @throws VideoDownloaderDownloadException
 */
public function getDownloadsInfo()
{
    $fullInfo = $this->getFullInfo();
    $fmts = $fullInfo['url_encoded_fmt_stream_map'];
    $downloadsInfo = [];
    $title = $fullInfo['title'];
    foreach ($fmts AS $item) {
        $url = $item['url'] . '&title=' . urlencode($title);
        $headers = RequestHelper::getResponseHeaders($url);
        $downloadInfo = new VideoDownloadInfo();
        $downloadInfo->fileSize = (int)$headers['Content-Length'];
        $downloadInfo->url = $url;
        $downloadInfo->fileType = $headers['Content-Type'];
        $ext = explode('/', $downloadInfo->fileType);
        $ext = $ext[1];
        $downloadInfo->name = $title . '.' . $ext;
        $downloadsInfo[] = $downloadInfo;
    }
    $fmts = $fullInfo['adaptive_fmts'];
    foreach ($fmts AS $item) {
        $type = explode(';', $item['type']);
        $type = $type[0];
        $baseType = explode('/', $type);
        $ext = $baseType[1];
        $baseType = $baseType[0];
        if ($baseType === 'audio') {
            $downloadInfo = new VideoDownloadInfo();
            $downloadInfo->fileSize = (int)$item['clen'];
            $downloadInfo->name = $title . '.' . $ext;
            $url = $item['url'] . '&title=' . urlencode($title);
            $downloadInfo->url = $url;
            $downloadInfo->fileType = $type;
            $downloadsInfo[] = $downloadInfo;
            break;
        }
    }
    return $downloadsInfo;
}


private function getFullInfo()
{
    if (is_null($this->fullInfo)) { 
        preg_match("/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/i", $this->url, $matches);
        $videoId = $matches[7];
        $fullInfoSource = file_get_contents("http://www.youtube.com/get_video_info?&video_id=$videoId&asv=3&el=detailpage&hl=en_US");
        $fullInfoSource = explode('&', $fullInfoSource);
        $this->fullInfo = [];
        foreach ($fullInfoSource AS $item) {
            $item = explode('=', $item);
            $this->fullInfo[$item[0]] = urldecode($item[1]);
        }
        $url_encoded_fmt_stream_map = $this->fullInfo['url_encoded_fmt_stream_map'];
        $url_encoded_fmt_stream_map = explode(',', $url_encoded_fmt_stream_map);
        $this->fullInfo['url_encoded_fmt_stream_map'] = [];
        foreach ($url_encoded_fmt_stream_map AS $downloadItem) {
            $downloadInfo = [];
            parse_str($downloadItem, $downloadInfo);
            $this->fullInfo['url_encoded_fmt_stream_map'][] = $downloadInfo;
        }

        $adaptive_fmts = $this->fullInfo['adaptive_fmts'];
        $adaptive_fmts = explode(',', $adaptive_fmts);
        $this->fullInfo['adaptive_fmts'] = [];
        foreach ($adaptive_fmts AS $item) {
            $itemInfo = [];
            parse_str($item, $itemInfo);
            $this->fullInfo['adaptive_fmts'][] = $itemInfo;
        }

        $this->fullInfo['html'] = file_get_contents("https://www.youtube.com/watch?v=$videoId");
        $this->fullInfo['video_id'] = $videoId;
    }
    return $this->fullInfo;
}
}

请帮我解决它。谢谢

最佳答案

试试这个只从 youtube 视频 ID 获取 Youtube 视频源

    // Get Youtube Source
function getYoutubeVideoSource1($id = 'jNQXAC9IVRw'){
    parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=".$id),$info);
    $streams = $info['url_encoded_fmt_stream_map'];
    $streams = explode(',',$streams);
    //dd($streams);
    // store value in array
    $video_stream = array();
    foreach ($streams as $stream){
        parse_str($stream,$data);
        $data = array(
            'type' => $data['type'],
            'quality' => $data['quality'],
            'url' => $data['url'],
        );
        $video_stream[] = (object) $data;
    }
    print_r($video_stream);

    //return $video_stream;
}

在这里,您将获得一系列视频流。
   array:4 [▼
  0 => {#216 ▼
    +"type": "video/webm; codecs="vp8.0, vorbis""
    +"quality": "medium"
    +"url": "https://r3---sn-ugp5obax-q5je.googlevideo.com/videoplayback?ipbits=0&ei=qC_9Wa3HD9rcogOXgZuAAw&lmt=1380226535526270&expire=1509786632&sparams=clen%2Cdur%2Cei%2C ▶"
  }
  1 => {#211 ▶}
  2 => {#212 ▶}
  3 => {#218 ▶}
]

现在你可以得到这样的视频链接
// Get Youtube Batch Download link
function youtubeBatchDownloadLink($videoId, $format){
    $video_source = getYoutubeVideoSource ($videoId);
    $url = $video_source[$format]->url;
    print_r($url);
    //return $url;
}

希望能帮到你

关于php - php 中的 Youtube 视频下载器失败 - 禁止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45081916/

相关文章:

php - 未指定应用程序加密 key

.net - Adobe Flash Media Encoder 和 WPF MediaElement

python - 如何添加功能以显示视频格式以及用户选择的下载分辨率

php - 使用 php mysql 和 jquery 显示数据库动态内容

来自平面 JSON 的 PHP 嵌套 JSON

php - 查询以获取同一表中的 2 列

video - 无法在 WhatsApp 上分享一些 FFMPEG 编码的视频

file - 如何为 MP4 文件创建缩略图系统

javascript - YouTube 视频嵌入循环

c# - 如何在Windows Store C#上加载FEEDS youtube apis v3