ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

VideoJS Flash版本无法播放文件(PHP通过流)

2019-11-23 02:30:05  阅读:230  来源: 互联网

标签:flash video-js streaming php


我正在使用最新版本的VideoJS,并通过以下方式通过PHP放置我的视频:

像这样调用:$this-> streaming-> handleDownload($videoPath,“ video / mp4”);

该函数的定义如下:

public function handleDownload($file, $mime = "") {
     if (is_file($file)) {
        header("Content-Type: $mime");

        if (isset($_SERVER['HTTP_RANGE']))  { // do it for any device that supports byte-ranges not only iPhone
            header("X-Info: Starting Ranged Download of $file");
            $this->rangeDownload($file);
        } else {
            header("Content-Length: ".filesize($file));
            header("X-Info: Starting Straight Download of $file");
            readfile($file);
        }
    } else {
        header("HTTP/1.1 500 Internal Server Error");
        $msg = "The requested path was no file!";
    }
}

rangeDownload()定义为:

private function rangeDownload($file) {
    ob_end_clean();
    $fp = @fopen($file, 'rb');

    $size   = filesize($file); // File size
    $length = $size;           // Content length
    $start  = 0;               // Start byte
    $end    = $size - 1;       // End byte

    header("Accept-Ranges: 0-$length");
    if (isset($_SERVER['HTTP_RANGE'])) {

        $c_start = $start;
        $c_end   = $end;
        // Extract the range string
        list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
        // Make sure the client hasn't sent us a multibyte range
        if (strpos($range, ',') !== false) {
            header('HTTP/1.1 416 Requested Range Not Satisfiable');
            header("Content-Range: bytes $start-$end/$size");
            exit;
        }
        header("X-Got-Range: $range, $range0");
        // If the range starts with an '-' we start from the beginning
        // If not, we forward the file pointer
        // And make sure to get the end byte if spesified
        if ($range0 == '-') {
            $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;
        }

        // End bytes can not be larger than $end.
        $c_end = ($c_end > $end) ? $end : $c_end;
        // Validate the requested range and return an error if it's not correct.
        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");
            // (?) Echo some info to the client?
            exit;
        }
        $start  = $c_start;
        $end    = $c_end;
        $length = $end - $start + 1; // Calculate new content length
        fseek($fp, $start);
        header('HTTP/1.1 206 Partial Content');
    }

    // Notify the client the byte range we'll be outputting
    header("Content-Range: bytes $start-$end/$size");
    header("Content-Length: $length");

    // Start buffered download
    $buffer = 1024 * 8;
    while(!feof($fp) && ($p = ftell($fp)) <= $end) {

        if ($p + $buffer > $end) {

            // In case we're only outputtin a chunk, make sure we don't
            // read past the length
            $buffer = $end - $p + 1;
        }
        set_time_limit(0); // Reset time limit for big files
        echo fread($fp, $buffer);
        flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
    }

    fclose($fp);
}

我正在通过PHP传递视频,因为仅在登录时文件才可见.
我使用“部分内容/范围”来做这些事情,因为我希望能够在“位置”栏的中间单击,并且视频应该从那里开始播放.

一切都适用于Chrome,较新的Safari,iOS,但是(由于Flash后备)在IE或Firefox(视频为mp4)中不起作用(也许加载时间太长)

Range可以在Chrome中运行,但是很好,VideoJS的Flash版本甚至不要求Range-Download,因此它获得了另一个版本.

如果我在Firefox中直接播放视频(使用PHP流,但不使用VideoJS)(只需在URL栏中调用Video-URL),它将直接启动并在播放时加载; VideoJS没有

我需要更改使视频开始直接在VideoJS Flash版本中播放吗?

解决方法:

我已经找到了解决方案(偶然;-))

问题是Flash播放器要求将视频作为完整文件.只要服务器不回答,就存在播放器不要求的流版本(接受范围:0- $size).

所以我设置了Header并且它起作用了.

因此,针对所有与VideoJS Flash后备广告有关的问题的小教程:

>视频需要通过MP4Box进行编码(请参见MP4 in Video.js not playing until fully loaded)
> PHP Passthrough脚本需要发送以下标头:

header(“ Content-Type:$videoMimeType”);
header(“ Content-Length:$videoByteCount”);
header(“接受范围:0- $videoByteCount”);
>如果要处理范围(由isset($_ SERVER [“ HTTP_RANGE”])标识),则还需要指定这些标头:

header(“ Content-Range:bytes $startByte- $endByte / $videoByteCount”);

这对我有用(请参阅我的问题中的代码).它可以与带有Flash的Chrome,FF,iOS,Safari和Safari(IE尚未经过测试)一起使用,并与mp4-Files一起使用HTML5

标签:flash,video-js,streaming,php
来源: https://codeday.me/bug/20191123/2064387.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有