1

I would like to crop the outliers of a landscape video so that it has the dimension ratio of a portrait video.

I was reading about cropping on this post: How do I use AVFoundation to crop a video

but I'm not sure how to set the transform - also this answer is now over four years old so I'm not sure how it applies to the larger screen sizes available today. Tnx!

Community
  • 1
  • 1
etayluz
  • 15,920
  • 23
  • 106
  • 151

1 Answers1

0

This seems to do the trick:

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

AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
float width = 1080.0/1920.0 * size.height;
videoComposition.renderSize = CGSizeMake(width, size.height);
videoComposition.frameDuration = CMTimeMake(1, 30);

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

AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform translate = CGAffineTransformMakeTranslation((width - size.width)/2, 0);
translate = CGAffineTransformScale(translate, 1, 1);

[transformer setTransform:translate atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

encoder.videoComposition = videoComposition;
etayluz
  • 15,920
  • 23
  • 106
  • 151