如何获取mp4的编解码器哑剧信息?



如何在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);
}

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

在许多其他功能中,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();
} );
<script src="https://gpac.github.io/mp4box.js/dist/mp4box.all.js"></script>

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

最新更新