Linux Blog

Rotating Videos in Linux

Filed under: General Linux,Linux Software — TheLinuxBlog.com at 11:17 pm on Friday, August 24, 2007

Like many people I receive video media from family members and friends on line. Sometimes for I receive videos that are shot in portrait mode instead of landscape. I used to find this quite annoying until I figured out how to rotate a video under Linux. All that is needed to rotate the video is mencoder. It comes bundled with the king of media players MPlayer.

Command to rotate a video is:

mencoder -ovc lavc -vop rotate=1 -oac copy input.mpg -o output.mpg

The rotate=1 can be replaced with whatever option best suits your needs. Rotating video options are below:

0    Rotate by 90 degrees clockwise and flip (default).
1    Rotate by 90 degrees clockwise.
2    Rotate by 90 degrees counterclockwise.
3    Rotate by 90 degrees counterclockwise and flip.

I have broken down the command and options below for those that are interested.

-ovc Output Video Codec. This is what codec mencoder should use when creating the video. The command above uses the libavcodec. This is known for quality. “mencoder -ovc help” will display all of the video codecs available

-vop still works but has been replaced with -vf. It is used to setup a chain of video filters in our case it is used to rotate each frame. See the above table to find out what rotate mode you need. Up-side-down videos can be rotated by doing rotate=1 twice.

-oac Output Audio Codec. If you would like to specify an audio codec to use this is where it should be done. “mencoder -oac help” will show all of the available audio codecs. Choosing an audio time adds encoding time but it can greatly reduce or increase file size. I use the copy codec to copy the exact sound from the original first. Then if I wish to reduce file size I can reduce the quality or change the codec afterwards.

input.mpg is the input file and will have to be changed for the file that you wish to rotate. The -o option is used to specify the output file. This must not be left out or mencoder will give an error and your file will not be written.

Now that your done reading all about how to rotate a video with Linux you can give it a try. By having a command line application rotating a whole directory of images can be done in minimal time without human interaction. Try rotating a hundred movies without user interaction in Windows Movie Maker!





Random Man Pages:
aiptek
sha256sum
wavelan
nsc

23 Comments »

Comment by Dan D

February 1, 2009 @ 11:44 pm

Super handy! Thanks! New version of mencoder uses -vf instead of -vop

Comment by Walyonaire

April 1, 2009 @ 11:50 am

“Try rotating a hundred movies without user interaction in Windows Movie Maker!”
Oh My God!!! I’d rather go directly to hell.

Comment by TheLinuxBlog.com

April 1, 2009 @ 12:51 pm

@WALYONAIRE And not pass go? Is it even possible to script windows movie maker?

Comment by ahmadinejad

July 4, 2010 @ 10:46 pm

Thanks dude!! It works for me and it’s very handy.

Comment by will

September 6, 2010 @ 3:02 am

Super helpful, thanks for the info! ;)

Comment by glyn

October 5, 2010 @ 7:51 pm

This works great! Needed to rotate videos I had taken with mobile and it worked perfectly.

Many thanks.

Comment by Rob

January 17, 2011 @ 11:42 pm

Mucho Gracias!

Comment by Max

February 12, 2011 @ 10:44 am

so far so good but it can’t find the file. i took the file out of its folder in the external hdd and put it on the desktop (i have no idea why that would have helped but whatever) but it says: File Not Found: ‘VIDEO0025.3gp’

is there something else i should type so that it knows where to look for the file or do i need to put that file in a certain spot?

by the way im newer to Linux, just built my first computer back in October and started off with this newest version of Ubuntu.

Comment by TheLinuxBlog.com

February 14, 2011 @ 10:13 am

@Max Either try to use ~/Desktop/VIDEO0025.3gp or from terminal type cd Desktop and see if that works.

Comment by Pontios

April 17, 2011 @ 8:30 am

works like a charm! Thanks THELINUXBLOG for posting this! I tried rotating video recorded with GalaxyS and it uses the following audio codec “Codec: MPEG AAC Audio (mp4a)” so my mencoder errored with:
Audio format 0x4134504d is incompatible with '-oac copy', please try '-oac pcm' instead or use '-fafmttag' to override it.
In the end I used ‘-oac mp3lame’ option, because ‘-oac pcm’ was make the output file quite large.
So my final command looks like this (hope it helps somebody):
mencoder -ovc lavc -vf rotate=1 -oac mp3lame input.mpg -o output.mpg

Comment by Mick

June 10, 2011 @ 4:11 am

Well I tried this on a 3gp file.
When it played back it was like watching a Charlie Chaplin Movie with quick jerky motion and the sound had disappeared.

Comment by TheLinuxBlog.com

June 10, 2011 @ 8:07 am

@Mick my guess is the codecs need to be changed to anti-charli-chaplin mode.

Pingback by Rotating Video from iPhone « Linux and Such

July 6, 2011 @ 11:45 am

[...] B.S., Apple, just rotate the video on the phone!) I tried the mencoder-based technique described here, but it was only a modification of the ffmpeg-based technique I learned about here that actually [...]

Comment by coldestlivewire

August 7, 2011 @ 8:55 am

had to use it three times i could have replaced the 1 with a 2

Comment by mutante

August 30, 2011 @ 12:14 am

got “-vop has been removed, use -vf instead.”
and “Audio format 0x4134504d is incompatible with ‘-oac copy’, please try ‘-oac pcm’ instead or use ‘-fafmttag’ to override it.”

for a .3gp video in my case needed to change it to:

mencoder -ovc lavc -vf rotate=1 -oac pcm VIDEO0039.3gp -o VIDEO0039_rotated.3gp

Comment by ron

September 18, 2011 @ 11:21 pm

How to preserve the quality when rotating iPhone videos. I used this command but the quality of the original video is far better than the rotated one:
mencoder -ovc lavc -vf rotate=1 -oac pcm input.mpg -o output.mpg

Comment by Steve

September 24, 2011 @ 5:47 pm

Fantastic :)
Pontios you help fix my issue, thanks to you too :)

Comment by EccentricIntelligenceAgency

November 14, 2011 @ 1:29 pm

Thanks for taking the time for this. I just filmed part of a protest, and was holding my camera sideways. I got home and had to tilt my head, and now it’s set straight. Long live Linux and those like your self!

Comment by Bogdan

December 5, 2011 @ 7:56 am

Thanks, it works great!!!

Comment by Isaac

February 24, 2012 @ 10:45 am

Amazing!! Works soooooo well thanks!

Comment by cwchia

July 24, 2012 @ 5:46 am

mencoder -ovc lavc -vf rotate=1 -oac pcm input.3gp -o output.3gp

Comment by cwchia

July 24, 2012 @ 5:47 am

the above is for 3gp files

Comment by Laura

September 1, 2012 @ 2:46 pm

thx @ CWCHIA, worked a treat!

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>