1

I have not found an API to get the duration of a video, does anyone know that?

asked Oct 7, 2017 at 23:29

1 Answer 1

3

Using ffprobe via fluent ffmpeg is perfect for this:

const ffmpeg = require('fluent-ffmpeg');
ffmpeg.ffprobe(videoFile, (error, metadata) => {
 const duration = metadata.format?.duration;
 console.log(duration);
});

Just make sure you've installed ffprobe, including it in your ffmpeg build.

The fluent ffmpeg docs also outline an approach using ffprobe as a method as part of your encoding function, but if you're doing that you need to make sure you're not streaming your source in, as it will be consumed by the ffprobe process, so for streaming I would recommend simply dedicating a few lines to ffprobe to obtain the duration, as above.

answered Oct 7, 2018 at 17:14
Sign up to request clarification or add additional context in comments.

4 Comments

const duration = metadata.format.duration; TypeError: Cannot read properties of undefined (reading 'format')
@Marc Was ffprobe definitely installed? Can always log out the entire metadata object to see what you get. And could check if there's an error returned too, before you try to parse the metadata object.
What's the reason for this returning N/A? All other data in the metadata is returned successfully, so I assume ffprobe is installed properly
@NotSimon For the duration? Could be that the metadata is not available for that particular encoding format, as is often the case with streamed formats.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.