Skip to content

Instantly share code, notes, and snippets.

@lsmolic
Last active December 29, 2015 12:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmolic/7669646 to your computer and use it in GitHub Desktop.
Save lsmolic/7669646 to your computer and use it in GitHub Desktop.
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