php 如何从 Google Drive 视频中获取 redirector.googlevideo.com 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42115189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get redirector.googlevideo.com link from a Google drive video
提问by chenophucu
I have a google drive video file (like https://drive.google.com/file/d/FILE_ID/view) and I want to get its redirector.googlevideo.com link.
我有一个谷歌驱动器视频文件(如https://drive.google.com/file/d/FILE_ID/view),我想获得它的 redirector.googlevideo.com 链接。
How do sites like http://api.getlinkdrive.com/do it? I've tried using the Google Drive REST API, (both v2 and v3) but still can't find a way to do it. Many tv-show and movie sites host their content on google drive, and use this "cloaked" URL that expires so you can't for example embed it somewhere else.
像http://api.getlinkdrive.com/这样的网站是如何做到的?我尝试过使用 Google Drive REST API(v2 和 v3),但仍然找不到方法。许多电视节目和电影网站将它们的内容托管在谷歌驱动器上,并使用这个过期的“隐藏”URL,因此您无法将其嵌入其他地方。
The closest I've gotten is by going to docs.google.com/get_video_info?docid=FILE_ID and getting the fmt_stream_map links, but that doesn't return the redirector link which I need.
我得到的最接近的是转到 docs.google.com/get_video_info?docid=FILE_ID 并获取 fmt_stream_map 链接,但这不会返回我需要的重定向器链接。
回答by Lin
Php code will not work here and even meaningless, because it will return the result for server side.
Php 代码在这里不起作用甚至毫无意义,因为它将返回服务器端的结果。
This is my Javascript/Ajax code. Unfortunately, you should use Access-Control-Allow-Origin extensionin Chrome browser. First, please see this screenshotof console window.
这是我的 Javascript/Ajax 代码。不幸的是,您应该在 Chrome 浏览器中使用Access-Control-Allow-Origin 扩展。首先,请查看控制台窗口的屏幕截图。
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://content.jwplatform.com/libraries/YOUR-JW-PLAYER-LICENSE-KEY.js"></script>
</head>
<body>
<p id="urls"></p>
<div id="myElement"></div>
</body>
<script>
var docid = "0B4Mn0g4wWmQ1ZThycVhOcDJQc2c";
var api_url = "https://docs.google.com/get_video_info";
var ans = {};
var url = "";
getGoogleVideoUrl();
function getGoogleVideoUrl(){
$.ajax({
url: api_url,
type: "get",
data: {
docid: docid,
},
success: function(response) {
//
// get value of which key is 'fmt_stream_map'.
//
var fmt_stream_map = getQueryVariable(response, 'fmt_stream_map');
//
// split my comma
//
var maps = fmt_stream_map.split("%2C");
//
// loop all links,
//
var purl = "";
for (x in maps) {
var res = decodeURIComponent(decodeURIComponent(maps[x])).split('|');
// purl = res[1].replace(new RegExp("/\/[^\/]+\.google\.com/", 'g'),"//redirector.googlevideo.com/");
// purl = res[1].replace(new RegExp("\.google\.com/", 'g'),".googlevideo.com/");
purl = res[1];
//.replace(/.c.docs.google.com/g,".googlevideo.com")
//.replace(/app=explorer/g,"app=storage")
//.replace(/key=ck2/g,"key=cms1")
//.replace(/&cp=/g,"&api=")
//.replace(/,cp&/g,',api&')
//.replace(/,cp,/g,',api,')
//.replace(/=cp,/g,'=api,')
//.replace(/&sparams=/g,'&cms_redirect=yes&sparams=');
switch (parseInt(res[0])) {
case 5:
quality = 'Low Quality, 240p, FLV, 400x240';
break;
case 17:
quality = 'Low Quality, 144p, 3GP, 0x0';
break;
case 18:
quality = 'Medium Quality, 360p, MP4, 480x360';
break;
case 22:
quality = 'High Quality, 720p, MP4, 1280x720';
break;
case 34:
quality = 'Medium Quality, 360p, FLV, 640x360';
break;
case 35:
quality = 'Standard Definition, 480p, FLV, 854x480';
break;
case 36:
quality = 'Low Quality, 240p, 3GP, 0x0';
break;
case 37:
quality = 'Full High Quality, 1080p, MP4, 1920x1080';
break;
case 38:
quality = 'Original Definition, MP4, 4096x3072';
break;
case 43:
quality = 'Medium Quality, 360p, WebM, 640x360';
break;
case 44:
quality = 'Standard Definition, 480p, WebM, 854x480';
break;
case 45:
quality = 'High Quality, 720p, WebM, 1280x720';
break;
case 46:
quality = 'Full High Quality, 1080p, WebM, 1280x720';
break;
case 82:
quality = 'Medium Quality 3D, 360p, MP4, 640x360';
break;
case 84:
quality = 'High Quality 3D, 720p, MP4, 1280x720';
break;
case 102:
quality = 'Medium Quality 3D, 360p, WebM, 640x360';
break;
case 104:
quality = 'High Quality 3D, 720p, WebM, 1280x720';
break;
default:
quality = 'transcoded (unknown) quality';
break;
}
ans[quality] = purl;
}
console.log(ans);
$('#urls').html(JSON.stringify(ans));
url = ans[Object.keys(ans)[0]];
build_player();
},
error: function(xhr) {
//Do Something to handle error
}
});
}
function build_player(){
var playerInstance = jwplayer("myElement");
playerInstance.setup({
file: url,
type: "mp4",
width: 800 ,
height: 600,
});
}
function getQueryVariable(query, variable) {
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
//return decodeURIComponent(pair[1]);
return pair[1];
}
}
console.log('Query variable %s not found', variable);
return "";
}
</script>
</html>
This code works basically, but doesn't play in jwplayer component.
这段代码基本上可以工作,但不能在 jwplayer 组件中播放。
However, if you enter this URL, "https://docs.google.com/get_video_info?docid=0B4Mn0g4wWmQ1ZThycVhOcDJQc2c", directly in chrome browser, get JSON file , parse it manually, use one of URLs directly in this code as jwplayer's file url, it works perfectly, although it contains "app=explorer" tag in itself.
但是,如果你输入这个网址,“ https://docs.google.com/get_video_info?docid=0B4Mn0g4wWmQ1ZThycVhOcDJQc2c”,直接在chrome浏览器中,获取JSON文件,手动解析,直接在这段代码中使用其中一个网址作为jwplayer的文件url,虽然它本身包含“app=explorer”标签,但它工作得很好。
I'm not sure why this happens. So I tried to compare 2 links and some parameters is different. The first one is from above code and not working, the 2nd JSON file directly and working.)
我不确定为什么会发生这种情况。所以我试图比较 2 个链接,有些参数是不同的。第一个来自上面的代码并且不工作,第二个 JSON 文件直接和工作。)
ei=hJ5yWaHCKYXb-wWona2YBA
ei=KZ5yWZSkK4aFqgXAwpoo
susci=o-AC34EOoA1Wst0Heh0U_bP9epqR8K9s4UBhwlqmsxKZKwAOA
susci=o-AH82qbGL8BcWQ3BPybbvZyuNBiDd2Uasz4J0ZNXJCZwobPje
expire=1500698308
expire=1500698217
cp=QVNFUkdfV1NOSVhOOnhwOWFybUloWXNX
cp=QVNFUkdfV1JPSFhOOmpURGRUeUt3eVpv
signature=3D306FD9D9ADA683D313AABDFE057B608A6F2A39.8BB3A9C321B6BEAC8D1D5AEED2F25511DF97CE2B
signature=2C2465BDFC4D9CCFD0D4A42F38BAEF44D55AFDF1.A916937113445ABB90D18B3AE89600729CFADDE6
Why first one is not working while 2nd is working? Any idea of this?
为什么第一个不工作而第二个工作?有什么想法吗?
回答by Football Live Streamings
He is talking about getting the URL from google drive all qualities in 360p,480p,720p,1080p and convert just before the /videoplayback to redirector.googlevideo.com and make it as video/mp4 to do playback in JW Player.I have the full script but it has got a minor problem it says forbidden 403 and it is because of changes google made in API.Have a look at the codes in PHP and let me know if someone can fix it,i will post full script later.
他正在谈论从 google drive 获取 360p、480p、720p、1080p 中的所有质量的 URL,并在 /videoplayback 之前转换为 redirector.googlevideo.com 并将其作为视频/mp4 在 JW Player 中播放。我有完整的脚本,但它有一个小问题,它说禁止 403,这是因为谷歌在 API 中所做的更改。看看 PHP 中的代码,让我知道是否有人可以修复它,我稍后会发布完整的脚本。
function Drive($link) {
$url = urldecode($link);
$get = curl1($url);
$data = explode(',["fmt_stream_map","', $get);
$data = explode('"]', $data[1]);
$data = str_replace(array('\u003d', '\u0026'), array('=', '&'), $data[0]);
$data = explode(',', $data);
asort($data);
foreach($data as $list) {
$data2 = explode('|', $list);
if($data2[0] == 37) {$q1080p = preg_replace("/\/[^\/]+\.google\.com/","/redirector.googlevideo.com",$data2[1]);} // 1080P
if($data2[0] == 22) {$q720p = preg_replace("/\/[^\/]+\.google\.com/","/redirector.googlevideo.com",$data2[1]);} // 720P
if($data2[0] == 59) {$q480p = preg_replace("/\/[^\/]+\.google\.com/","/redirector.googlevideo.com",$data2[1]);} // 480P
if($data2[0] == 18) {$q360p = preg_replace("/\/[^\/]+\.google\.com/","/redirector.googlevideo.com",$data2[1]);} // 360P
}
$js[0][0] = "$q1080p";
$js[0][1] = "$q720p";
$js[0][2] = "$q480p";
$js[0][3] = "$q360p";
$js[1][0] = "1080P";
$js[1][1] = "720P";
$js[1][2] = "480P";
$js[1][3] = "360P";
return $js;
}
if ($jw[0][0] != "") {
echo('{file: "'.urldecode($jw[0][0]).'",type: "video/mp4",label: "'.urldecode($jw[1][0]).'"},');
}
if ($jw[0][1] != "") {
echo('{file: "'.urldecode($jw[0][1]).'",type: "video/mp4",label: "'.urldecode($jw[1][1]).'"},');
}
if ($jw[0][2] != "") {
echo('{file: "'.urldecode($jw[0][2]).'",type: "video/mp4",label: "'.urldecode($jw[1][2]).'"},');
}
if ($jw[0][3] != "") {
echo('{file: "'.urldecode($jw[0][3]).'",type: "video/mp4",label: "'.urldecode($jw[1][3]).'"},');
}
回答by 5DemandsNot1Less
I implemented a better algorithm from the previous answer.
我从上一个答案中实现了一个更好的算法。
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, $return);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function Drive($id) {
$o = [];
$url = "https://docs.google.com/get_video_info?docid=$id";
$get = curl($url);
parse_str($get, $out);
$data = explode(",", $out["fmt_stream_map"]);
foreach($data as $d) {
switch ((int)substr($d, 0, 2)) {
case 18:
$r = "360P";
break;
case 22:
$r = "720P";
break;
case 37:
$r = "1080P";
break;
case 59:
$r = "480P";
break;
default:
break;
}
$o[$r] = substr(preg_replace("/\/[^\/]+\.google\.com/","/redirector.googlevideo.com", $d), 3);
}
ksort($o);
return $o;
}
$jw = Drive(FILE_ID_HERE);
foreach ($jw as $k => $r) {
echo json_encode(array("file"=> $r, "type"=> "video/mp4", "label"=> $k))."\n";
}