I am recording a webcam-video through recordRTC which has an ondataavailable
callback which is called every 2000 milliseconds. The ondataavailable
method provides a Blob
as parameter, so it will always contain the current video blob. I would like to upload the video in slices to the server while the recording is ongoing to avoid having a big file to upload in the end as one.
For that, the blobs are sliced with Blob.slice():
ondataavailable(blob) {
// Create a new blob from last blob size to the end of the blob
const sliceBlob = blob.slice(this._state.lastBlobSize, blob.size, blob.type)
this.setState('lastBlobSize', blob.size)
this.fileHandler.upload(sliceBlob)
}
When I upload the blobs to the server and convert them to .webm
videos which are later concatenated again using FFMpeg, only the first blob is a valid video, but every following webm-file does not play.
Here is what the logged blobs look like:
I assume that the blobs are invalid due to the byte-length not containing valid sizes or something like that, so all videos after the first start with an illegal offset and can not be read therefore. But I have no idea how to change the slices to get properly working parts.
Also, the videoBitsPerSecond
is set to 128000 * 4
on the recordRTC config, which would be 32000
byte per video slice (16000 per second) which might help finding the correct offset. Hover, looking at the "sliced blob" sizes, this does not seem to directly correlate - the blob sizes are bigger.
If there is no way to easily perform the slices like this, is there another way to achieve "streaming" smaller files to the server while the recording is ongoing?
Edit:
As to CBroe's comment, I modified the controller to do the following when a blob is received:
$filePath = "/var/www/html/public/assets/blob.webm";
$fp = fopen($filePath, file_exists($filePath) ? "a" : "w");
fwrite($fp, file_get_contents($_FILES["blob"]["tmp_name"]));
fclose($fp);
So it will always append the latest blob to the complete file. However, the file is still invalid when the video is properly stopped and the last blob is uploaded. Any suggestions?