hughfenghen / hughfenghen.github.io

blog
https://hughfenghen.github.io/
43 stars 2 forks source link

Webcodecs 编解码对视频格式的兼容性(支持性)检测 #129

Open hughfenghen opened 1 year ago

hughfenghen commented 1 year ago

可复制代码在浏览器控制台中验证当前浏览器的支持情况

编码

const codecs = ["avc1.42E01F", "vp8", "vp09.00.10.08", "av01.0.04M.08", "hvc1.1.6.L123"];
const accelerations = ["prefer-hardware", "prefer-software"];

const configs = [];
for (const codec of codecs) {
  for (const acceleration of accelerations) {
    configs.push({
      codec,
      hardwareAcceleration: acceleration,
      width: 1280,
      height: 720,
      not_supported_field: 123,
    });
  }
}

for (const config of configs) {
  const support = await VideoEncoder.isConfigSupported(config);
  console.log(
    `support: ${
      support.supported
    }; codec: ${config.codec}(${config.hardwareAcceleration === 'prefer-hardware' ? '硬编' : '软编'});  config ${JSON.stringify(support.config)}`
  );
}

输出示例

support: true; codec: avc1.42E01F(硬编); config {"alpha":"discard","bitrateMode":"variable","codec":"avc1.42001E","hardwareAcceleration":"prefer-hardware","height":720,"latencyMode":"quality","width":1280}
support: true; codec: avc1.42E01F(软编); config {"alpha":"discard","bitrateMode":"variable","codec":"avc1.42001E","hardwareAcceleration":"prefer-software","height":720,"latencyMode":"quality","width":1280}
support: false; codec: vp8(硬编); config {"alpha":"discard","bitrateMode":"variable","codec":"vp8","hardwareAcceleration":"prefer-hardware","height":720,"latencyMode":"quality","width":1280}
support: true; codec: vp8(软编); config {"alpha":"discard","bitrateMode":"variable","codec":"vp8","hardwareAcceleration":"prefer-software","height":720,"latencyMode":"quality","width":1280}
support: false; codec: vp09.00.10.08(硬编); config {"alpha":"discard","bitrateMode":"variable","codec":"vp09.00.10.08","hardwareAcceleration":"prefer-hardware","height":720,"latencyMode":"quality","width":1280}
support: true; codec: vp09.00.10.08(软编); config {"alpha":"discard","bitrateMode":"variable","codec":"vp09.00.10.08","hardwareAcceleration":"prefer-software","height":720,"latencyMode":"quality","width":1280}
support: false; codec: av01.0.04M.08(硬编); config {"alpha":"discard","bitrateMode":"variable","codec":"av01.0.04M.08","hardwareAcceleration":"prefer-hardware","height":720,"latencyMode":"quality","width":1280}
support: true; codec: av01.0.04M.08(软编); config {"alpha":"discard","bitrateMode":"variable","codec":"av01.0.04M.08","hardwareAcceleration":"prefer-software","height":720,"latencyMode":"quality","width":1280}
support: false; codec: hvc1.1.6.L123(硬编); config {"alpha":"discard","bitrateMode":"variable","codec":"hvc1.1.6.L123","hardwareAcceleration":"prefer-hardware","height":720,"latencyMode":"quality","width":1280}
support: false; codec: hvc1.1.6.L123(软编); config {"alpha":"discard","bitrateMode":"variable","codec":"hvc1.1.6.L123","hardwareAcceleration":"prefer-software","height":720,"latencyMode":"quality","width":1280}

解码


const codecs = ["avc1.42E01F", "vp8", "vp09.00.10.08", "av01.0.04M.08", "hvc1.1.6.L123"];
const accelerations = ["prefer-hardware", "prefer-software"];

const configs = [];
for (const codec of codecs) {
  for (const acceleration of accelerations) {
    configs.push({
      codec,
      hardwareAcceleration: acceleration,
      codecWidth: 1280,
      codecHeight: 720,
      not_supported_field: 123,
    });
  }
}

for (const config of configs) {
  const support = await VideoDecoder.isConfigSupported(config);
  console.log(
    `support: ${
      support.supported
    }; codec: ${config.codec}(${config.hardwareAcceleration === 'prefer-hardware' ? '硬解' : '软解'}); config ${JSON.stringify(support.config)}`
  );
}

输出示例

support: true; codec: avc1.42E01F(硬解); config {"codec":"avc1.42001E","hardwareAcceleration":"prefer-hardware"}
support: true; codec: avc1.42E01F(软解); config {"codec":"avc1.42001E","hardwareAcceleration":"prefer-software"}
support: false; codec: vp8(硬解); config {"codec":"vp8","hardwareAcceleration":"prefer-hardware"}
support: true; codec: vp8(软解); config {"codec":"vp8","hardwareAcceleration":"prefer-software"}
support: true; codec: vp09.00.10.08(硬解); config {"codec":"vp09.00.10.08","hardwareAcceleration":"prefer-hardware"}
support: true; codec: vp09.00.10.08(软解); config {"codec":"vp09.00.10.08","hardwareAcceleration":"prefer-software"}
support: false; codec: av01.0.04M.08(硬解); config {"codec":"av01.0.04M.08","hardwareAcceleration":"prefer-hardware"}
support: true; codec: av01.0.04M.08(软解); config {"codec":"av01.0.04M.08","hardwareAcceleration":"prefer-software"}
support: true; codec: hvc1.1.6.L123(硬解); config {"codec":"hvc1.1.6.L123","hardwareAcceleration":"prefer-hardware"}
support: true; codec: hvc1.1.6.L123(软解); config {"codec":"hvc1.1.6.L123","hardwareAcceleration":"prefer-software"}
hughfenghen commented 1 year ago

音频类似:

const codecs = ["mp4a.40.2", "mp3"];
const configs = [];
for (const codec of codecs) {
  configs.push({
    codec,
    sampleRate: 48000,
    numberOfChannels: 1,
    not_supported_field: 123,
  });
}
for (const config of configs) {
  const support = await AudioEncoder.isConfigSupported(config);
  console.log(
    `AudioEncoder's config ${JSON.stringify(support.config)} support: ${
      support.supported
    }`
  );
}

代码来源: https://developer.mozilla.org/en-US/docs/Web/API/AudioEncoder/isConfigSupported_static