gpt4 book ai didi

php - 带有 php 的 youtube 视频下载器

转载 作者:行者123 更新时间:2023-12-03 05:18:09 25 4
gpt4 key购买 nike

我正在尝试用 php 制作一个 youtube 视频下载器,它工作正常,但是如果 youtube 视频 id 有一个像 - 或 _ 这样的字符,它显示错误 undefined index url,似乎 php 没有看到这个字符 e ,g 如果 youtube 视频 ID 是 N65RvNkZFGE 它会工作但是如果它像 wXhTHyIgQ_U 它不会工作因为下划线这里是代码:

// Check whether the url is valid 
if(!empty($youtubeURL) && !filter_var($youtubeURL, FILTER_VALIDATE_URL) === false){
// Get the downloader object
$downloader = $handler->getDownloader($youtubeURL);

// Set the url
$downloader->setUrl($youtubeURL);

// Validate the youtube video url
if($downloader->hasVideo()){
// Get the video download link info
$videoDownloadLink = $downloader->getVideoDownloadLink();

$videoTitle = $videoDownloadLink[0]['title'];
$videoQuality = $videoDownloadLink[0]['qualityLabel'];
$videoFormat = $videoDownloadLink[0]['format'];
$videoFileName = strtolower(str_replace(' ', '_', $videoTitle)).'.'.$videoFormat;
$downloadURL = $videoDownloadLink[0]['url'];
$fileName = preg_replace('/[^A-Za-z0-9.\_\-]/', '', basename($videoFileName));

包含 setUrl() 函数的 YouTubeDownloader.class.php 文件,获取 extractVideoId() 的代码如下:

     public function setUrl($url){ 
$this->video_url = $url;
}

private function extractVideoId($video_url){
//parse the url
$parsed_url = parse_url($video_url);
if($parsed_url["path"] == "youtube.com/watch"){
$this->video_url = "https://www.".$video_url;
}elseif($parsed_url["path"] == "www.youtube.com/watch"){
$this->video_url = "https://".$video_url;
}

if(isset($parsed_url["query"])){
$query_string = $parsed_url["query"];
//parse the string separated by '&' to array
parse_str($query_string, $query_arr);
if(isset($query_arr["v"])){
return $query_arr["v"];
}
}
}

最佳答案

与“_”或“-”无关。 “n85KukOXc0A”也不起作用。有些视频不返回“url”字段,而是返回“cipher”。这是 YouTube 试图混淆 URL。

视频 ID“wXhTHyIgQ_U”的 $videoDownloadLink 看起来像:

array (
0 =>
array (
'itag' => 18,
'bitrate' => 568627,
'width' => 640,
'height' => 360,
'lastModified' => '1575010363774854',
'contentLength' => '16085704',
'quality' => 'medium',
'qualityLabel' => '360p',
'projectionType' => 'RECTANGULAR',
'averageBitrate' => 568472,
'audioQuality' => 'AUDIO_QUALITY_LOW',
'approxDurationMs' => '226371',
'audioSampleRate' => '44100',
'audioChannels' => 2,
'cipher' => 's=__L8kZ2zTIc_OfmovvG91jyFU3WN4QTERuPCxA7rHfbHICEhCrCQkmqPth6pmfw5wmrIPOT_ijWceGCWdCeK-lVYXgIARwMGkhKDv&url=https%3A%2F%2Fr4---sn-hpa7kn7s.googlevideo.com%2Fvideoplayback%3Fexpire%3D1583898090%26ei%3DigloXtGYD4bngAeu8YXQCg%26ip%3D2a00%253Aee2%253A1200%253Ae400%253A8c11%253A6897%253A2e00%253Abef0%26id%3Do-AAcaOp-0syooPWmAUuzOfm6gHGPWYCiDlfa-RNdIP34W%26itag%3D18%26source%3Dyoutube%26requiressl%3Dyes%26mm%3D31%252C26%26mn%3Dsn-hpa7kn7s%252Csn-nv47lnly%26ms%3Dau%252Conr%26mv%3Dm%26mvi%3D3%26pl%3D32%26gcr%3Dsi%26initcwndbps%3D1023750%26vprv%3D1%26mime%3Dvideo%252Fmp4%26gir%3Dyes%26clen%3D16085704%26ratebypass%3Dyes%26dur%3D226.371%26lmt%3D1575010363774854%26mt%3D1583876448%26fvip%3D4%26fexp%3D23842630%26c%3DWEB%26txp%3D5531432%26sparams%3Dexpire%252Cei%252Cip%252Cid%252Citag%252Csource%252Crequiressl%252Cgcr%252Cvprv%252Cmime%252Cgir%252Cclen%252Cratebypass%252Cdur%252Clmt%26lsparams%3Dmm%252Cmn%252Cms%252Cmv%252Cmvi%252Cpl%252Cinitcwndbps%26lsig%3DABSNjpQwRQIgBvV2KI0zNTv-7PsmdoRnpyNBvxeMRJIHSlKjfScxihcCIQDlHa5A-1cGAVReyssZ4YkH2nV2rdN1fel6_-Bkv7CAjA%253D%253D&sp=sig',
'title' => 'Post Malone - Circles',
'mime' => 'video/mp4',
'format' => 'mp4',
),
)

如您所见,没有“url”字段,但是有一个“cipher”字段。
如果我们用 parse_str($videoDownloadLink[0]['cipher'], $cipher) 解码它,我们得到:

array (
's' => '__L8kZ2zTIc_OfmovvG91jyFU3WN4QTERuPCxA7rHfbHICEhCrCQkmqPth6pmfw5wmrIPOT_ijWceGCWdCeK-lVYXgIARwMGkhKDv',
'url' => 'https://r4---sn-hpa7kn7s.googlevideo.com/videoplayback?expire=1583898090&ei=igloXtGYD4bngAeu8YXQCg&ip=2a00%3Aee2%3A1200%3Ae400%3A8c11%3A6897%3A2e00%3Abef0&id=o-AAcaOp-0syooPWmAUuzOfm6gHGPWYCiDlfa-RNdIP34W&itag=18&source=youtube&requiressl=yes&mm=31%2C26&mn=sn-hpa7kn7s%2Csn-nv47lnly&ms=au%2Conr&mv=m&mvi=3&pl=32&gcr=si&initcwndbps=1023750&vprv=1&mime=video%2Fmp4&gir=yes&clen=16085704&ratebypass=yes&dur=226.371&lmt=1575010363774854&mt=1583876448&fvip=4&fexp=23842630&c=WEB&txp=5531432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=ABSNjpQwRQIgBvV2KI0zNTv-7PsmdoRnpyNBvxeMRJIHSlKjfScxihcCIQDlHa5A-1cGAVReyssZ4YkH2nV2rdN1fel6_-Bkv7CAjA%3D%3D',
'sp' => 'sig',
)

您需要正确打乱“s”字段值并将其作为以“sp”字段值命名的字段添加到URL。
它需要加扰的方式会定期更改。 https://www.youtube.com/yts/jsbin/player_ias-vfle4a9aa/en_US/base.js 中的当前方式是:

var Ps = 函数(a) { a = a.split(""); Os.Dw(a, 1); Os.hZ(a, 21); Os.An(a, 24); Os.hZ(a, 34); Os.hZ(a, 18); Os.hZ(a, 63); 返回 a.join("") };

var Os = {
Dw: function(a, b) {
a.splice(0, b)
},
An: function(a) {
a.reverse()
},
hZ: function(a, b) {
var c = a[0];
a[0] = a[b % a.length];
a[b % a.length] = c
}
};

转换为 PHP 为:

function scramble($a) {
$a = str_split($a);
scr_splice($a, 1);
scr_swap($a, 21);
scr_reverse($a, 24);
scr_swap($a, 34);
scr_swap($a, 18);
scr_swap($a, 63);
return implode('', $a);
}

function scr_reverse(&$a) {
$a = array_reverse($a);
}
function scr_splice(&$a, $b) {
array_splice($a, 0, $b);
}
function scr_swap(&$a, $b) {
$c = $a[0];
$a[0] = $a[$b % count($a)];
$a[$b % count($a)] = $c;
}

在您的代码中,您需要检查您获得的 URL 类型并获取正确的 URL。

if (isset($videoDownloadLink[0]['url'])) {
$downloadURL = $videoDownloadLink[0]['url'];
}
else if (isset($videoDownloadLink[0]['cipher'])) {
parse_str($videoDownloadLink[0]['cipher'], $cipher);
$downloadURL = $cipher['url']."&".$cipher["sp"]."=".scramble($cipher["s"]);
}
else {
die('Error getting YouTube URL!');
}

注意:
这只会在 YouTube 再次更改其加扰方式之前有效。

关于php - 带有 php 的 youtube 视频下载器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60607291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com