24

I'm trying to use AVFoundation to crop videos I'm recording. So lets say i create a AVCaptureVideoPreviewLayer and set the frame to be 300x300.

AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer     layerWithSession:session];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.delegate = self;
captureVideoPreviewLayer.frame = CGRectMake(0,0, 300, 300);
[previewView.layer addSublayer:captureVideoPreviewLayer];

The user sees the video cropped. I'd like to save the video exactly the way the user is viewing it. Using AVCaptureMovieFileOutput, the video obviously gets saved without cropping. I was considering using a AVCaptureVideoDataOutput to intercept the frames and crop them myself, but I was wondering if there is a more efficient way to do this, perhaps with AVExportSession and using an AVVideoComposition.

Any guidance would be appreciated.

1
  • Were you able to get the output to look as crisp as the preview layer? That's been a problem, matching the sharpness of AVLayerVideoGravityResizeAspectFill.
    – Crashalot
    Feb 8, 2016 at 21:19

2 Answers 2

31

Soemthing like this. 99% of this code just sets it up to do a custom CGAffineTransform, and then save out the result.

I'm assuming that you want the cropped video to take up full size/width of the output - so that e.g a Scale Affine is the correct solution (you zoom in on the video, giving the effect of having cropped + resized).

AVAsset* asset = // your input

AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableVideoComposition* videoComposition = [[AVMutableVideoComposition videoComposition]retain];
videoComposition.renderSize = CGSizeMake(320, 240);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform finalTransform = // setup a transform that grows the video, effectively causing a crop
    [transformer setTransform:finalTransform atTime:kCMTimeZero];
    instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

exporter = [[AVAssetExportSession alloc] initWithAsset:saveComposition presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComposition;
exporter.outputURL=url3;
exporter.outputFileType=AVFileTypeQuickTimeMovie;

[exporter exportAsynchronouslyWithCompletionHandler:^(void){}];
11
  • 2
    Adam, thanks for the post. I actually did exactly that and was going to post the answer, but you beat me to the punch! Thank you for the help regardless...I think others will definitely benefit.
    – haider
    Mar 9, 2011 at 7:29
  • Could you elaborate on getting the AVAsset input. Do you run this on the movie file after its been saved with AVCaptureMovieFileOutput? Mar 7, 2012 at 21:19
  • AVAsset is Apple's catch-all class that wraps any kind of input you could provide to AVFoundation. Off the top of my head, you want to look at the various subclasses that exist in AVF (go to any of Apple's docs pages for AVF, and click on any link to "Class Reference" and you'll get the full list) - they're all named "AV[something]Asset" IIRC.
    – Adam
    Mar 8, 2012 at 10:12
  • 1
    Can we get an example of a "transform that grows the video, effectively causing a crop"?
    – etayluz
    Jul 17, 2015 at 15:34
  • 1
    @AnkitKumarGupta You have a working example - you replied to it. If you have a different problem, ask a new question. That's what this site is for.
    – Adam
    Apr 18, 2017 at 8:57
11

ios7 added a specific Layer instruction just for cropping.

videolayerInstruction setCropRectangle:atTime:

_mike

3
  • 2
    This does not provide the expected results. FYI. This setting will crop a rect, from the original video pixel size. Unrelated to screen size on device. May 7, 2014 at 23:26
  • @nibeck, could you kindly include a code block that demonstrates how to use this?
    – etayluz
    Jul 17, 2015 at 15:24
  • I tried this. But did not work for me. Had to switch back to translation transform.
    – Sreeraj
    Aug 3, 2015 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.