video rotation

OS X Photos Video Archiver

October 4, 2015

Its taken me a while to get used to Apple's Photos app, but I've definitely come around.  The ability to nest folders has dramatically cleaned up my library.  I signed up for iCloud Photo Library so I can share my entire library between my two macs, my iPhone and my wife's iPhone.  Unfortunately iCloud Photo Library only works with one Apple ID, so my wife and I are still sharing the same ID.  Hopefully Apple realizes that there are legitimate cases where multiple people want to share the entire library, but that's besides the point.

So I'm loving Photos and especially the access I have from every device.  One thing that still bothers me, though, is that videos don't get their own app.  Instead they are sort of just second-class citizens within the "Photos" app.  I have about 15,000 photos, so I appreciate having an app like Photos to organize them.  With videos, however, I prefer to organize on the file system.  Maybe I'm old-fashioned.  The other thing is that Apple stores the videos in the highest quality possible, so they chew up tons of space.  Check it out in Apple's own words (iPhone 6s on iOS 9.0.2).


130 MB - 200 MB per minute if you record in 1080p!  I am the father of three children and have hundreds of videos of their antics.  Even with the biggest iPhone I would quickly fill up the storage.  It would also chew up tons of iCloud storage space if I left everything as recorded.  What I want to do is export all the videos and compress for storage in iCloud drive where I can still access them from every device.  I've been doing this in the past manually with Handbrake, where I have come up with a preset that looks reasonably good to me.  I couldn't find anyone else that had put together a solution around automating this, so a buddy and I created one for ourselves.  In the interest of sharing this with the community we've posted it on GitHub.

The Photos Video Archiver is an Automator workflow that exports selected videos from Photos, compresses them to 720p via Handbrake, and finally sets the created timestamp of the new video with the original video creation timestamp.  Since its all on GitHub you can download it for yourself and change any settings you want.  Perhaps you want 1080p with a different Handbrake preset?  No problem!  I will be working on the documentation soon, and will also create a video demonstrating usage.  For now, due to what I think is a bug in El Capitan, you need to open the workflow from within Automator and click "run" after you've selected the videos in the Photos app.

"But wait!", you say, "Doesn't Photos allow you to do 720p exports from within the app?".  It does.  I have found, though, that its compression is lacking.  Using the Photos Video Archiver project when I exported about 300 videos the final size was 4.18 GB.  Using the 720p export from Photos the final size was 11.4 GB.  The stats for exporting these videos with various options are:

  • Photos Export Unmodified Original = 22.92 GB
  • Photos Export with 720p Movie Quality = 11.4 GB
  • Photos Video Archiver = 4.18 GB

That's nearly an 82% reduction from original file size, 63% reduction from the Photos 720p export file size.  Again this preset is one that I've found works well for me.  Its easy enough to adapt it for yourself.  It took about 6 hours for the encoding to finish (MacBook Pro, Mid 2014, 2.5 GHz i7).  The preset I am using is basically:

H.264 (x264)
"veryslow" preset under "Encoder Options"
Constant Quality RF (Rate Factor) 22
Output 1280 x 720 (or 720 x 1280, depending on rotation)

Going into this project I wasn't sure how iCloud Photo Library handled videos.  My fear was that the source quality video was only on the phone, and that a lower-quality version was stored in the cloud.  To my relief I found that the "Unmodified Original" truly is byte-for-byte the same as copying it off the phone with a lightning cable.  When you drag a video out of Photos it treats it as a 720p export.  Something to keep in mind.

Before Photos it was difficult to export slomo videos.  I've addressed this messy issue in a previous post.  Fortunately the video export from Photos preserves the slomo (ducking, audio pitch, slow playback) effects when it exports at 30 FPS.  That means anyone can play back slomo files and see the cool effects.  It also means though that you cannot further modify the slomo after it has been exported, so make sure you're happy with it before you begin.

Something else I've pointed out in the past is that iPhones record all videos with the volume-buttons as the bottom side of the video.  So even if you record a video in portrait (home button at the bottom), if you play it back in something like VLC, it will be rotated 90°, outputting in landscape with all your content sideways.  Fortunately the video export from Photos corrects rotation, so playback in any app (e.g. VLC) has the correct rotation.  So that we don't have to solve rotation ourselves in Photo Video Archiver, we encode videos that have already been exported from Photos in a higher quality.  The workflow is:

  1. Applescript changes the title of all selected videos to include the date and time the video was originally created.
  2. User manually exports selected videos using 1080p Movie Quality, setting the File Name to "Use Title".  The videos need to be exported to an empty folder.
  3. Automator then prompts user to select the folder they just sent the export to.
  4. Unix bash script executes and:
    1. Creates an "encoded" folder within the export folder.
    2. Detects if the video is landscape or portrait.
    3. Sends the video through the HandBrakeCLI with the correct width parameter for landscape or portrait videos.
    4. Renames the newly encoded video with the original title.
    5. Sets the creation date of the newly encoded video with the original creation date.

More work is needed to improve usability and expose more options to the user, but if you are willing to run this from within Automator you can start using it today.  I'm hoping others will find this helpful and will contribute as well.  The ultimate solution, in my opinion, would be for Apple to expose "advanced" options for encoding within Photos.  Until then, we can solve this ourselves!

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