【问题标题】:How to get mp4's codec mime information?如何获取 mp4 的编解码器 mime 信息?
【发布时间】:2021-03-17 16:23:52
【问题描述】:

如何在 JavaScript 中获取 MP4 的 codec 信息,以便检查浏览器是否支持?

const audioCodec = '';//<---?? exmaple: "mp4a.40.2"
const videoCodec = '';//<---?? example: "avc1.42e01e"
const video = document.getElementById('video');
const mimeCodec = 'video/mp4; codecs="' + audioCodec + ', ' + videoCodec + '"';

if (!('MediaSource' in window) || !MediaSource.isTypeSupported(mimeCodec)) {
    console.error('Unsupported MIME type or codec: ', mimeCodec);
}

【问题讨论】:

  • 为什么需要“在 javascript 中”获取它?该文件来自哪里?
  • 用户上传的
  • 那你为什么要使用 MediaSource 呢?您不能直接将文件传递给视频元素吗?无论如何,您可能不会有自适应内容。

标签: javascript video html5-video mime-types codec


【解决方案1】:

我们没有内置的方法可以访问,因此您必须自己解析文件,或者使用库来这样做。

在许多其他功能中,MP4Box.js 可以为 mp4 文件执行此操作。

const mp4boxfile = MP4Box.createFile();
mp4boxfile.onError = console.error;
mp4boxfile.onReady = (info) => {
  console.log( info.tracks.map( track => track.codec ) );
};
fetch( "https://dl.dropboxusercontent.com/s/hyeredbcn60feei/BigChunksBunny1.mp4" ).then( (resp) => resp.arrayBuffer() )
  .then( (buf) => {
    buf.fileStart = 0;
    mp4boxfile.appendBuffer( buf );
    mp4boxfile.flush();
  } );
&lt;script src="https://gpac.github.io/mp4box.js/dist/mp4box.all.js"&gt;&lt;/script&gt;

他们显然也有一个“简单”的构建,可能足以满足您的用例。

【讨论】:

  • 是的,我试过 mp4box,但它加载了错误,不支持更大的视频(250MB 以上的视频不会加载)。
  • @orangeMint 你可以传递更大的视频块const buf = await file.slice(start, start+chunk_size).arrayBuffer(); buf. fileStart = start; mp4boxfile.appendBuffer( buf );
  • 我会试一试,然后告诉你进展如何:)
猜你喜欢
  • 2019-11-09
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多