【问题标题】:PHP VideoStream class in SilverStripeSilverStripe 中的 PHP VideoStream 类
【发布时间】:2016-10-25 22:37:20
【问题描述】:

我正在使用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']);
    }
}

【问题讨论】:

  • 结果页的源代码(视频部分)是什么? 的 src="" 属性是否有预期值?
  • 视频源 VideoMP4 由页面模型中的 has_one 数组保存。您有使用 Silverstripe 的经验吗?
  • @3dgoo,感谢您清理我的英语和代码!你有答案吗?您似乎是 Silverstripe 编码的大师。

标签: php mp4 silverstripe webm


【解决方案1】:

这是一种方法。

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 %>

【讨论】:

  • 像魅力一样工作!!!我只需要将我的 $stream 变量替换为您的。 $stream = new VideoStream($this->VideoMP4()->getFullPath());谢谢,非常感谢。我不知道 getFullPath() 命令。
猜你喜欢
  • 1970-01-01
  • 2011-08-25
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多