js获取上传视频播放时间

https://stackoverflow.com/questions/29285056/get-video-duration-when-input-a-video-file

  <body>
    <input
      type="file"
      name="asd"
      id="a"
      accept="image/png, image/jpeg, video/*"
      multiple="multiple"
    />
    <script>
      const l = console.log;
      var myVideos = [];
      document.querySelector("#a").onchange = setFileInfo;

      function setFileInfo() {
        var files = this.files;
        myVideos.push(files[0]);
        var video = document.createElement("video");
        video.preload = "metadata";

        video.onloadedmetadata = function() {
          window.URL.revokeObjectURL(video.src);
          l( video.duration )
          var duration = video.duration;
          myVideos[myVideos.length - 1].duration = duration;
          updateInfos();
        };

        video.src = URL.createObjectURL(files[0]);
      }

      function updateInfos() {
        for (var i = 0; i < myVideos.length; i++) {
            l(myVideos[i].name, myVideos[i].duration)
        }
      }
    </script>
  </body>
posted @ 2018-11-14 19:46  Ajanuw  阅读(606)  评论(0编辑  收藏  举报