video rotation

Finally, true video rotation on the iPhone!

October 27, 2013

The "Video Rotate" app from Gadget Juice is just what I've been looking for!  It allows you to do a frame-by-frame reencode of movies on your iPhone adjusting for a specified rotation.  This is useful if you accidently switch rotations during recording, but is also useful for rencoding the video so it can be played back in VLC and other non-Apple players with the proper rotation.


When you first open the app it will ask for permission to access your photo library.  Once you grant that access, you can see your recorded videos in a list.  Tap on the video you wish to modify.


The app detected that my video already had a meta tag saying the rotation should be 90°.  Even though it was already selected, I tapped on 90° and was prompted to choose a rotation mode.


Choose "Slow" mode to re-encode your video.


On my iPhone 5s I was averaging around mid-fifties for re-encoding framerate.  Not bad!  My wife's iPhone averages in the mid-thirties.  This sort of heavy lifting really shows how a 64 bit architecture improves performance.


Once done your new movie (complete with a new filename) is added back into your library.


My original movie was 27.3 MB.  After the reencoding it was 34.7MB.  I didn't notice any degradation in quality.  Keep in mind at this point it is still a 1080p video.  I like to save my files in 720p to save on storage space.  I shared the new movie with my mac via iMessage (send an iMessage to yourself from the iPhone with the Messages app open on the mac) and it came across as a 3.7 MB 720p video.  I haven't spent much time comparing the quality of the iMessage encode to that of Handbrake, but my guess is handbrake is much better so I will continue to use it.  By the way, did you know the Handbrake app can do batch?  Just add an entire folder to Handbrake, it will scan each movie as a separate title.  In the File menu you'll see there is a new "Add All Titles To Queue" option :-D

iPhone video rotation (and compression)

January 13, 2012

I have found two problems with video recorded from my iPhone 4s.

  1. File sizes are very large.

The iPhone records H.264 at 1920x1080 with a bitrate of 24.0Mbps.  A one minute video is 180MB.

  1. Video playback is sometimes upside down or rotated 90 degrees CW or CCW.

The iPhone records all video with the volume buttons side down.  This means nearly all landscape videos will be upside-down as the Volume-Up button is the start/stop recording trigger.  Portrait videos will appear to be rotated 90° counter-clockwise if they were recorded with the Home button at the bottom.  The iPhone embeds meta data within the video that tells the player how to rotate the video to make it right-side-up for playback.  While QuickTime and others respect that meta data, my preferred player, VLC, does not.  Since I will be sharing my videos and I do not know which players my audience will be using, I want to correct the video stream to be right-side-up regardless of the meta data.

The first problem is very easy to deal with, compress the video with something like Handbrake.  I've also noticed that the 1080p video can be grainy, so I can further reduce the filesize and not lose too much quality by downscaling to 720p.

The second problem is more complex.  Unfortunately HandBrake (or at least the GUI) does not offer a way to rotate video.  The HandBrake CLI does have a "rotate" option, however I found it is not a true rotation.  Rather, it simply flips on an axis.  The documentation is poor, but I found that a value of 1 flips on X, 2 flips on Y, and 3 flips on X and Y.  So using a value of 3 is the same as doing a 180° rotation, which is useful for videos that are upside down, but not for videos that are sideways.  After a bit of hunting I discovered that mencoder can do proper rotation.  I didn't want to spend the time trying to compile my own mencoder binary, so I found some precompiled mac binaries that others had done.  I couldn't find a Mac OS X mencoder binary that has both faad support (to decode source aac which the iPhone uses for audio) and x264 (to encode h264).  I settled on one I found at which can read aac and outputs to raw.  The solution I came up with was to use mencoder to rotate the video, and have it output raw video and pcm audio.  I then pass that raw input into the HandBrake CLI to do the downscaling and compressing.  The raw temp files are stores in /tmp, and they do get very large.  This isn't a problem for me as most of my videos are under a few minutes.  If you are going to be doing anything much longer you probably want to find a better mencoder binary that can do it all for you so you don't need to do raw to handbrake step.


