How to get mp4's codec mime information?

和自甴很熟 提交于 2020-12-13 03:51:33

问题


How can I get a MP4's codec information in JavaScript so that I can check if the browser supports it?

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);
}


回答1:


There is no built in way accessible to us, so you'd have to parse the files yourself, or use a library to do so.

Among a lot of other features, MP4Box.js can do this for mp4 files.

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>

They apparently also have a "simple" build which may be enough for your use case.



来源:https://stackoverflow.com/questions/65163495/how-to-get-mp4s-codec-mime-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!