Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to step one frame forward and one frame backward in video playback?

Tags:

html5-video

I need to search for some special features/patterns that might be visible in only one (or two) of many frames. The frame rate can be as slow as 25 frames per second and the video may contain over 7500 frames. I often start by scanning the video at high speed looking for a segment where I might find the feature, then rewind. I repeat this procedure while gradually reducing the playback speed, until I find a fairly small time window in which I can expect to find the feature (if it is present). I would then like to step forward and backward by single frames using key hit events (e.g. right arrow and left arrow keys) to find the feature of interest. I have managed to use HTML5 with JavaScript to control the forward speed; but, still do not know how to use the keyboard for single frame stepping forward and backward through a video. How can this be accomplished? Note, my web browser is Firefox 26 running on a Windows 7 platform.

like image 289
user2989402 Avatar asked Dec 02 '13 20:12

user2989402


People also ask

How do I advance video one frame at a time?

There are two very useful keyboard shortcuts for watching YouTube videos which almost never mentioned. They are the comma and the full stop. Both work when you have paused a video (which you can do with the pause button or by pressing Space or K). The first (,) nudges you back a frame in the video.

How do I move one forward frame in VLC?

To use a hotkey to play your video frame by frame, then first, open your video file with VLC. When the video opens, on your keyboard, press the E key. If your video is playing, VLC will pause it and let you move one frame at a time. Keep hitting E to move frame by frame in your video.


1 Answers

You can seek to any time in the video by setting the currentTime property. Something like this:

var video = document.getElementById('video'), frameTime = 1 / 25; //assume 25 fps

window.addEventListener('keypress', function (evt) {
    if (video.paused) { //or you can force it to pause here
        if (evt.keyCode === 37) { //left arrow
            //one frame back
            video.currentTime = Math.max(0, video.currentTime - frameTime);
        } else if (evt.keyCode === 39) { //right arrow
            //one frame forward
            //Don't go past the end, otherwise you may get an error
            video.currentTime = Math.min(video.duration, video.currentTime + frameTime);
        }
    }        
});

Just a couple things you need to be aware of, though they shouldn't cause you too much trouble:

  1. There is no way to detect the frame rate, so you have to either hard-code it or list it in some lookup table or guess.

  2. Seeking may take a few milliseconds or more and does not happen synchronously. The browser needs some time to load the video from the network (if it's not already loaded) and to decode the frame. If you need to know when seeking is done, you can listen for the 'seeked' event.

like image 65
brianchirls Avatar answered Oct 12 '22 23:10

brianchirls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!