4

例如,我有这个 URL:

http://video.ak.fbcdn.net/hvideo-ak-prn2/v/1032822_578813298845318_1606611618_n.mp4?oh=c3c6a02985213f7c47386f4653792ca6&oe=5200506F&__gda__=1375798216_02752679a44bc4b3c514bee21e000959

如何通过 PHP 下载视频源文件?

注意下载网址不会给我视频源!

// does not work:
file_put_contents('video.mp4', 'http://video.ak.fbcdn.net/hvideo-ak-prn2/v/1032822_578813298845318_1606611618_n.mp4?oh=c3c6a02985213f7c47386f4653792ca6&oe=5200506F&__gda__=1375798216_02752679a44bc4b3c514bee21e000959');      
// this does not download the video source but instead gets me a file that links to the video hosted on Facebook.
4

3 回答 3

6

file_put_contents('derp.mp4', file_get_contents('http://video.ak.fbcdn.net/hvideo-ak-prn2/v/1032822_578813298845318_1606611618_n.mp4?oh=c3c6a02985213f7c47386f4653792ca6&oe=5200506F&__gda__=1375798216_02752679a44bc4b3c514bee21e000959'));

于 2013-08-04T20:31:44.100 回答
1

此代码更智能,您只需在此代码中提供视频链接即可。要获取视频链接,只需右键单击视频,然后单击显示视频链接,或者您可以直接从浏览器 URL 栏中复制视频链接,如下图所示: 复制视频网址

然后将该 URL 粘贴到下面代码的PASTE_FACEBBOOK_VIDEO_LINK_HERE部分中

<?php
    $options  = array('http' => array('user_agent' => 'custom user agent string'));
    $context  = stream_context_create($options);
    $response = file_get_contents('__PASTE_FACEBBOOK_VIDEO_LINK_HERE__', false, $context);
    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', strip_tags($response), $match);
    $searchword = 'video';
    $matches = array_filter($match[0], function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
    $filename = rand().".mp4";
    file_put_contents($filename, fopen(reset($matches), 'r'));

生成的 .mp4 文件看起来像 24424353.mp4

于 2019-11-07T18:47:56.110 回答
0

有一种简单的方法可以做到这一点。您需要创建用于高清和标清质量的函数,然后创建使用 curl 的文件获取函数

function hdLink($curl_content)
{
    $regex = '/hd_src:"([^"]+)"/';
    if (preg_match($regex, $curl_content, $match)) {
        return $match[1];
    } else {
        return;
    }
}

function sdLink($curl_content)
{
    $regex = '/sd_src_no_ratelimit:"([^"]+)"/';
    if (preg_match($regex, $curl_content, $match1)) {
        return $match1[1];
    } else {
        return;
    }
}
function url_get_contents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

因此,在您的 HTML 中,您会将 Facebook 视频 URL 传递给 url_get_contents() 函数

<?php 
require_once("functions.php");

if (!empty($_POST["url"]) ) {

    $data = url_get_contents($_POST["url"]);
    $hdlink = hdLink($data);
    $sdlink = sdLink($data);

    if (!empty($sdlink) && !empty($hdlink) ) {?>

       <a target="_blank" download data-href="<?php echo $hdlink; ?>" href="<?php echo $hdlink; ?>" class="btn btn-block btn-lg btn-success">Download Video</a>
   <?php }

 }
 ?>

参考:如何在answerbox.net上通过3 步开发自己的 Facebook 视频下载器

于 2020-05-17T17:12:30.010 回答