0
点赞
收藏
分享

微信扫一扫

利用js文件获取视频文件详细信息 如帧速率、总比特率等

无聊到学习 2022-01-14 阅读 21

问题描述:上传的视频文件有些在苹果手机上无法播放,需要要获取详细信息以判断是受哪些关键属性影响的。然而普通的方法没法获取全部的详细信息。
解决方法:引入mediainfo.js,这个js插件可以获取到全部的详细信息。其官方demo演示地址:https://mediainfo.js.org/
GitHub地址:https://github.com/buzz/mediainfo.js
官方例子:

const fileinput = document.getElementById('fileinput')
const output = document.getElementById('output')

const onChangeFile = (mediainfo) => {
  const file = fileinput.files[0]
  if (file) {
    output.value = 'Working…'

    const getSize = () => file.size

    const readChunk = (chunkSize, offset) =>
      new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.onload = (event) => {
          if (event.target.error) {
            reject(event.target.error)
          }
          resolve(new Uint8Array(event.target.result))
        }
        reader.readAsArrayBuffer(file.slice(offset, offset + chunkSize))
      })

    mediainfo
      .analyzeData(getSize, readChunk)
      .then((result) => {
        output.value = result
      })
      .catch((error) => {
        output.value = `An error occured:\n${error.stack}`
      })
  }
}

MediaInfo({ format: 'text' }, (mediainfo) => {
  fileinput.addEventListener('change', () => onChangeFile(mediainfo))
})

浏览器引入方式:

<script type="text/javascript" src="https://unpkg.com/mediainfo.js/dist/mediainfo.min.js"></script>

或者把mediainfo.min.js下载之后上传到自己服务器的地址。

经测试这个插件还是非常好用的。

举报

相关推荐

0 条评论