event.target.files[0] in .t" /> event.target.files[0] in .t" /> event.target.files[0] in .t"/>

How can I get the audio or video duration of selected file in angular form

1.7k Views Asked by At

In my angular form, I get my video or audio files by

(change)="fileSelected($event)

in .html form and

attachedFile = <File>event.target.files[0]

in .ts file that has name, type and size. How can I get audio/video duration time?

2

There are 2 best solutions below

0
Akil Makda On

Try below code,

    attachedFile.addEventListener('timeupdate', (event) => {
       // Your code.
    });
8
Umesh Patadiya On

You can create an audio or video element dynamically based on selected file type and then can get other info. Here is the code...

fileSelected($event) {
  attachedFile = <File>event.target.files[0]

  let duration:any;

  //here you can check the file type for attachedFile either video or audio

  var video = document.createElement('video');
  video.preload = 'metadata';

  video.onloadedmetadata = function() {
    window.URL.revokeObjectURL(video.src);
    duration = video.duration; // here you could get the duration
  }

  video.src = URL.createObjectURL(attachedFile);
}