Get video duration when input a video file

前端 未结 3 662
忘掉有多难
忘掉有多难 2020-11-27 02:37

I\'m doing a project with HTML and Javascript that will run local with local files. I need to select a file by input, get its information and then decide if I\'ll add to my

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 03:32

    In modern browsers, You can use the URL API's URL.createObjectURL() with an non appended video element to load the content of your file.

    var myVideos = [];
    
    window.URL = window.URL || window.webkitURL;
    
    document.getElementById('fileUp').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);
        var duration = video.duration;
        myVideos[myVideos.length - 1].duration = duration;
        updateInfos();
      }
    
      video.src = URL.createObjectURL(files[0]);;
    }
    
    
    function updateInfos() {
      var infos = document.getElementById('infos');
      infos.textContent = "";
      for (var i = 0; i < myVideos.length; i++) {
        infos.textContent += myVideos[i].name + " duration: " + myVideos[i].duration + '\n';
      }
    }
    upload! (ღ˘⌣˘ღ)

提交回复
热议问题