A
A
Andrey Ivanov2020-05-01 09:27:33
JavaScript
Andrey Ivanov, 2020-05-01 09:27:33

How to calculate the length of a video file?

Wrote a script that would calculate the duration of the video file.
Right now, the duration of the video is calculated only for .mp4 and .mov files, and for other video formats, for example, flv or avi is not calculated.
And even if the duration of the video is calculated in .mp4, then this is only for small files, if you upload a video file of 1.5GB, the duration of the video clip will not be calculated for .mp4

Sandbox

const $uploadFile = document.querySelector('.uploadFile')

// video tag creation 
const $video = document.createElement('video')
const $source = document.createElement('source')


$uploadFile.addEventListener('change', (event) => {
  const inputFile = event.target.files[0]
  
  if (inputFile) {
    $video.preload = 'metadata';
    
    $video.onloadedmetadata = function() {
      window.URL.revokeObjectURL($video.src)
          
      inputFile.duration = $video.duration
    }    
  }
  
  $video.src = URL.createObjectURL(inputFile)
  
  onChageFile(inputFile)
})

const onChageFile = (inputFile) => {
  console.log(inputFile)
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question