Async recording with mimeType picker

by jib1

HTML

<button id="button">Record!</button>
<select id="select"></select><br>
<video id="video1" height="120" width="160" autoplay></video>
<video id="video2" height="120" width="160" autoplay controls></video><br>
<div id="div" style="white-space: pre;"></div><br>
<a id="link"></a>

JavaScript

const console = {log: msg => div.innerHTML += `${msg}<br>`};
const wait = ms => new Promise(r => setTimeout(r, ms));
const seconds = 5;

const vC = ["vp8", "vp08", "vp9", "vp09", "av1", "av01",
            "h264", "avc1", "avc3",
            "hvc1", "hev1"],
      pros = ["bar", ".", ".foo", ".0.19M.08",
              ".64003E", ".1.6.L186.B0", ".40.2"],
      aC = ["opus", "pcm", "x-pcm", "L8", "L16", "L20", "L24", "mp4a"],
      out = [];

for (const m of ["audio/webm", "audio/mp4", "audio/wav", "audio/aiff"]){
  out.push(m);
  for (const ac of aC) {
    out.push(`${m};codecs=${ac}`);
    for (const pro of pros) out.push(`${m};codecs=${ac}${pro}`);
  }
}
for (const m of ["video/webm", "video/mp4", "video/x-matroska"]) {
  out.push(m);
  for (const vc of vC) {
    out.push(`${m};codecs=${vc}`);
    for (const pro of pros) out.push(`${m};codecs=${vc}${pro}`);
    for (const ac of aC) {
      out.push(`${m};codecs=${vc},${ac}`);
      for (const pro of pros) out.push(`${m};codecs=${vc},${ac}${pro}`);
    }
  }
}

let foo;
function fooFilter(mt) {
  if (mt.endsWith(".foo")) { foo = mt; return true; }
  if (!foo) return true;
  if (mt.startsWith(foo.slice(0, -3))) return false;
  return true;
}

const mimeTypes = out
  .filter(mt => MediaRecorder.isTypeSupported(mt))
  .filter(fooFilter)
  .map(mt => mt.replace(/\.foo/g, '.*').replace(/bar/g, '*'));

for (const mimeType of mimeTypes) {
  const option = document.createElement("option");
  option.value = option.text = mimeType;
  select.appendChild(option);
}
console.log(`Supported mimeTypes (${mimeTypes.length}) = ${JSON.stringify(mimeTypes, null, 2)}`);

button.onclick = async () => {
  try {
    video1.srcObject = await navigator.mediaDevices.getUserMedia({
      video: true,
      audio: true,
    })
    await new Promise((r) => (video1.onloadedmetadata = r))
    console.log(`Recording ${seconds} seconds in ${select.value}`)
    const blob = await...