如何将使用MediaRecorder API录制的视频保存到php服务器?
问题描述:
我可以用网络摄像头录制视频,在浏览器上播放结果blob并将其下载到本地机器上,但是当我将文件保存到服务器时,它是无法读取的。我已经尝试将块发送到服务器并将它们连接在一起,并且还发送了整个blob,但结果是相同的(不可读的视频)。 我首先用FileReader()读取blob,它提供了base64结果,然后将其发送到服务器,在那里我base64_decode()它并将其保存到文件夹。如何将使用MediaRecorder API录制的视频保存到php服务器?
JS代码:
var reader = new FileReader();
reader.readAsDataURL(chunks[index]);
reader.onload = function() {
upload(reader.result, function(response){
if(response.success){
// upload next chunk
}
});
};
在服务器上:
$json = json_decode($request->getContent(), true);
$chunk = base64_decode($json["chunk"]);
// all chunks get
file_put_contents("/uploadDirecotry/chunk".$json['index'].".webm", $json["chunk"]);
当所有块被上传:
for ($i = 0; $i < $nrOfChunks; $i++) {
$file = fopen("/uploadDirectory/chunk".$i.".webm", 'rb');
$buff = fread($file, 1024000);
fclose($file);
$final = fopen("/processed/".$video->getFileName()."-full.webm", 'ab');
$write = fwrite($final, $buff);
fclose($final);
unlink("/uploadDirectory/chunk".$i.".webm");
}
我不知道我做错了。我已经尝试了一个多星期的工作,但它不会。请帮忙!
答
你必须保存解码块
,而不是这个
file_put_contents("/uploadDirecotry/chunk".$json['index'].".webm", $json["chunk"]);
使用本
file_put_contents("/uploadDirecotry/chunk".$json['index'].".webm", $chunk);
而且我建议,请打开最终文件的“for循环”,在写模式之前的并在循环之后关闭它,而不是每次都在“for循环”中重新打开。
太棒了!非常感谢!一直在搞乱它,我已经多次更改了代码,并且错过了这个小小的错字。此外,必须添加以下行: $ json [“chunk”] = str_replace('data:video/webm; base64,','',$ json [“chunk”]); 解码之前,使其工作。 –