bbc / VideoContext

An experimental HTML5 & WebGL video composition and rendering API.
http://bbc.github.io/VideoContext/
Apache License 2.0
1.33k stars 157 forks source link

Original duration of video #77

Closed ZulfadhliM closed 6 years ago

ZulfadhliM commented 6 years ago

Is there a way to get the original length of the video instead of "Infinity"? Lets say the length of the video is 10 seconds. Code example as below:

var canvas = document.getElementById("canvas");
var videoCtx = new VideoContext(canvas);
var videoNode = videoCtx.video('./movie.mp4');
videoNode.connect(videoCtx.destination);
videoNode.start(0);
videoCtx.play();
console.log(videoNode.duration); // outputs: Infinity
console.log(videoNode); // outputs: duration: 10
PTaylour commented 6 years ago

Looking at the code:

The duration getter calculates duration from the internal _stopTime. Until _stopTime is known it returns Infinity.

_stopTime is set on loading the videoNode if the element has readyState > 3 (which is HAVE_ENOUGH_DATA)

https://github.com/bbc/VideoContext/blob/cc83357fc5ee62d30b97c5ce3eade829a46daf68/src/SourceNodes/videonode.js#L73

So,videoNode.duration, as things stand, will only give you the duration once the node has loaded.

When you do console.log(videoNode); // outputs: duration: 10, are you reading the duration from the underlying element?

If you read videoNode.duration on the next tick (eg after requestAnimationFrame), does it still give you Infinity?

ZulfadhliM commented 6 years ago

Thanks! I used setInterval() to fetch the duration until it is ready.