Huy Nguyen
Huy Nguyen

Reputation: 591

How to save an array of Buffer to webm video in Nodejs

[
  <Buffer 1a 45 df a3 9f 42 86 81 01 42 f7 81 01 42 f2 81 04 42 f3 81 08 42 82 84 77 65 62 6d 42 87 81 04 42 85 81 02 18 53 80 67 01 ff ff ff ff ff ff ff 15 49 ... 6365 more bytes>,
  <Buffer 41 83 81 03 c0 80 fb 03 5e ca af 6b 8a 35 12 02 42 0f 9b 35 37 9f 1f 4e a1 a3 05 7d b8 22 45 50 11 4f 01 d4 69 01 8d ee b6 3d 11 1e 7b 9d 25 f7 39 6b ... 6624 more bytes>,
  <Buffer 41 bc 81 07 bc 80 fb 83 7b 7f 5a 83 1e bf 32 bc a3 93 af dc c1 b3 14 09 52 d1 5b ca 91 ab 52 43 3e a2 d5 e6 11 d1 cf 54 24 71 ad 73 57 85 d0 5e 34 02 ... 6762 more bytes>,
  <Buffer 41 86 81 0b b8 80 fb 83 7f 80 53 a3 08 1f 88 d2 b1 66 e8 7f 1c 58 fc 7b 93 26 4b e6 58 0f 83 85 ee 27 e9 80 48 19 d7 fb 0a b2 0f 20 27 82 12 46 fe 6f ... 6496 more bytes>
]

I have an array of Buffer like this. How can i create a webm video in Nodejs? In frontend I can new Blob and send to server. But in Node there isn't new Blob method.

Upvotes: 0

Views: 2364

Answers (1)

kahwooi
kahwooi

Reputation: 91

I assume the "Blob" is a WebM binary file that you want to save in the server.

You can push those buffers into a readable stream and pipe it out as a WebM file.

Example code:

const fs = require('fs');
const { Readable } = require('stream');
const webmBuffers = [
   <Buffer...>,
   <Buffer...>,
];

const webmReadable = new Readable();
webmReadable._read = () => {  };
webmBuffers.forEach(chunk => {
    webmReadable.push(chunk);
});
webmReadable.push(null);

const outputWebmStream = fs.createWriteStream('./bunny.webm');
webmReadable.pipe(outputWebmStream);

Output:

A media file bunny.webm is generated in the same directory.

Upvotes: 3

Related Questions