Saving Huge Files

寵の児 提交于 2019-12-05 02:48:37

问题


I need to save a file of unknown size, potentially multiple gigabytes, in JS. The data source is a mediastream captured using mediarecorder.

In Chrome, this can be accomplished using the filesystem and filewriter apis with filesystem: urls by writing blob chunks to a fileentry as they are received, then setting a download link to the file url.

However, I cannot find a way to do this in Firefox or Edge (whenever it gets mediarecorder).


回答1:


This works for me in Firefox:

navigator.mediaDevices.getUserMedia({ video: true })
  .then(stream => record(stream, 5000)
    .then(recording => {
      stop(stream);
      video.src = link.href = URL.createObjectURL(new Blob(recording));
      link.download = "recording.webm";
      link.innerHTML = "Download blob";
      log("Playing "+ recording[0].type +" recording.");
    })
    .catch(log).then(() => stop(stream)))
  .catch(log);

var record = (stream, ms) => {
  var rec = new MediaRecorder(stream), data = [];
  rec.ondataavailable = e => data.push(e.data);
  rec.start();
  log(rec.state + " for "+ (ms / 1000) +" seconds...");
  var stopped = new Promise((r, e) => (rec.onstop = r, rec.onerror = e));
  return Promise.all([stopped, wait(ms).then(() => rec.stop())])
    .then(() => data);
};

var stop = stream => stream.getTracks().forEach(track => track.stop());
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var log = msg => div.innerHTML += "<br>" + msg;
<video id="video" height="120" width="160" autoplay></video>
<a id="link"></a><br>
<div id="div"></div>

A user still has to click the download link. I haven't experimented with how large the file can get.



来源:https://stackoverflow.com/questions/36317046/saving-huge-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!