Monday, March 30, 2009

sdrawkcaB oediV gniyalP

I had a video that someone made where they thought they were being artistic and reversed it so it was playing backwards. I had this itching to see it forward, and set out on a quest to figure out how to play video backwards under Linux with mplayer. Here's what I did:
  1. Create an empty directory, and change to it
  2. Use the following command to convert all the frames in the movie to individual JPEG files:
    $ mplayer -vo jpeg:quality=100 \
    /path/to/video/backwards.avi

    You will end up with a directory full of files like 00000001.jpg. Each one of these files is a single frame of the video you are attempting to reverse.
  3. Now create a subdirectory called TARG - this will contain the same JPEG files linked in reverse order. Do not change into it just yet...
  4. Create the following silly perl script (substitute the $a=2800 for the number of the last file in your directory):
    #!/usr/bin/perl

    $a = 2800;
    $b = 1;

    while ($a > 0) {
    $sa = sprintf "%08d.jpg", $a;
    $sb = sprintf "%08d.jpg", $b;

    link("$sa", "TARG/$sb");
    $a = $a - 1;
    $b = $b + 1;
    }

    exit 0;
  5. Now change into the TARG directory. There will be a list of files like before, but they will be named in reverse order. Use the following command to reassemble them:
    $ mencoder "mf://*.jpg" -mf fps=12 \
    -o /path/to/new/movie/rev_movie.avi \
    -ovc lavc -lavcopts vcodec=mpeg4

    You will likely need to adjust the fps (frames per second) - this may take some trial-and-error if you don't know anything about the original movie. You can try this out (without doing the encoding) by using:
    $ mplayer "mf://*.jpg" -mf fps=12
  6. Enjoy. You can wipe out all those frames now...

4 comments:

Ish Kumar said...

I had similar situation, and your blog post helped me out.

Thanks You VVM!

Lohith said...

I am reading this so late, after 3 years of its posting. I actually need this now. But can this be done for Android platform? (using Windows OS computer)

fenn said...

i had to do this to make an animated gif. this way is simpler:

mplayer foo.avi -vo png
ls -r *.png >> filelist
mplayer mf://@filelist

you can also use avxsynth

Ben in Seattle said...

Still a useful tool after all these years. Here's a way to reverse the audio and play it back as well:

  mplayer -ao pcm -vo jpeg foo.avi
  sox audiodump.wav sdrawkcab.wav reverse
  ls -r *jpg > filelist
  mplayer mf://@filelist -audiofile sdrawkcab.wav -fps 24


If you're not sure, you can find out the original file's FPS (frames per second) like so,

  mplayer -identify -endpos 0 foo.avi | grep FPS

which will tell you something like,
ID_VIDEO_FPS=24.000