|
1 | 1 | # fix-webm-duration
|
2 |
| -navigator.mediaDevices.getUserMedia + MediaRecorder create WEBM files without duration metadata. This library appends missing metadata section right to the file blob. |
| 2 | + |
| 3 | +`navigator.mediaDevices.getUserMedia` + `MediaRecorder` create WEBM files without duration metadata. |
| 4 | + |
| 5 | +This library appends missing metadata section right to the file blob. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +The library contains only one script `fix-webm-duration.js` and has no dependencies. |
| 10 | + |
| 11 | +Syntax: |
| 12 | + |
| 13 | +```javascript |
| 14 | +ysFixWebmDuration(blob, duration, callback); |
| 15 | +``` |
| 16 | + |
| 17 | +where |
| 18 | +- `blob` is `Blob` object with file contents from `MediaRecorder` |
| 19 | +- `duration` is video duration in milliseconds (you should calculate it while recording the video) |
| 20 | +- `callback` is callback function that will receive fixed blob |
| 21 | + |
| 22 | +`ysFixWebmDuration` will parse and fix your file asynchronously and will call your callback once the result is ready. |
| 23 | + |
| 24 | +If the original blob already contains duration metadata section, the callback will receive it without any changes made. |
| 25 | + |
| 26 | +Example: |
| 27 | + |
| 28 | +```javascript |
| 29 | +var mediaRecorder; |
| 30 | +var mediaParts; |
| 31 | +var startTime; |
| 32 | + |
| 33 | +function startRecording(stream, options) { |
| 34 | + mediaParts = []; |
| 35 | + mediaRecorder = new MediaRecorder(stream, options); |
| 36 | + mediaRecorder.onstop = function() { |
| 37 | + var duration = Date.now() - startTime; |
| 38 | + var buggyBlob = new Blob(mediaParts, { type: 'video/webm' }); |
| 39 | + |
| 40 | + ysFixWebmDuration(buggyBlob, duration, function(fixedBlob) { |
| 41 | + displayResult(fixedBlob); |
| 42 | + }); |
| 43 | + }; |
| 44 | + mediaRecorder.ondataavailable = function(event) { |
| 45 | + var data = event.data; |
| 46 | + if (data && data.size > 0) { |
| 47 | + mediaParts.push(data); |
| 48 | + } |
| 49 | + }; |
| 50 | + mediaRecorder.start(); |
| 51 | + startTime = Date.now(); |
| 52 | +} |
| 53 | + |
| 54 | +function stopRecording() { |
| 55 | + mediaRecorder.stop(); |
| 56 | +} |
| 57 | + |
| 58 | +function displayResult(blob) { |
| 59 | + // ... |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +Note: this example **is not** a `MediaRecorder` usage guide. |
0 commit comments