【问题标题】:how to check how much of a video stream has been seen by user with php如何检查用户使用php查看了多少视频流
【发布时间】:2015-07-20 20:27:43
【问题描述】:

我正在开发一个向用户显示视频广告的项目,60 秒后用户可以看到一个链接。 我的老板希望确保用户观看全部 60 秒。现在我想知道有没有办法检查 php 看到了多少视频流? 要将视频呈现为流,我使用了以下代码:

http://codesamplez.com/programming/php-html5-video-streaming-tutorial

class VideoStream
{
    private $path = "";
    private $stream = "";
    private $buffer = 102400;
    private $start  = -1;
    private $end    = -1;
    private $size   = 0;

    function __construct($filePath) 
    {
        $this->path = $filePath;
    }

    /**
     * Open stream
     */
    private function open()
    {
        if (!($this->stream = fopen($this->path, 'rb'))) {
            die('Could not open stream for reading');
        }

    }

    /**
     * Set proper header to serve the video content
     */
    private function setHeader()
    {
        ob_get_clean();
        header("Content-Type: video/mp4");
        header("Cache-Control: max-age=2592000, public");
        header("Expires: ".gmdate('D, d M Y H:i:s', time()+2592000) . ' GMT');
        header("Last-Modified: ".gmdate('D, d M Y H:i:s', @filemtime($this->path)) . ' GMT' );
        $this->start = 0;
        $this->size  = filesize($this->path);
        $this->end   = $this->size - 1;
        header("Accept-Ranges: 0-".$this->end);

        if (isset($_SERVER['HTTP_RANGE'])) {

            $c_start = $this->start;
            $c_end = $this->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 $this->start-$this->end/$this->size");
                exit;
            }
            if ($range == '-') {
                $c_start = $this->size - substr($range, 1);
            }else{
                $range = explode('-', $range);
                $c_start = $range[0];

                $c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $c_end;
            }
            $c_end = ($c_end > $this->end) ? $this->end : $c_end;
            if ($c_start > $c_end || $c_start > $this->size - 1 || $c_end >= $this->size) {
                header('HTTP/1.1 416 Requested Range Not Satisfiable');
                header("Content-Range: bytes $this->start-$this->end/$this->size");
                exit;
            }
            $this->start = $c_start;
            $this->end = $c_end;
            $length = $this->end - $this->start + 1;
            fseek($this->stream, $this->start);
            header('HTTP/1.1 206 Partial Content');
            header("Content-Length: ".$length);
            header("Content-Range: bytes $this->start-$this->end/".$this->size);
        }
        else
        {
            header("Content-Length: ".$this->size);
        }  

    }

    /**
     * close curretly opened stream
     */
    private function end()
    {
        fclose($this->stream);
        exit;
    }

    /**
     * perform the streaming of calculated range
     */
    private function stream()
    {
        $i = $this->start;
        set_time_limit(0);
        while(!feof($this->stream) && $i <= $this->end) {
            $bytesToRead = $this->buffer;
            if(($i+$bytesToRead) > $this->end) {
                $bytesToRead = $this->end - $i + 1;
            }
            $data = fread($this->stream, $bytesToRead);
            echo $data;
            flush();
            $i += $bytesToRead;
        }
    }

    /**
     * Start streaming video content
     */
    function start()
    {
        $this->open();
        $this->setHeader();
        $this->stream();
        $this->end();
    }
}

$stream = new VideoStream($filePath);
$stream->start();

谢谢。

【问题讨论】:

  • 我在上面的代码中没有看到任何试图实现这一点的尝试。我所看到的只是您将教程中的代码链接到我们。你的努力在哪里?
  • @Ohgodwhy,实际上我尝试根据我的经验但没有成功,现在我试图询问天气是否有人这样做过。

标签: php html video stream


【解决方案1】:

下载视频后,服务器的工作就完成了。这必须在客户端完成。您需要一段 javascript 来在视频播放后显示链接。

【讨论】:

  • 如何查看视频是否播放了 60 秒?对我来说,用户首先观看 60 秒的视频然后我向他们显示链接是必要的。
  • 如何依赖您的播放器(html5、flash 等)。
  • 你能推荐一个对我的问题有帮助的特定播放器吗?
【解决方案2】:

这只能在 java 脚本的客户端完成。当视频开始播放时,您需要启动计时器。检查视频播放器 API,您将需要找到事件 API 以在视频开始时执行事件代码。 对于广告,您需要借助 css 在视频屏幕上重叠您的广告容器。 然后在函数中执行该代码

 setTimeout(function(){ 
 //Show your ad container here  
 }, 60000);
 //60,000 is equal to 60 seconds

【讨论】:

  • 谢谢,我检查了 FFMpeg,但他们没有任何方法、类或库来解决我的问题。
  • 您可以获得前 1 分钟的帧,然后在其间添加视频帧。这就像把汉堡面包切成两半,然后把你的东西放进去。
  • 我想你误会了,我有一个时长为 1 分钟的视频。我想向用户展示这个视频,并确保用户观看了全部 60 秒。之后,我想向用户显示链接。我只想检查用户使用 php 而不是使用 javascript 或 jquery 观看视频的秒数,因为有一些技巧可以欺骗 javascript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2016-09-14
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多