1
0

More than 1 year has passed since last update.

【メモ】local環境でWebRTCを用いて映像を撮影した時のデータ保存について

Last updated at Posted at 2022-03-04

該当箇所のコード

index.js
function startRecorder() {
 navigator.mediaDevices.getUserMedia(constrains)
 .then(function (stream) {
   recorder = new MediaRecorder(stream)
   recorder.ondataavailable = function (e) {
     console.log(e);
     // id testを操作できるよう取得
     var testvideo = document.getElementById('test')
 
     // id testに属性を追加する
     // https://developer.mozilla.org/ja/docs/Web/API/Element/setAttribute
     testvideo.setAttribute('controls', '')
     testvideo.setAttribute('width', width)
     testvideo.setAttribute('height', height)
 
     var outputdata = window.URL.createObjectURL(e.data)
 
     record_data.push(e.data)
     testvideo.src = outputdata
   }
 })
}

個別箇所の挙動

recorder = new MediaRecorder(stream)
recorder.ondataavailable = function (e) {
var outputdata = window.URL.createObjectURL(e.data)

Blobとは

  • typeを持つバイナリデータ
  • 巨大な画像、音声ファイル、動画ファイルなどを扱うことができる

URLとしてのBlob

  • typeがあることによりblobをダウンロード/アップロード可能
  • URL.createObjectURLを使用して端末のメモリに保存されたblobにアクセスが可能な一意のURLを生成できる
  • メモリに存在するblobへのアクセスマッピングを行っている
  • 生成したURLはブラウザを閉じるまで有効
    • 逆に言えばブラウザを閉じるまではメモリに常駐し続けるのでURL.revokeObjectURLを使って明示的にメモリ解放する必要がある
  • 参考
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0