title: How to detect if a video file was recorded in portrait orientation, or landscape in iOS
body: <p>I am using <code>AlAssetsGroup enumerateAssetsAtIndexes</code> to list the assets in the Photos (Camera) app. For a given video asset I want to determine whether it was shot in portrait or landscape mode. </p>
  
  <p>In the following code, asset is an <code>AlAsset</code> and I have tested to see if it is a video asset <code>[asset valueForProperty:ALAssetPropertyType]</code> is <code>AlAssetTypeVideo</code>, then:</p>
  
  <pre><code>int orientation = [[asset valueForProperty:ALAssetPropertyOrientation] intValue];
  </code></pre>
  
  <p>In this case <code>orientation</code> is always 0 which is <code>ALAssetOrientationUp</code>. Maybe this is to be expected, all videos are up right, but a portrait video is represented in MPEG-4 as a landscape video turned 90 degrees (i.e. all videos are actually landscape, try the MediaInfo app on the mac if you don't believe me). </p>
  
  <p>Where within the file and/or how do I access the information that tells me it was actually recorded while holding the phone in portrait orientation?</p>
  
  <p>I have also tried this, given the url of the asset:</p>
  
  <pre><code>AVURLAsset *avAsset = [[AVURLAsset alloc] initWithURL:url options:nil];
  CGSize size = [avAsset naturalSize];
  NSLog(@"size.width = %f size.height = %f", size.width, size.height);
  CGAffineTransform txf = [avAsset preferredTransform];
  NSLog(@"txf.a = %f txf.b = %f  txf.c = %f  txf.d = %f  txf.tx = %f  txf.ty = %f",
              txf.a, txf.b, txf.c, txf.d, txf.tx, txf.ty);
  </code></pre>
  
  <p>Which always yields a width > height so for iPhone 4, width=1280 height=720 and the transform a and d values are <code>1.0</code>, the others are <code>0.0</code>, regardless of the capture orientation.</p>
  
  <p>I have looked at the meta data using MediaInfo app on the Mac, I have done a Hexdump and so far have not found any difference between a landscape and portrait video. But QuickTime knows and displays portrait videos vertically, and the phone knows by rotating a portrait video if you are holding the phone in landscape orientation on playback and correctly displaying it if holding it in portrait.</p>
  
  <p>BTW I can't use <code>ffmpeg</code> (can't live with the license restrictions). Is there an iPhone SDK native way to do this?</p>