我想要一个可以返回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;
}
当我转到终端时,Chrome认出它是一段视频,但我不认为它会返回任何实际视频。玩家没有看到任何东西,它会瞬间加载。
答案 0 :(得分: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")))
}
或使用自定义缓冲的非Slim方法:
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);
}