The following direction are known to work on OS X 10.7.2:

  1. Create a directory for this project.
  2. Download the HandBrake CLI from  I grabbed the 64 bit version 0.9.5.  Mount the disk image and move HandBrakeCLI into your project directory.
  3. Download from .  Copy mencoder into your project directory.
  4. Using TextEdit, or your plain text editor of choice, create a file named "" in the project directory.  Copy the following contents into this file.
# used to flip iPhone videos that are upside-down in landscape right-side-up.  Takes iPhone 4s 1920*1080 video, compresses and outputs at 1280x720
# $1 is input file
# $2 is output file, should end with .m4v
width=1280  #height will be scaled to preserve aspect ratio
audioBitrate=128 #AAC kbps
constantQualityRF=22 #  22 looks good for iPhone
start="started at `date`"
# advanced options from HandBrake preset
# rotate, 1 flips on x, 2 flips on y, 3 flips on both (equivalent of 180 degree rotation)
./HandBrakeCLI -i "$1" -o "$2" -e x264 -O -B $audioBitrate -q $constantQualityRF -w $width --denoise="weak" --rotate=3 -x $x264Advanced
echo $start
echo "finished at " `date`
  1. The string for the x264Advanced variable can be obtained from the "Advanced" tab in the HandBrake GUI.  Shown above is the string from the "Normal" preset.

  1. Create another file named "".  Copy the following contents into this file.

# used to rotate iPhone portrait videos 90 degrees clockwise.  Compresses and outputs at 720x1280.  Note on the iPhone the side with the volume buttons is always "down" in videos.
# $1 is input file
# $2 is output file, should end with .m4v
#couldn't find a mac os x mencoder binary that has both faad (to decode source aac) and x264 (to encode h264).  Settled on this one which can read aac and outputs to raw.  Uses a lot of disk space!
start="started at `date`"
tmpRaw="/tmp/`date +%s`.raw"
#./mencoder -vf rotate=1 -ovc x264 -oac pcm "$1" -o "$2"#this is what we'd do with a better mencoder binary (x264 support)
./mencoder -vf rotate=1 -ovc raw -oac pcm "$1" -o "$tmpRaw"
height=1280  #width will be scaled to preserve aspect ratio, 720x1280 for vertical iPhone
audioBitrate=128 #AAC kbps
constantQualityRF=22 #  22 looks good for iPhone
./HandBrakeCLI -i "$tmpRaw" -o "$2" -e x264 -O -B $audioBitrate -q $constantQualityRF --height $height --denoise="weak" -x $x264Advanced
rm "$tmpRaw"
echo $start
echo "finished at " `date`
  1. In Terminal, cd into the project directory and make sure the permissions looks like this.  You'll probably need to run chmod 744 *.sh
-rwxr-xr-x@ 1 Thom  staff  16765320 Jan  3  2011 HandBrakeCLI
-rwxr--r--@ 1 Thom  staff       843 Jan  4 21:49
-rwxr--r--@ 1 Thom  staff      1252 Jan  4 21:50
-rwxr-xr-x@ 1 Thom  staff   8090416 Sep  7  2008 mencoder


To flip a video upside down, execute the following command in Terminal (within the context of your project directory).  Change the first argument to the path of your input file, and the second argument to the path of your desired output file, which should end in ".m4v".

./ ~/Desktop/IMG_0095.MOV ~/Desktop/IMG_0095.m4v


To flip a video 90° clockwise, execute the following command in Terminal (within the context of your project directory).  Change the first argument to the path of your input file, and the second argument to the path of your desired output file, which should end in ".m4v".

./ ~/Desktop/IMG_0112.MOV ~/Desktop/IMG_0112.m4v


"Before" landscape video.  Length = 1 minute.  Size = 182 MB.
"After" landscape video.  Length = 1 minute.  Size = 70 MB.

"Before" portrait video.  Length = 10 seconds.  Size = 33 MB.
"After" portrait video.  Length = 10 seconds.  Size = 3.2 MB.

If you know of a better mencoder binary, please let me know about it in the comments! 

Subscribe to RSS - video rotation