2017-04-24 216 views
0

我需要將Web服務器上選定的視頻文件傳輸到客戶端。我正在使用PHP腳本根據請求流式傳輸這些文件,但是我遇到了一個問題,如果我在本地網絡上,它可以正常工作,但如果我是遠程的,它會結束。例如。它似乎加載流,停止,加載更多,停止等,直到有足夠的媒體可用於播放。我對這個東西很陌生,所以我需要一些關於如何解決這個問題的建議。帶寬不應該是一個問題,所以我不知道發生了什麼。我已經嘗試了幾個PHP的飄帶,但所有的行爲都差不多。我正在使用videojs在客戶端中顯示它。使用PHP從服務器流式傳輸視頻

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

的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(); 

>

目前我想出來的VideoStream.php類爲:? VideoStream

什麼也奇怪的是,玩家底部的酒吧似乎表明該視頻已被緩衝,但玩家仍然口吃。不確定那是什麼意思。

+0

如果您分享您迄今爲止所嘗試的內容,包括示例代碼,它會有所幫助。 – iangetz

+0

你有沒有看過http://nginx.org/en/docs/http/ngx_http_mp4_module.html和https://github.com/arut/nginx-rtmp-module,PHP並不是很適合你想要的東西,它增加了很多開銷。 – hdezela

回答

0

您描述的行爲是低或間歇性帶寬連接上視頻傳輸的典型特徵。

的方法來解決這個通常是相當複雜的,但在服務器端內置於現成的流媒體服務器,例如:https://gstreamer.freedesktop.org

你也可以使用一個CDN像雲鋒或Akami等受益 - 這些基本上旨在在網絡邊緣創建內容的副本,以縮短用戶的響應時間。