I have not found an API to get the duration of a video, does anyone know that?
1 Answer 1
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.
Sign up to request clarification or add additional context in comments.
4 Comments
Marc
const duration = metadata.format.duration; TypeError: Cannot read properties of undefined (reading 'format')
Chris
@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.
NotSimon
What's the reason for this returning
N/A? All other data in the metadata is returned successfully, so I assume ffprobe is installed properlyChris
@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.
lang-js