简体   繁体   English

使用PHP从服务器流式传输视频

[英]Streaming video with PHP from server

I need to stream selected video files on the web server to clients. 我需要将Web服务器上的选定视频文件流式传输到客户端。 I am using a PHP script to stream these files as requested, but I am running into an issue where if I am on a local net, it works fine, but if I am remote, it stutters. 我正在使用PHP脚本按请求流式处理这些文件,但是遇到了一个问题,即如果我在本地网络上,它可以正常工作,但是如果我是远程服务器,它会结结巴巴。 eg. 例如。 it appears to load the stream, stop, load more, stop, etc. until enough media is available to play. 它似乎可以加载流,停止,加载更多,停止等,直到可以播放足够的媒体为止。 I am new to this stuff, so I need some advise on how to overcome this issue. 我是新手,所以我需要一些有关如何克服此问题的建议。 The bandwidth should not be a problem, so I am not sure what is going on. 带宽应该不是问题,所以我不确定发生了什么。 I have tried several php streamers, but all act pretty much the same. 我已经尝试了几种php流光,但是它们的行为几乎相同。 I am using videojs to display it within the client. 我正在使用videojs在客户端中显示它。

<?php
use vendor\videojswidget\VideoJsWidget;
$url = Yii::$app->urlManager->createUrl('/site/putvideo');
$currentVideo = '';
$script1 = "function playAnotherVideo(){
    var source = document.getElementById('fileSelector');
    var path = source.value;
    $.ajax({
        type: 'post',
        data: {file: path},
        url: '" .$url. "',
        success: function(result){
            var video = document.getElementById('videoPlayer');
            video.src = result;
            alert(result);
            video.load();
        },
        error: function(){
            alert('error');
        }
    });
}";

$this->registerJs($script1, yii\web\View::POS_END, 'my-options');

/* @var $this yii\web\View */

$this->title = 'View Vault Lecture Capture System';
?>
<div class="site-index" style="background-color: black; color: yellow;">

    <div style="background-color: black;">  
        </br></br></br></br>     
        <h1 align="center">Welcome to the TEKVOX Lecture Capture System</h1>
        </br></br></br></br> 
    </div>
    <div class="body-content">
        <div style="width: 100%; overflow: hidden;">
        <div style="width: 50px; float: left;">
        </br></br>
        <label for="fileselector" style="margin-left: 4em; width: 10em; 
            font-weight: bold; color: #FFFF00;">Video_Files</label>
        <select id="fileSelector" onchange="playAnotherVideo()" size="10em" 
        style="margin-left: 0em; color: #FFFF00; background-color: #000000; 
        border-color: #FFFFFF">
<?php
        if ($handle = opendir('c:/users/admin/videos/')) {

            while (false !== ($entry = readdir($handle))) {

                if ($entry != "." && $entry != ".." && 
                    strtolower(substr($entry, strrpos($entry, '.') + 1)) == 
                       'mp4' ){       
                    echo "<option value='$entry'>" .$entry. "</option>";
                }
            }

            closedir($handle);
        }
?>
</select>
</div>
<span id="divplayer" style="margin-left: 20em;">
<?php

echo VideoJsWidget::widget([
    'options' => [
        'class' => '',
        'id' => 'videoPlayer',
        'poster' => "GreenX.png",
        'controls' => true,
        'preload' => 'none',
        'width' => '800',
        'height' => '450',
    ],
    'tags' => [
        'source' => [
            ['src' => '', 'type' => 'video/mp4'],
        ],
        'track' => [
            ['kind' => 'captions', 'src' => 
'http://vjs.zencdn.net/vtt/captions.vtt', 'srclang' => 'en', 'label' => 
'English']
        ]
    ]
]); 

?>
        </span> 
    </div>
</div>
</br>

The getVideo.php code: getVideo.php代码:

<?php
include_once "videoStream.php";
$filename = $_GET['filename'];
if($filename == '')
    return;
$file = "c:\\users\\admin\\videos\\" .$filename;

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

?> ?>

The VideoStream.php class I am currently trying out is at: VideoStream 我目前正在尝试的VideoStream.php类位于: VideoStream

What is also strange is that the bar at the bottom of the player seems to indicate that the video is buffered, yet the player still stutters. 同样奇怪的是,播放器底部的栏似乎表明视频已缓冲,但是播放器仍然结结巴巴。 Not sure what that means. 不确定那是什么意思。

The behaviour you describe is typical of video delivery over a low or intermittent bandwidth connection. 您描述的行为是通过低带宽或间歇带宽连接进行视频传输的典型行为。

The approaches to resolve this are typically quite sophisticated but on the server side built into readily available streaming servers, eg: https://gstreamer.freedesktop.org 解决此问题的方法通常非常复杂,但在服务器端内置于易于使用的流服务器中,例如: https : //gstreamer.freedesktop.org

You also may benefit from using a CDN like cloud front or Akami etc - these are basically designed to create copies of your content at the edge of the network to improve response times for users. 您还可以从使用CDN(例如Cloud Front或Akami等)中受益-这些CDN的基本目的是在网络边缘创建内容的副本,以缩短用户的响应时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM