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

0 comments: