如何用PHP或HTML实现外部视频链接的直接下载功能
实现外部视频链接的直接下载功能(HTML/PHP方案)
一、HTML 方案(简单但有局限)
利用 HTML5 的 download 属性可尝试直接触发下载,但仅适用于同源资源或服务器允许跨域下载的情况,对于你提供的 Google Video 这类外部链接,大概率会因浏览器跨域限制无法生效。
代码示例:
<a href="https://rr4---sn-4wg7ln7k.googlevideo.com/videoplayback?expire=1659178416&ei=ULnkYojCCteA2LYP6fy9gAY&ip=18.207.249.128&id=o-AH5lc61_k63NQzB0KfAwR3eKkWwyf6UlhiglXm4yeZ1G&itag=244&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278%2C597%2C598&source=youtube&requiressl=yes&spc=lT-KhmC1OQXH6E1Bwoi7FX0xjBgFfGU&vprv=1&mime=video%2Fwebm&ns=VbQMOPVRVjJo8hxCD1PMQ88H&gir=yes&clen=21400428&dur=460.000&lmt=1659122075115614&keepalive=yes&fexp=24001373,24007246&c=MWEB&txp=7319224&n=vuqpLdTQIjRGZJYI&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cspc%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhAKjNSNNnW-Zi3_qeXCI3N6Stx9M1BoTKyXMlU5_xlZMHAiEA_tGiCCCcd37kVTJHmE7AxiJHlSw05UJY_MFHBcNEKpE%3D&rm=sn-p5qe7r7e,sn-uxaxovg23-aixe7l&req_id=e29350f8131ca3ee&ipbypass=yes&redirect_counter=2&cms_redirect=yes&cmsv=e&mh=iR&mip=103.7.79.121&mm=29&mn=sn-4wg7ln7k&ms=rdu&mt=1659156550&mv=m&mvi=4&pl=24&lsparams=ipbypass,mh,mip,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRgIhAJWZ4pMz_cCq37cAARiY6120pKwzb2lMPiKEQa4A-lqXAiEAz-wXlSB2hoxwLu-V3nPL69zGhF0cJ9CxDvMLEuUUvrQ%3D" download="my-video.webm">点击下载视频</a>
注意事项:
- 若目标服务器设置了
Content-Disposition: inline或限制跨域访问,浏览器仍会直接打开视频而非触发下载。 - 你提供的 Google Video 链接带有
expire参数,属于临时有效链接,过期后会失效。
二、PHP 代理下载方案(稳定可靠)
通过 PHP 作为中间层代理请求远程视频,再输出给用户触发下载,能规避跨域限制,是更通用的解决方案。
基础实现代码:
<?php // 目标视频链接 $videoUrl = 'https://rr4---sn-4wg7ln7k.googlevideo.com/videoplayback?expire=1659178416&ei=ULnkYojCCteA2LYP6fy9gAY&ip=18.207.249.128&id=o-AH5lc61_k63NQzB0KfAwR3eKkWwyf6UlhiglXm4yeZ1G&itag=244&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278%2C597%2C598&source=youtube&requiressl=yes&spc=lT-KhmC1OQXH6E1Bwoi7FX0xjBgFfGU&vprv=1&mime=video%2Fwebm&ns=VbQMOPVRVjJo8hxCD1PMQ88H&gir=yes&clen=21400428&dur=460.000&lmt=1659122075115614&keepalive=yes&fexp=24001373,24007246&c=MWEB&txp=7319224&n=vuqpLdTQIjRGZJYI&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cspc%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhAKjNSNNnW-Zi3_qeXCI3N6Stx9M1BoTKyXMlU5_xlZMHAiEA_tGiCCCcd37kVTJHmE7AxiJHlSw05UJY_MFHBcNEKpE%3D&rm=sn-p5qe7r7e,sn-uxaxovg23-aixe7l&req_id=e29350f8131ca3ee&ipbypass=yes&redirect_counter=2&cms_redirect=yes&cmsv=e&mh=iR&mip=103.7.79.121&mm=29&mn=sn-4wg7ln7k&ms=rdu&mt=1659156550&mv=m&mvi=4&pl=24&lsparams=ipbypass,mh,mip,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRgIhAJWZ4pMz_cCq37cAARiY6120pKwzb2lMPiKEQa4A-lqXAiEAz-wXlSB2hoxwLu-V3nPL69zGhF0cJ9CxDvMLEuUUvrQ%3D'; // 验证链接有效性 $headers = get_headers($videoUrl, true); if (!$headers || strpos($headers[0], '200') === false) { die('视频链接无效或已过期'); } // 设置下载响应头 $mimeType = $headers['Content-Type'] ?? 'video/webm'; $fileName = 'downloaded-video.webm'; header('Content-Type: ' . $mimeType); header('Content-Disposition: attachment; filename="' . $fileName . '"'); header('Content-Length: ' . $headers['Content-Length']); // 输出视频内容 readfile($videoUrl); exit; ?>
进阶优化(兼容禁用allow_url_fopen的服务器)
如果服务器禁用了 allow_url_fopen,改用 CURL 实现:
<?php $videoUrl = 'https://rr4---sn-4wg7ln7k.googlevideo.com/videoplayback?expire=1659178416&ei=ULnkYojCCteA2LYP6fy9gAY&ip=18.207.249.128&id=o-AH5lc61_k63NQzB0KfAwR3eKkWwyf6UlhiglXm4yeZ1G&itag=244&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278%2C597%2C598&source=youtube&requiressl=yes&spc=lT-KhmC1OQXH6E1Bwoi7FX0xjBgFfGU&vprv=1&mime=video%2Fwebm&ns=VbQMOPVRVjJo8hxCD1PMQ88H&gir=yes&clen=21400428&dur=460.000&lmt=1659122075115614&keepalive=yes&fexp=24001373,24007246&c=MWEB&txp=7319224&n=vuqpLdTQIjRGZJYI&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cspc%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&sig=AOq0QJ8wRgIhAKjNSNNnW-Zi3_qeXCI3N6Stx9M1BoTKyXMlU5_xlZMHAiEA_tGiCCCcd37kVTJHmE7AxiJHlSw05UJY_MFHBcNEKpE%3D&rm=sn-p5qe7r7e,sn-uxaxovg23-aixe7l&req_id=e29350f8131ca3ee&ipbypass=yes&redirect_counter=2&cms_redirect=yes&cmsv=e&mh=iR&mip=103.7.79.121&mm=29&mn=sn-4wg7ln7k&ms=rdu&mt=1659156550&mv=m&mvi=4&pl=24&lsparams=ipbypass,mh,mip,mm,mn,ms,mv,mvi,pl&lsig=AG3C_xAwRgIhAJWZ4pMz_cCq37cAARiY6120pKwzb2lMPiKEQa4A-lqXAiEAz-wXlSB2hoxwLu-V3nPL69zGhF0cJ9CxDvMLEuUUvrQ%3D'; $fileName = 'downloaded-video.webm'; $ch = curl_init($videoUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); $response = curl_exec($ch); if (curl_errno($ch)) { die('请求视频失败:' . curl_error($ch)); } // 分离响应头和内容 $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headers = substr($response, 0, $headerSize); $body = substr($response, $headerSize); // 解析内容长度 preg_match('/Content-Length: (\d+)/', $headers, $matches); $contentLength = $matches[1] ?? strlen($body); // 设置下载头 header('Content-Type: video/webm'); header('Content-Disposition: attachment; filename="' . $fileName . '"'); header('Content-Length: ' . $contentLength); // 输出视频内容 echo $body; curl_close($ch); exit; ?>
注意事项:
- 确保服务器开启了
allow_url_fopen(基础方案)或 CURL 扩展(进阶方案)。 - 处理大文件时,建议分块输出避免内存溢出。
- 你提供的 Google Video 链接是临时授权链接,过期后无法下载,实际场景中需先获取有效长期的视频源链接。
内容的提问来源于stack exchange,提问作者Syed Khuram Abbas Naqvi
相关产品推荐
相关产品推荐

