iOS AVWritter从中心

时间:2016-07-05 11:00:24

标签: ios objective-c

我正在使用AVCapture录制视频。我需要裁剪视频。我能够通过设置

来做到这一点
AVVideoWidthKey : [NSNumber numberWithInteger:outputSize.width],
AVVideoHeightKey : [NSNumber numberWithInteger:outputSize.height],

但是,我需要使用偏移进行裁剪,我不想在默认情况下从中心裁剪。

我在AVFoundation的AVVideoSettings.h中读到我可以使用AVVideoCleanApertureVerticalOffsetKey

来实现这一点
  

此键的值是包含AVVideoCleanAperture *键的NSDictionary。 AVVideoCleanApertureWidthKey和AVVideoCleanApertureHeightKey定义一个以视频帧为中心的干净矩形。要从中心偏移此矩形,请使用AVVideoCleanApertureHorizo​​ntalOffsetKey和AVVideoCleanApertureVerticalOffsetKey。 AVVideoCleanApertureHorizo​​ntalOffsetKey的正值将清洁光圈区域向右移动,AVVideoCleanApertureVerticalOffsetKey的正值将清洁光圈区域向下移动。

我正在尝试以下方法,但我只是将作物放在中心而没有偏移。

NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [NSNumber numberWithInteger:outputSize.width], AVVideoCleanApertureWidthKey,
                                            [NSNumber numberWithInteger:outputSize.height], AVVideoCleanApertureHeightKey,
                                            [NSNumber numberWithInteger:0], AVVideoCleanApertureHorizontalOffsetKey,
                                            [NSNumber numberWithInteger:-200], AVVideoCleanApertureVerticalOffsetKey,
                                            nil];

NSMutableDictionary *compressionSettings = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithUnsignedLong:bitrate] forKey:AVVideoAverageBitRateKey];

[compressionSettings setObject:videoCleanApertureSettings forKey:AVVideoCleanApertureKey];

if (self.shouldKeepOnlyKeyFrames) {
    [compressionSettings setObject:@1 forKey:AVVideoMaxKeyFrameIntervalKey];
}

if (self.profileLevel) {
    [compressionSettings setObject:self.profileLevel forKey:AVVideoProfileLevelKey];
}
[compressionSettings setObject:@NO forKey:AVVideoAllowFrameReorderingKey];
[compressionSettings setObject:@30 forKey:AVVideoExpectedSourceFrameRateKey];



return @{
         AVVideoCodecKey : self.codec,
         AVVideoScalingModeKey : self.scalingMode,
         AVVideoWidthKey : [NSNumber numberWithInteger:outputSize.width],
         AVVideoHeightKey : [NSNumber numberWithInteger:outputSize.height],
         AVVideoCompressionPropertiesKey : compressionSettings
         };

1 个答案:

答案 0 :(得分:0)

您缺少信息。你的参数应该更像这样。

NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                       [NSNumber numberWithInt:320], AVVideoCleanApertureWidthKey,
                                       [NSNumber numberWithInt:480], AVVideoCleanApertureHeightKey,
                                       [NSNumber numberWithInt:10], AVVideoCleanApertureHorizontalOffsetKey,
                                       [NSNumber numberWithInt:10], AVVideoCleanApertureVerticalOffsetKey,
                                        nil];


NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                         [NSNumber numberWithInt:3], AVVideoPixelAspectRatioHorizontalSpacingKey,
                                         [NSNumber numberWithInt:3],AVVideoPixelAspectRatioVerticalSpacingKey,
                                                nil];



NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                           [NSNumber numberWithInt:960000], AVVideoAverageBitRateKey,
                           [NSNumber numberWithInt:1],AVVideoMaxKeyFrameIntervalKey,
                           videoCleanApertureSettings, AVVideoCleanApertureKey,
                           //videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
                           //AVVideoProfileLevelH264Main30, AVVideoProfileLevelKey,
                           nil];





NSString *targetDevice = [[UIDevice currentDevice] model];

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                           AVVideoCodecH264, AVVideoCodecKey,
                           codecSettings,AVVideoCompressionPropertiesKey,
                           [NSNumber numberWithInt:320], AVVideoWidthKey,
                           [NSNumber numberWithInt:480], AVVideoHeightKey,
                           nil];