I’m building a retiming tool for videos on YouTube and other websites, and finding the frame rate of the video element is necessary for frame-by-frame seeking and rounding reasons.
The way I’m doing it so far is by using requestVideoFrameCallback() and finding the smallest difference between two frame times: (sorry for bad formatting, I’m on mobile)
var v = document.querySelector(“video”)
var c = 0; // time of last frame
var framelength = 1; // length of one frame
var fps;
function check(t, m) {
var diff = Math.abs(m.mediaTime - c); // difference between this frame and the last
if (diff && diff < framelength) {
framelength = diff;
fps = Math.round( 1 / framelength)
}
c = m.mediaTime;
v.requestVideoFrameCallback(check);
}
v.requestVideoFrameCallback(check);
However, there are sometimes rounding issues due to the mediaTime having only 3 decimal places, so is there a better way to find the FPS with JavaScript?