How to crop video to square iOS using AVAssetWriter

I am using AVAssetWriter to record video, and I want to be able to crop the video into a square with an offset at the top. Here is my code -

NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys: @320, AVVideoCleanApertureWidthKey, @320, AVVideoCleanApertureHeightKey, @10, AVVideoCleanApertureHorizontalOffsetKey, @10, AVVideoCleanApertureVerticalOffsetKey, nil]; NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys: @3, AVVideoPixelAspectRatioHorizontalSpacingKey, @3,AVVideoPixelAspectRatioVerticalSpacingKey, nil]; NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInteger:bitsPerSecond], AVVideoAverageBitRateKey, @1,AVVideoMaxKeyFrameIntervalKey, videoCleanApertureSettings, AVVideoCleanApertureKey, //AVVideoScalingModeFit,AVVideoScalingModeKey, videoAspectRatioSettings, AVVideoPixelAspectRatioKey, nil]; NSDictionary *videoCompressionSettings = [NSDictionary dictionaryWithObjectsAndKeys: AVVideoCodecH264, AVVideoCodecKey, codecSettings,AVVideoCompressionPropertiesKey, @320, AVVideoWidthKey, @320, AVVideoHeightKey, nil]; 

Whenever I uncomment AVVideoScalingModeKey, my scripting resource gives me the error that you cannot apply the video composition settings. I tried using How to make a video with a reduced size using AVAssetWriter? but it still does not work for me.

+4
source share
1 answer

I added AVVideoScalingModeFit,AVVideoScalingModeKey to my videoCompressionSettings to make it work.

+5
source

Source: https://habr.com/ru/post/1493679/


All Articles