SilverStripe中的PHP VideoStream类

时间:2016-10-25 22:37:21

标签: php mp4 silverstripe webm

我正在使用VideoStream课,效果非常好。我尝试将其集成到Page_Controller中,并保护视频不被上传。因此,每次加载页面时,视频链接总是在重新加载时更改,没有人可以直接从浏览器URL下载。

但课程似乎不起作用,我不知道我错过了什么。我还使用ChunkedUpload模块将大文件上传到CMS。

任何想法如何使用该类?

这是我的代码。

注意:我的代码有效,我在下面注明“我原来的......”

PageVideo.ss

<video controls preload="auto" id="video1">
    <% if $VideoMP4 %><source src="id/$Video($VideoMP4.ID)" type="video/mp4"><% end_if %>
    Your browser does not support HTML5 video.
</video>

PageVideo.php

class PageVideo extends Page {

    private static $has_one = array(
        'VideoMP4' => 'File'
    );

    function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab(
            'Root.Main ',
            $uploadField = new ChunkedUploadField(
                $name = 'VideoMP4',
                $title = "VideoMP4"
            )
        );

        $uploadField->setFolderName('Uploads/videos/');
        $uploadField->setDisplayFolderName('Uploads/videos');
        $uploadField->getValidator()->allowedExtensions = array("mp4");

        $sizeMB = 500 * 1024 * 1024; // 500 MB in bytes
        $uploadField->getValidator()->setAllowedMaxFileSize($sizeMB);
        $uploadField->setConfig('maxChunkSize', .05 * 1024 * 1024 );

        return $fields;
    }
}

class PageVideo_Controller extends Page_Controller {

    private static $allowed_actions = array('VideoGrab');
    private static $url_handlers = array('id/$hash' => 'VideoGrab');

    public function VideoGrab(SS_HTTPRequest $request) {

        $hash = $request->param('hash');

        $file = File::get()->filter('ID', $_SESSION['keyID'])->first();

        if (md5($file->Name . $_SESSION['key']) == $hash) {

            $_SESSION['key'] = "";
            $_SESSION['keyID'] = "";

            //**************** HERE IS THE CLASS *********************
            include "libraries/VideoStream.php";
            $stream = new VideoStream($file->AbsoluteURL);
            $stream->start();
            exit;

            // the original php working just for some browsing and bad codes
            /*
            $ext = pathinfo($file->Filename);
            header('Cache-control: private');
            header('Content-Type: video/' . $ext['extension']);
            header('Accept-Ranges: bytes');
            header("Content-Transfer-Encoding: binary");

            return readfile($file->AbsoluteURL);
            exit;
            */
        } else {
            return $this->httpError(404, "Not Found");
        }
    }

    public function Video($n) {

        $file = File::get()->filter('ID', $n)->First();
        $_SESSION['key'] = time();
        $_SESSION['keyID'] = $file->ID;

        return md5($file->Name . $_SESSION['key']);
    }
}

1 个答案:

答案 0 :(得分:0)

这是一种方法。

<强>类

class PageVideo extends Page {

    private static $has_one = array(
        'VideoMP4' => 'File'
    );

    function getCMSFields() {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab('Root.Video', $uploadField = UploadField::create('VideoMP4'));

        $uploadField->setFolderName('Uploads/videos/');
        $uploadField->getValidator()->allowedExtensions = array('mp4');

        return $fields;
    }
}

class PageVideo_Controller extends Page_Controller {

    private static $allowed_actions = array(
        'VideoStream'
    );

    function VideoStream() {
        if ($this->VideoMP4()->exists()) {
            include 'libraries/VideoStream.php';
            $stream = new VideoStream($this->VideoMP4()->getFullPath());
            return $stream->start();
        }
        return false;
    }
}

<强>模板

<% if $VideoMP4 %>
<video controls preload="auto" id="video1">
    <source src="{$Link}VideoStream" type="video/mp4">
    Your browser does not support HTML5 video.
</video>
<% end_if %>