How to rotate an AVI or MPEG file taken in Portrait
I've got a little Casio Exilim that I use to make little videos to post on this blog. Sometimes, though, I rotate the camera and end up creating a video that's, well, rotated in portrait, not landscape, and I want to rotate the Video to a vertical orientation.
There's an open source tool called MPlayer out of Hungary that you can download. Get the MPlayer 1.0rc1 Windows package, NOT the Windows GUI package. We're not looking for a Video Player here but rather a utility called MEncoder that comes with the MPlayer package.
This command-line tool is rather obscure, as it seems is the whole culture of video wonks, but I just want to do my work and move on. If you like, however, there's a whole world of Unofficial Packages that sit in front of this obscure tool and do the hard work for you.
Here's the magic I used to rotate my video 90 degrees to the right:
mencoder -vf rotate=1 -o OUTPUT.AVI -oac copy -ovc lavc INPUT.AVI
The -vf is for "Video Filter" and you can chain them, like -vf rotate=1,flip,scale=640:480 if you like. The -oac and -ovc are the audio and video outputs. In this case, I "copy" the audio over, and use the "libavcodecs" for the output. You can do mencoder -ovc help to get a list of choices or read the massive docs.
At this point, I have a really tall 480x640 video, since it used to be at 640x480. Certainly I can leave it there, or I can expand it to a more regular 4:3 ratio. As it's now 640 tall, it'll need to be 853x640 to be 4:3.
mencoder -vop expand=853:640 -oac copy ovc lavc -lavcopts vcodec=mjpeg OUTPUT.AVI -o BIGOUTPUT.AVI
Notice that the portrait is now centered between black bars like a vertical DVD. The aspect ratio is correctish now and we have a vertically oriented video inside a horizontally oriented "container."
I probably (and you as well) should use "raw" as your output format as you move through these steps, as we're making copies of copies here and the quality is getting worse and worse. You'll end up with gigabyte-size temp files, for a bit, but your output will be much better.
Next I'll just scale the video back to 640x480 for posting on the web.
mencoder -ovc lavc -lavcopts vcodec=mjpeg -vop scale=640:480 -oac copy BIGOUTPUT.AVI -o FINAL.AVI
Again, pick the codec that is right for you in order to tune your output size and quality.
UPDATE: Someone emailed saying that the AVI format made by mencoder doesn't always work on Windows Media. (I use VLC Player, works fine) and they suggested the final encoding pass be done by FFMPEG, rather than mencoder:
c:\utils\ffmpeg_mp2.exe" -i finaltest.avi -sameq -acodec pcm_u8 -vcodec mjpeg FFMPEGOUTPUT.AVI
Worked for me.