Skip to content

Instantly share code, notes, and snippets.

@jblocksom
Created May 3, 2012 20:56
Show Gist options
  • Select an option

  • Save jblocksom/2589408 to your computer and use it in GitHub Desktop.

Select an option

Save jblocksom/2589408 to your computer and use it in GitHub Desktop.
AVFoundation Chapter Helpers HBM 4/30/12
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toIO
duration:(NSTimeInterval)duration
{
CGFloat angle = 0;
switch (toIO) {
case UIInterfaceOrientationPortrait:
angle = 0;
break;
case UIInterfaceOrientationPortraitUpsideDown:
angle = M_PI;
break;
case UIInterfaceOrientationLandscapeLeft:
angle = M_PI_2;
break;
case UIInterfaceOrientationLandscapeRight:
angle = -M_PI_2;
break;
default:
break;
}
[previewLayer setAffineTransform:CGAffineTransformMakeRotation(angle)];
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromIO
{
CALayer *rootLayer = [previewView layer];
[previewLayer setFrame:[rootLayer bounds]];
}
- (CGAffineTransform)assetWriterVideoInTransform:(UIInterfaceOrientation)io
{
CGFloat angle = 0;
switch (io) {
case UIInterfaceOrientationPortrait:
angle = M_PI_2;
break;
case UIInterfaceOrientationPortraitUpsideDown:
angle = -M_PI_2;
break;
case UIInterfaceOrientationLandscapeLeft:
angle = M_PI;
break;
case UIInterfaceOrientationLandscapeRight:
angle = 0;
break;
default:
break;
}
return CGAffineTransformMakeRotation(angle);
}
- (BOOL) setupAssetWriterVideoInput:(CMFormatDescriptionRef)currentFormatDescription
{
float bitsPerPixel;
CMVideoDimensions dimensions =
CMVideoFormatDescriptionGetDimensions(currentFormatDescription);
int numPixels = dimensions.width * dimensions.height;
int bitsPerSecond;
// Assume that lower-than-SD resolutions are intended for streaming,
// and use a lower bitrate
if (numPixels < (640 * 480))
bitsPerPixel = 4.05; // quality produced by AVCaptureSessionPresetMedium or Low.
else
bitsPerPixel = 11.4; // quality produced by AVCaptureSessionPresetHigh.
bitsPerSecond = numPixels * bitsPerPixel;
NSDictionary *videoCompressionSettings =
[NSDictionary dictionaryWithObjectsAndKeys:
AVVideoCodecH264, AVVideoCodecKey,
[NSNumber numberWithInteger:dimensions.width], AVVideoWidthKey,
[NSNumber numberWithInteger:dimensions.height], AVVideoHeightKey,
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInteger:bitsPerSecond],
AVVideoAverageBitRateKey,
[NSNumber numberWithInteger:30],
AVVideoMaxKeyFrameIntervalKey,
nil], AVVideoCompressionPropertiesKey,
nil];
if ([assetWriter canApplyOutputSettings:videoCompressionSettings
forMediaType:AVMediaTypeVideo]) {
assetWriterVideoIn = [[AVAssetWriterInput alloc]
initWithMediaType:AVMediaTypeVideo
outputSettings:videoCompressionSettings];
assetWriterVideoIn.expectsMediaDataInRealTime = YES;
assetWriterVideoIn.transform =
[self assetWriterVideoInTransform:[self interfaceOrientation]];
if ([assetWriter canAddInput:assetWriterVideoIn])
[assetWriter addInput:assetWriterVideoIn];
else {
NSLog(@"Couldn't add asset writer video input.");
return NO;
}
}
else {
NSLog(@"Couldn't apply video output settings.");
return NO;
}
return YES;
}
- (void)removeMovieFile
{
// remove the file if it already exists
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [movieURL path];
if ([fileManager fileExistsAtPath:filePath]) {
NSError *error;
BOOL success = [fileManager removeItemAtPath:filePath error:&error];
if (!success)
NSLog(@"(removeMovieFile) error %@: %@", error.localizedDescription,
error.localizedFailureReason);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment