title: Cropping video to square iOS
body: <p>I know this is a frequently asked question, I've looked around and there is no distinct answer.</p>
  
  <p>The code is from an article: <a href="http://www.netwalk.be/article/record-square-video-ios" rel="nofollow">http://www.netwalk.be/article/record-square-video-ios</a> </p>
  
  <p>What I want: I want to crop the video to make it square.</p>
  
  <p>So basically this is what I'm doing. It makes sense to me, but for some reason the video is not cropping, in fact, its staying the same size (width and height).</p>
  
  <pre><code>AVAsset* asset = [AVAsset assetWithURL:self.videoURL];
  
  AVMutableComposition *composition = [AVMutableComposition composition];
  [composition  addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
  
  // input clip
  AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
  
  // make it square
  AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
  videoComposition.renderSize = CGSizeMake(3.0 , 3.0);
  videoComposition.frameDuration = CMTimeMake(1, 30);
  
  AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
  instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(clipVideoTrack.naturalSize.width, clipVideoTrack.naturalSize.width) );
  
  // rotate to portrait
  AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
  CGAffineTransform t1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, -(clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) /2 );
  CGAffineTransform t2 = CGAffineTransformRotate(t1, M_PI_2);
  
  CGAffineTransform finalTransform = t2;
  [transformer setTransform:finalTransform atTime:kCMTimeZero];
  instruction.layerInstructions = [NSArray arrayWithObject:transformer];
  videoComposition.instructions = [NSArray arrayWithObject: instruction];
  
  AVAssetExportSession *exporter;
  
  // export
  exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality] ;
  exporter.videoComposition = videoComposition;
  exporter.outputURL=self.videoURL;
  exporter.outputFileType=AVFileTypeMPEG4;
  
  
  [exporter exportAsynchronouslyWithCompletionHandler:^(void){
      NSLog(@"Exporting done!");
  
  }]; 
  </code></pre>
  
  <p>I believe the problem is in the exporter at the end. Either its not exporting correctly, or there is something else I'm missing. Please someone refer me to a good way to do it. Thanks.</p>