Skip to content

Instantly share code, notes, and snippets.

@lsmolic

lsmolic/gist:7669646

Last active Dec 29, 2015
Embed
What would you like to do?
NSDictionary *videoCleanApertureSettings = [NSDictionary dictionaryWithObjectsAndKeys:
@480, AVVideoCleanApertureWidthKey,
@480, AVVideoCleanApertureHeightKey,
@0, AVVideoCleanApertureHorizontalOffsetKey,
@0, AVVideoCleanApertureVerticalOffsetKey,
nil];
NSDictionary *videoAspectRatioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
@1,AVVideoPixelAspectRatioHorizontalSpacingKey,
@1,AVVideoPixelAspectRatioVerticalSpacingKey,
nil];
// NSNumber* bitsPerSecond = [NSNumber numberWithDouble:1024.0*1024.0];
NSNumber* bitsPerSecond = [NSNumber numberWithDouble:1024*256];
// NSNumber* bitsPerSecond = [NSNumber numberWithDouble:1024*178];
NSDictionary *codecSettings = [NSDictionary dictionaryWithObjectsAndKeys:
bitsPerSecond, AVVideoAverageBitRateKey,
//@1,AVVideoMaxKeyFrameIntervalKey,
videoCleanApertureSettings, AVVideoCleanApertureKey,
videoAspectRatioSettings, AVVideoPixelAspectRatioKey,
nil];
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
codecSettings,AVVideoCompressionPropertiesKey,
AVVideoScalingModeResizeAspectFill,AVVideoScalingModeKey, // this is the key to getting square video out
@480, AVVideoWidthKey,
@480, AVVideoHeightKey,
nil];
videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
NSParameterAssert(videoWriterInput);
videoWriterInput.expectsMediaDataInRealTime = YES;
// Add the audio input
AudioChannelLayout acl;
bzero( &acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
NSDictionary* audioOutputSettings = nil;
// Both type of audio inputs causes output video file to be corrupted.
audioOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt: kAudioFormatMPEG4AAC ], AVFormatIDKey,
[NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
[NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
[NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,nil];
audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings];
audioWriterInput.expectsMediaDataInRealTime = YES;
// add input
[videoWriter addInput:videoWriterInput];
[videoWriter addInput:audioWriterInput];
self.swapCameraButton.enabled = NO;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment