4

I'm using RecordRTC to record a video in 1000ms chunks, which are sent as blobs via AJAX to my server. The server saves these files, and at the end of the recording I need to rebuild them all into a single .webm.

I thought this would be as simple as concatenating the files and giving it the correct extension. Unfortunately not.

Below is my code for building the final file. It's in Laravel, but it's pretty self-explanatory:

public function buildBlob(Request $request)
{
    $files = Storage::disk('local')->files('blobs');
    $contents = '';

    foreach($files as $file) {
        $contents.= file_get_contents(storage_path('app/'. $file));
    }

    Storage::put('video.webm', $contents);

    return ['success' => true];
}

VLC will not play this file. Firefox complains it's an unknown filetype. Chrome just doesn't play it at all.

I guess my question is: how do I compile a new blob in PHP with the correct mimetype? In JavaScript I can do:

let blob = new File(this.blobs, 'video.webm', {
    type: 'video/webm'
});

So I suppose I need an equivalent.

Mike
  • 8,259
  • 8
  • 41
  • 93
  • _“record a video in 1000ms chunks, which are sent as blobs via AJAX”_ - those are probably each _complete_ little 1s long videos then …? If they include all the necessary extra headers / meta data that makes them playable on their own, then you will probably have to start re-encoding them on the server side, to get a full, playable video of the combined length again. – 04FS Nov 11 '19 at 11:58
  • They're blobs, not complete videos. There's no mime-type associated with them and they're not playable on their own unfortunately. – Mike Nov 11 '19 at 12:12
  • How are you guaranteeing the correct order of the files? – 04FS Nov 11 '19 at 12:15
  • They're named appropriately in JavaScript before uploading. `video-001.web`, `video-002.webm` etc – Mike Nov 11 '19 at 12:32
  • And `Storage::disk('local')->files('blobs')` _guarantees_ you get them sorted by those file names? – 04FS Nov 11 '19 at 12:33
  • It does, they come back alphabetically. I've just double checked the order in which they come back and it is correct – Mike Nov 11 '19 at 13:38
  • And what do they actually contain? _“which are sent as blobs via AJAX to my server. The server saves these files”_ is pretty vague. – 04FS Nov 11 '19 at 13:39
  • It's a 1 second chunk of video. Looking at the file created this is usually around 1KB of binary data. Each 1000ms has a different blob size but they're roughly the same (973B, 980B, 980B, 976B, etc). None of these blobs contain any plain text – Mike Nov 11 '19 at 13:51

0 Answers0