Niklas blog

Just another WordPress weblog

Rotating video clips shot in "portrait" mode

July 28th, 2006 in Uncategorized

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.

6 Responses to “Rotating video clips shot in "portrait" mode”

  • niklas
    July 30th, 2006 at 14:00

    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

  • jonny
    August 30th, 2006 at 20:25

    i have recored video’s from my mobile (samsung D600) in vertical and i what to change them to play horizontal. can someone help me plz, email me on jonathonmcallister2@hotmail.com thanks

  • gene
    February 2nd, 2007 at 21:38

    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.

    Gene
    wikidpad.com

  • Adam Dempsey
    August 23rd, 2007 at 14:37

    Thanks :D That was exactly what I needed!

  • Hartmut
    November 21st, 2007 at 13:18

    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?

    Hartmut

  • Jonar
    August 28th, 2008 at 20:45

    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.

Leave a Reply