2017-12-29 60 views
0

私はmp4ビデオを返すエンドポイントを望んでいました。Slim 3でmp4ビデオを返す

フルエンドポイント

$app->get('{userid}/clips/{clipid}/video', '\GameDVRController:clipGetVideo'); 

たそしてそのエンドポイントのための機能が

public function clipGetVideo($request, $response, $args) { 
    $clipid = $args['clipid']; 
    $clip = GameClip::where('id', $clipid)->first(); 

    // (Note: clip->File is full path to file on disk) 
    $file = file_get_contents($clip->File); 
    $response->getBody()->write($file); 

    $response = $response->withHeader('Content-type', 'video/mp4'); 
    return $response; 
} 

私はエンドポイントに行き、クロムは、それがビデオだと認識しているが、私はそれはないと思います実際の動画のいずれかを返します。プレイヤーには何も表示されず、秒単位で読み込まれます。

+0

ファイルがどのくらいありますか? – jmattheis

答えて

0

これはおそらく大きなファイルで問題になります。私にとってはすべてが〜20MBのファイルで動作するようです。

私はこのようなストリームとしてファイルを設定することで問題を解決できます。

public function clipGetVideo($request, $response, $args) { 
    set_time_limit(0); 

    $clipid = $args['clipid']; 
    $clip = GameClip::where('id', $clipid)->first(); 

    $response = $response->withHeader('Content-type', 'video/mp4'); 
    return $response->withBody(new Stream(fopen($clip->File, "rb"))) 
} 

またはカスタムバッファリングとスリムではないアプローチを使用して:

public function clipGetVideo($request, $response, $args) { 
    set_time_limit(0); 

    $clipid = $args['clipid']; 
    $clip = GameClip::where('id', $clipid)->first(); 

    header('Content-Type: video/mp4'); 
    header('Content-Length: ' . filesize($clip->File)); 

    $handle = fopen($clip->File, "rb"); 
    while (!feof($handle)){ 
     echo fread($handle, 8192); 
     ob_flush(); 
     flush(); 
    } 
    fclose($handle); 
    exit(0); 
} 
関連する問題