play a video in reverse using HTML5 video element

谁说我不能喝 提交于 2019-11-28 14:09:01

This is an old thread so I'm not sure how useful the following will be.

The Mozilla developer notes point out that:

Negative values [for playbackRate] don't currently play the media backwards.

Same goes for Chrome, but Safari on Mac works.

This page from w3c is great at observing and understanding events:

http://www.w3.org/2010/05/video/mediaevents.html

I took the jsfiddle you mentioned and added some extra controls for fast forward/rewind speeds:

http://jsfiddle.net/UkMV2/5/

However, though this works fine for me with Chrome/Firefox/Safari, I should point out that it doesn't really address your key questions.

Firstly, the approach assumes that negative playback rates don't work (which at the time I write this, is largely true AFAICS). Instead, it fakes it by calculating and setting the current time in the video:

function rewind(rewindSpeed) {    
   clearInterval(intervalRewind);
   var startSystemTime = new Date().getTime();
   var startVideoTime = video.currentTime;

   intervalRewind = setInterval(function(){
       video.playbackRate = 1.0;
       if(video.currentTime == 0){
           clearInterval(intervalRewind);
           video.pause();
       } else {
           var elapsed = new Date().getTime()-startSystemTime;
           log.textContent='Rewind Elapsed: '+elapsed.toFixed(3);
           video.currentTime = Math.max(startVideoTime - elapsed*rewindSpeed/1000.0, 0);
       }
   }, 30);
}

Chrome handles this quite seamlessly, even playing snippets of audio as it goes.

Secondly, I think you want to play the video in reverse as soon as the page loads. I can't imagine a use case for this, but the first issue I see is that the whole video will need to be downloaded prior to playback, so you'll need to wait - but download probably won't happen until you start playing. So you could set the currentTime to near the end and wait for the canPlay event, and then start playing in reverse. Even then, this seems very awkward.

I think there are these broad options:

1) Use a native video widget rather than HTML. I'm guessing (without checking) that the native API supports reverse playback.

2) Generate a proper reversed video and play it as normal. For example, on a server somewhere use a program like fmpeg to reverse the video. Then your app downloads the video and plays it normally, which looks to the user like reverse.

Assuming it really does make sense to have an application that plays a video in reverse when you load it, then I'd personally go for #2.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!