使用mp4box校验文件视频文件解码类型
2022-03-08  本文已影响0人 
羊驼626
场景:由于浏览器标签video不支持播放hevc编码格式的MP4文件,故在上传文件时,通过校验视频文件类型,过滤hevc编码格式的视频文件
1.使用mp4box
https://github.com/gpac/gpac/wiki/MP4Box
2.应用
<input type="file" @change="fileChange">
import MP4Box from 'mp4box'
async fileChange(e) {
  console.log(e);
  let result;
  await this.checkVideoCode(e.target.files[0]).then((res) => {
    result = res;
    console.log(result); // 其他格式需要实际实验判断
  });
  if (result.mime.indexOf("hvc") !== -1) {
    console.log("格式不支持");
  }
},
checkVideoCode(file) {
  return new Promise((resolve, reject) => {
    const mp4boxFile = MP4Box.createFile();
    const reader = new FileReader();
    reader.readAsArrayBuffer(file);
    reader.onload = function (e) {
      const arrayBuffer = e.target.result;
      arrayBuffer.fileStart = 0;
      mp4boxFile.appendBuffer(arrayBuffer);
    };
    mp4boxFile.onReady = function (info) {
      resolve(info);
    };
    mp4boxFile.onError = function (info) {
      reject(info);
    };
  });
},


