4

I am working on a functionality explained below- Once a video is loaded/with a button click, it has to play in reverse. The target devices are smart phones and tablet devices. I am trying to do this using HTML5 tag and read about the playBackRate attribute. Further down, I am using the HTML5 stuff to generate my mobile apps using Phonegap.

Here's a link I found interesting but partially working - http://www.w3.org/2010/05/video/mediaevents.html

The playbackrate, when changed doesn't do anything except it pushes the video back, by whatever number is given there.

I read it here that when playBackRate is set to -1, the reverse playback should happen.

I am not clear on how to exactly implement this. Does the playbackrate actually do this reversing? or should I opt on doing something else?

NEW LINE HERE: I am almost there.. found a working example here. But this is not working when I am trying it on my system. I am not sure where I am making it wrong.

Community
  • 1
  • 1
Lohith Korupolu
  • 1,066
  • 1
  • 18
  • 52
  • any help here please? am in a great need of this functionality – Lohith Korupolu Aug 05 '13 at 12:18
  • That link works for me on latest Chrome. What browser are you using? Do you mean it won't work on your android/cordova app? – MBillau Aug 05 '13 at 17:59
  • @MBillau - thanks for your reply. Yes, I tried it on Google Chrome. Its version is "Version 28.0.1500.95 m". And, have you ever tried on with android/cordova? – Lohith Korupolu Aug 06 '13 at 05:31
  • Hi, no I have not had a change to try it on android cordova yet but will hopefully be able to test it today. I'm a little concerned because Cordova Android uses an "old" browser, (the WebView) that has known limitations with some fancy things (see on this site people having problems with some more advanced browser features.) Maybe this is one of those limitations, but I'm not sure. – MBillau Aug 06 '13 at 12:34
  • @MBillau - I got the jsfiddle example working on my browser and on an android device as well (using cordova). But the reverse play distorts a little. It is not smooth. Also, is there a chance to play my video in reverse directly when the screen loads? At this moment, we can rewind/reverse only after the video plays for a while (few seconds). – Lohith Korupolu Aug 07 '13 at 06:17
  • @MBillau - any input from you helps me move further on this. I would like to know if the reverse playing can be done on screen load. Kindly help :) – Lohith Korupolu Aug 07 '13 at 09:09

1 Answers1

14

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/uvLgbqoa/

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 ffmpeg 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.

Andrew E
  • 7,697
  • 3
  • 42
  • 38
  • just to add on this answer, some may go on a wild goose chase like i did. the provided function (and fiddle) works well, knowing that your video FPS (frames per second) must be smooth as well. in most cases, 30fps and plus. of course, it doesn't mean it's not working when u try it on a smaller fps although it may seem like it – Amjo Apr 18 '17 at 11:33
  • 1
    To reverse video: `ffmpeg -i video.mp4 -vf reverse reversed.mp4` or to reverse video with audio: `ffmpeg -i video.mp4 -vf reverse -af areverse reversed.mp4` – ashleedawg Jan 07 '23 at 10:01