如何使用AVAssetWriter将视频裁剪为方形iOS

时间:2013-07-26 13:40:25

标签: iphone ios objective-c avfoundation

我正在使用AVAssetWriter录制视频,我希望能够将视频剪裁成一个偏离顶部的正方形。这是我的代码 -

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];

每当我取消注释AVVideoScalingModeKey时,我的assetWriter会给我一个关于无法应用videoCompressionSettings的错误。我尝试使用How do make a reduced size video using AVAssetWriter?,但它仍然不适用于我。

1 个答案:

答案 0 :(得分:5)

我已将AVVideoScalingModeFit,AVVideoScalingModeKey添加到videoCompressionSettings以使其正常运行。