Son*_*ony 8 php download vimeo
我希望通过PHP脚本直接链接到Vimeo的视频.我设法手动找到它们,但我的PHP脚本不起作用.这是倡议:例如,我拍摄了这个视频:http://vimeo.com/22439234
当您进入页面时,Vimeo会生成与当前时间戳和此视频相关联的签名.此信息存储在一个JavaScript变量中,紧接在第520行之后:
window.addEvent ('domready', function () {
然后,当您单击"播放"时,HTML5播放器将读取此变量并发送HTTP请求:
http:// player.vimeo.com/play_redirect?clip_id=37111719&sig={SIGNATURE}&time={TIMESTAMP}&quality=sd&codecs=H264,VP8,VP6&type=moogaloop_local&embed_location=
Run Code Online (Sandbox Code Playgroud)
但它也适用于:
http:// player.vimeo.com/play_redirect?clip_id=37111719&sig={SIGNATURE}&time={TIMESTAMP}&quality=sd
Run Code Online (Sandbox Code Playgroud)
如果此URL未使用打开的http://vimeo.com/22439234的IP地址打开,则会返回HTTP代码200并显示错误消息.
如果使用正确的IP地址打开此URL,则标题"位置"会重定向以链接到视频文件:
http://av.vimeo.com/XXX/XX/XXXX.mp4?aksessionid=XXXX&token=XXXXX_XXXXXXXXX
当我http://player.vimeo.com/play_redirect?...手动构建此链接时("右键单击">"源代码">"第520行"),它可以正常工作.
但是使用PHP和正则表达式,它会返回HTTP code 200错误消息.
为什么?
从我的观察,Vimeo的不检查HTTP请求的头http:// player.vimeo.com/play_redirect?...
GET,HEAD与饼干,没有cookies,引用等等......不会改变.
使用PHP,我使用函数file_get_contents()和get_headers().
<?php
function getVimeo($id) {
$content = file_get_contents('http://vimeo.com/'.$id);
if (preg_match('#document\.getElementById\(\'player_(.+)\n#i', $content, $scriptBlock) == 0)
return 1;
preg_match('#"timestamp":([0-9]+)#i', $scriptBlock[1], $matches);
$timestamp = $matches[1];
preg_match('#"signature":"([a-z0-9]+)"#i', $scriptBlock[1], $matches);
$signature = $matches[1];
$url = 'http://player.vimeo.com/play_redirect?clip_id='.$id.'&sig='.$signature.'&time='.$timestamp.'&quality=sd';
print_r(get_headers($url, 1));
}
Run Code Online (Sandbox Code Playgroud)
该算法如下所示:
\n\n这是我的简单课程,目前正在使用。
\n\nclass VideoController\n{\n\n /**\n * @var array Vimeo video quality priority\n */\n public $vimeoQualityPrioritet = array(\'sd\', \'hd\', \'mobile\');\n\n /**\n * @var string Vimeo video codec priority\n */\n public $vimeoVideoCodec = \'h264\';\n\n /**\n * Get direct URL to Vimeo video file\n * \n * @param string $url to video on Vimeo\n * @return string file URL\n */\n public function getVimeoDirectUrl($url)\n {\n $result = \'\';\n $videoInfo = $this->getVimeoVideoInfo($url);\n if ($videoInfo && $videoObject = $this->getVimeoQualityVideo($videoInfo->request->files))\n {\n $result = $videoObject->url;\n }\n return $result;\n }\n\n /**\n * Get Vimeo video info\n * \n * @param string $url to video on Vimeo\n * @return \\stdClass|null result\n */\n public function getVimeoVideoInfo($url)\n {\n $videoInfo = null;\n $page = $this->getRemoteContent($url);\n $dom = new \\DOMDocument("1.0", "utf-8");\n libxml_use_internal_errors(true);\n $dom->loadHTML(\'<?xml version="1.0" encoding="UTF-8"?>\' . "\\n" . $page);\n $xPath = new \\DOMXpath($dom);\n $video = $xPath->query(\'//div[@data-config-url]\');\n if ($video)\n {\n $videoObj = json_decode($this->getRemoteContent($video->item(0)->getAttribute(\'data-config-url\')));\n if (!property_exists($videoObj, \'message\'))\n {\n $videoInfo = $videoObj;\n }\n }\n return $videoInfo;\n }\n\n /**\n * Get vimeo video object\n * \n * @param stdClass $files object of Vimeo files\n * @return stdClass Video file object\n */\n public function getVimeoQualityVideo($files)\n {\n $video = null;\n if (!property_exists($files, $this->vimeoVideoCodec) && count($files->codecs))\n {\n $this->vimeoVideoCodec = array_shift($files->codecs);\n }\n $codecFiles = $files->{$this->vimeoVideoCodec};\n foreach ($this->vimeoQualityPrioritet as $quality)\n {\n if (property_exists($codecFiles, $quality))\n {\n $video = $codecFiles->{$quality};\n break;\n }\n }\n if (!$video)\n {\n foreach (get_object_vars($codecFiles) as $file)\n {\n $video = $file;\n break;\n }\n }\n return $video;\n }\n\n /**\n * Get remote content by URL\n * \n * @param string $url remote page URL\n * @return string result content\n */\n public function getRemoteContent($url)\n {\n $ch = curl_init();\n curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);\n curl_setopt($ch, CURLOPT_TIMEOUT, 20);\n curl_setopt($ch, CURLOPT_HEADER, false);\n curl_setopt($ch, CURLOPT_URL, $url);\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);\n curl_setopt($ch, CURLOPT_MAXREDIRS, 10);\n curl_setopt($ch, CURLOPT_USERAGENT, \'spider\');\n $content = curl_exec($ch);\n\n curl_close($ch);\n\n return $content;\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n使用:
\n\n$video = new VideoController;\nvar_dump($video->getVimeoDirectUrl(\'http://vimeo.com/90747156\'));\nRun Code Online (Sandbox Code Playgroud)\n
尝试将有效的用户代理添加到每个请求的标头中。为此,您必须使用 cURL 或 HttpRequest 而不是 file_get_contents()。
经过这样的操作后,我得到了下载视频文件的工作链接。
这是我的代码:
function getVimeo($id) {
// get page with a player
$queryResult = httpQuery('http://vimeo.com/' . $id);
$content = $queryResult['content'];
if (preg_match('#document\.getElementById\(\'player_(.+)\n#i', $content, $scriptBlock) == 0)
return 1;
preg_match('#"timestamp":([0-9]+)#i', $scriptBlock[1], $matches);
$timestamp = $matches[1];
preg_match('#"signature":"([a-z0-9]+)"#i', $scriptBlock[1], $matches);
$signature = $matches[1];
$url = 'http://player.vimeo.com/play_redirect?clip_id=' . $id . '&sig=' . $signature . '&time=' . $timestamp . '&quality=sd';
// make the request for getting a video url
#print_r(get_headers($url, 1));
$finalQuery = httpQuery($url);
return $finalQuery['redirect_url'];
}
// make queries via CURL
function httpQuery($url) {
$options = array(
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML, like Gecko) Ubuntu/12.04 Chromium/18.0.1025.168 Chrome/18.0.1025.168 Safari/535.19',
CURLOPT_RETURNTRANSFER => true,
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$result = $info;
$result['content'] = $content;
return $result;
}
echo getVimeo(22439234);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16302 次 |
| 最近记录: |