Scott Hanselman

How to rotate an AVI or MPEG file taken in Portrait

February 11, 2007 Comment on this post [7] Posted in Musings | Tools
Sponsored By

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.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
February 12, 2007 2:05
I usually use a simple AviSynth script to rotate my video:

I open the .avs file in VirtualDub, and save it as a new AVI. I'm sure you can expand it to be 4:3 and resize back to 640x480 with the AviSynth script but I'm usually just uploading the video to Google Video, so I don't bother with that.
February 12, 2007 20:36 ya'know that Windows Movie Maker (that tool that comes free with WindowsXP) has a rotate clip command?
February 12, 2007 21:57
James - Of course, but its not *nearly* as flexible in its choice of codecs, filters, etc...
March 11, 2007 12:03
Hi Scott,
Took videos of a wedding with my Olympus digital camera.
Turned camera,to get full body shots of everyone. They're beautiful,
but I get a crick in my neck when I view them. Have downloaded
various programs which are meant to rotate them, but to no avail.
Need a simple, easy to understand program I can download. Don't want
to have to buy any extra programs to assist with the video rotation,
because I'll never again turn my camera when I do videos. I have Adobe
Photoshop Elements 5.0 and Adobe Premiere Elements 3.0., and updated
Windows Media Player. Can you suggest an easy to use program I can download?
Thank you for your time and attention.
Sincerely, Marilynn
March 12, 2007 13:30
Marilynn - I think you'd probably do best to use Windows Movie Maker.
April 11, 2007 2:50
Your advice was useful but I have had some problems. After the expand command the video shows lots of artifacts that do not go away after the scale command. It varries across videos but generally looks like green mush. Is there a way to expand the video without artifacts that obscure the video? I understand to expect a reduction of quality but this goes way beyond that.
April 11, 2007 2:52
Craig - I lowered the quality to save bandwidth. My local copies look fine after rotation. There's some "copy of a copy" expense if you rotate an already lossy format, but that's easily fixed by just rotating the UNCOMPRESSED video.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.