0

我可以用网络摄像头录制视频,在浏览器上播放生成的 blob 并将其下载到本地机器上,但是当我将文件保存到服务器时,它是不可读的。我尝试将块发送到服务器并将它们连接在那里,并发送整个 blob,但结果是相同的(不可读的视频)。我首先使用 FileReader() 读取 blob,它给出 base64 结果,然后将其发送到服务器,在服务器上我使用 base64_decode() 并将其保存到文件夹中。

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");
}

我不知道我做错了什么。我已经尝试了一个多星期来让它工作,但它不会。请帮忙!

4

1 回答 1

0

你必须保存解码的块

而不是这个

file_put_contents("/uploadDirecotry/chunk".$json['index'].".webm", $json["chunk"]);

用这个

file_put_contents("/uploadDirecotry/chunk".$json['index'].".webm", $chunk);

另外我建议,请在写入模式下在“for循环”之前打开最终文件并在循环后关闭它,而不是每次都在“for循环”中重新打开。

于 2017-08-28T12:37:17.597 回答