question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting the duration of an audio file on drop

See original GitHub issue

Is it possible to get the duration of audio files on drop? I’m wanting to do something like this

  onDrop(files) {
    files.forEach((file) => {
       console.log(file.duration);
    })
  }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

6reactions
malimccallacommented, Mar 6, 2017

Anyone else who might want to do this I solved it by doing the following

  onDrop(files) {
    files.forEach((file) => {
    const audio = document.createElement('AUDIO');
    audio.src = file.preview;
    audio.addEventListener('loadedmetadata', () => {
       console.log(audio.duration);
    });
  }

Not sure if this is the right way to do it but it works for what I’m doing

1reaction
seanmaischcommented, Jun 16, 2022

@muhilham @kumar88kanishk @pbirsinger

Revisiting this old topic because I was searching for the same solution. I was able to gather the video duration by using this code. In my scenario, I am allowing for a single video upload, hence the file[0] array selection. Hope someone finds this helpful in the future!

onDrop: (file) => {
	Object.assign(file[0], {
		preview: URL.createObjectURL(file[0])
	});

	const video = document.createElement("video");
	video.src = file[0].preview;

	video.addEventListener("loadedmetadata", () => {
		console.log(video.duration);
	});
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get the duration of audio in Python?
Use: It returns audio length in seconds. The value returned is in float(by default).
Read more >
How to get the correct duration from an audio file with large ...
I want to drag'n'drop local files to receive file info like duration, bitrate and file size. I only managed to receive the file...
Read more >
Audio Duration Calculator
This utility calculates the duration of audio files (both uncompressed, PCM/IEEE FP audio, such as .WAV, .W64 .AIFF/.AIF and also compressed files such...
Read more >
HTML DOM Audio duration Property
The duration property returns the length of an audio, in seconds. Note: Different browsers return different values. In the example above, Opera 18+...
Read more >
How to play video and audio files on Dropbox
Play video or audio files on dropbox.com or the Dropbox mobile app. Share your videos, or open a video or audio file someone...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found