Rotating video clips shot in "portrait" mode

When using my Canon IXUS 40 digital camera to record short videos I sometimes accidently rotate the camera 90 degrees. Of course this isn’t a problem when taking photos since it’s very simple to rotate an image using Photoshop, GIMP, ImageMagick, etc.

The solution might seem less obvious when dealing with video files. Fortunately, there’s this great tool called mencoder (which is part of MPlayer) which let’s us fix things like this:

mencoder -ovc lavc -lavcopts vcodec=mjpeg -vop rotate=1 -oac copy input.avi -o tmp1.avi

This works well for my kind of camera which saves video as MJPEG in 320×240 resolution. The resulting file tmp1.avi will in my case be a 240×320 MJPEG clip.

Now 240×320 is quite an odd aspect ration (3:4). mencoder fix this too:

mencoder -ovc lavc -lavcopts vcodec=mjpeg -vop expand=427:320 -oac copy tmp1.avi -o tmp2.avi
mencoder -ovc lavc -lavcopts vcodec=mjpeg -vop scale=320:240 -oac copy tmp2.avi -o output.avi

The first command will expand the video to 427×320 which is roughly 4:3. The video will be placed in the center and black borders will be added to the left and right sides.

The final command scales the video down to 320×240. Please note that this step lowers the quality of the video. But if you want to playback the video on something like a standalone DVD player you might have to use a standard resolution like this one.

Congratulations! You now have a nice 320×240 video clip.

11 thoughts on “Rotating video clips shot in "portrait" mode”

  1. It turns out that Windows Media Player cannot play MJPEG files produced by mencoder. When playing the mencoder produced file using mplayer I see the following on the console:

    [mjpeg @ 0x86fd3e8]mjpeg: using external huffman table

    which I don’t see when playing the original file. I found a fix for this problem on the MPlayer mailing list which involves running the mencoder produced file through ffmpeg:

    ffmpeg -i output.avi -sameq -acodec pcm_u8 -vcodec mjpeg output_wmp.avi

    // Niklas

  2. I just commented that I didn’t know your OS, but doing everything from a command-line rather than with a mouse (-ovc lavc -lavcopts vcodec=mjpeg -vop rotate=1 -oac copy input.avi -o tmp1.avi) makes it clearer.


  3. Is the rotation lossless?
    Since MJPEG is only a sequence of jpeg images each of which can be lossless rotated, it’s definetely possible.
    But – is this case when using mencoder?


  4. Open in Quicktime pro. ctrl J and select video, and it’s a button there to rotate. Save as self contained and it will not be re-encoded.

  5. I haven’t had a great luck on rotating a video but your tip was quite the solution I’ve been looking for.

    I tried Avidemux and somehow made the rotate but the audio doesn’t sync well.

    Here’s what I’ve used on Ubuntu 8.10 platform:

    mencoder -ovc lavc -lavcopts vcodec=mjpeg -vf rotate=1 -oac pcm tmp1.avi -o tmp2.avi

  6. hi

    I tried the command mencoder -ovc lavc -lavcopts vcodec=mjpeg -vf rotate=1 -oac copy input.flv -o output.avi
    but the audio came out qll scrambled :( ?

  7. Hey Niklas,
    many thanks for your tips! It was exactly what I was looking for!
    I needed to rotate videos taken with my Canon FS100, which provides a resolution of 720×576. Here are my commands for such resolution:

    Rotate a video 720×576 from portrait to landscape mode

    > mencoder -ovc lavc -lavcopts vcodec=mjpeg -vf rotate=1 -oac copy -o
    > mencoder -ovc lavc -lavcopts vcodec=mjpeg -vf expand=1280:720 -oac copy -o
    > mencoder -ovc lavc -lavcopts vcodec=mjpeg -vf scale=720:576 -oac copy -o

    Great help. Thx

  8. Tried using -vop and memcoder error was:
    -vop has been removed, use -vf instead.

    Used -vf instead. Video’s great but audio scrambled.

    How do I install vop?

    MEncoder SVN-r29237-4.4.1
    Ubuntu 9.10
    Dell Optiplex GX260, P4, 1.80GHz

Comments are closed.