Get video duration when input a video file

前端 未结 3 637
忘掉有多难
忘掉有多难 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:12

    I needed to validate a single file before continuing to execute more code, here is my method with the help of Kaiido's answer!

    onchange event when a user uploads a file:

    $("input[type=file]").on("change", function(e) {
    
        var file = this.files[0]; // Get uploaded file
    
        validateFile(file) // Validate Duration
    
        e.target.value = ''; // Clear value to allow new uploads
    })
    

    Now validate duration:

    function validateFile(file) {
    
        var video = document.createElement('video');
        video.preload = 'metadata';
    
        video.onloadedmetadata = function() {
    
            window.URL.revokeObjectURL(video.src);
    
            if (video.duration < 1) {
    
                console.log("Invalid Video! video is less than 1 second");
                return;
            }
    
            methodToCallIfValid();
        }
    
        video.src = URL.createObjectURL(file);
    }
    
    0 讨论(0)
  • 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';
      }
    }
    <div id="input-upload-file" class="box-shadow">
      <span>upload! (ღ˘⌣˘ღ)</span>
      <input type="file" class="upload" id="fileUp" name="fileUpload">
    </div>
    <pre id="infos"></pre>

    0 讨论(0)
  • 2020-11-27 03:34

    Here is async/await Promise version:

    const loadVideo = file => new Promise((resolve, reject) => {
        try {
            let video = document.createElement('video')
            video.preload = 'metadata'
    
            video.onloadedmetadata = function () {
                resolve(this)
            }
    
            video.onerror = function () {
                reject("Invalid video. Please select a video file.")
            }
    
            video.src = window.URL.createObjectURL(file)
        } catch (e) {
            reject(e)
        }
    })
    

    Can be used as follows:

    const video = await loadVideo(e.currentTarget.files[0])
    console.log(video.duration)
    
    0 讨论(0)
提交回复
热议问题