HTML5视频流仅限播放器

时间:2016-12-15 08:19:08

标签: php html5 video streaming

我试图让我在网站上的视频更难下载,但失败了一点。如果您的目标是在15分钟后杀死视频链接(足以让用户看到视频),解决方案就可以正常工作。

但我也想让播放器将此流式传输到HTML5视频播放器中,但不要让用户通过将视频源链接放在新标签中来下载它。

我试图让这个链接一次性工作,但问题是HTML5播放器连接到流式脚本不止一次。

所以可能有任何解决方案如何在我的流媒体脚本中我可以检查脚本是否被迫直接打开或HTML5播放器强制执行?或者也许还有另一种方法来阻止直接打开这个脚本?

流媒体脚本:

<?php

[...]    

if (!empty($_GET['id']) && !empty($_GET['token'])){
    if (strtotime($array['created']) > strtotime('-15 minutes')) {

    $file = 'Z:/home/localhost/www/mvc/video/' . $_GET['id'];

    $fp = @fopen($file, 'rb');
    $size   = filesize($file); // File size
    $length = $size;           // Content length
    $start  = 0;               // Start byte
    $end    = $size - 1;       // End byte

   header('Content-type: video/mp4');

   //header("Accept-Ranges: 0-$length");
   header("Accept-Ranges: bytes");

    if (isset($_SERVER['HTTP_RANGE'])) {
        $c_start = $start;
        $c_end   = $end;
        list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    if (strpos($range, ',') !== false) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    if ($range == '-') {
        $c_start = $size - substr($range, 1);
    }else{
        $range  = explode('-', $range);
        $c_start = $range[0];
        $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] :       $size;
    }
    $c_end = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    $start  = $c_start;
    $end    = $c_end;
    $length = $end - $start + 1;
    fseek($fp, $start);
    header('HTTP/1.1 206 Partial Content');
}

header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);
$buffer = 1024 * 32;

while(!feof($fp) && ($p = ftell($fp)) <= $end) {
    if ($p + $buffer > $end) {
        $buffer = $end - $p + 1;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
}

fclose($fp);
exit();
}
else {
echo 'Token is closed';
}
}
else {
echo 'Denied';
}

播放器看起来像这样:

<video width="640" height="480" preload controls>
<source src="http://localhost/mvc/video/video.php?id=video.mp4&token=0c9eb340fa59db2accf61b16663c79b1" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

1 个答案:

答案 0 :(得分:1)

经过两天残酷的战斗后,我终于找到了决定。

为了实现这个目标(让HTML5视频播放器流视频,但阻止直接下载链接),我改变了这个:

if (!empty($_GET['id']) && !empty($_GET['token'])){
if (strtotime($array['created']) > strtotime('-15 minutes'))
{
stream video...
}

到此:

if (!empty($_GET['id']) && !empty($_GET['token'])){
if (strtotime($array['created']) > strtotime('-15 minutes') && $_SERVER['HTTP_RANGE'] == true) {
stream...
}

当然,这不是保证视频的100%保证,但现在下载起来比较困难。

相关问题