标签:
/// 创建相机和滤镜
- (void)createVideoCamera:(AVCaptureDevicePosition)frontOrBack{
[videoCamera removeAllTargets];
videoCamera = nil;
videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:frontOrBack];
videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
/// horizontallyMirrorFrontFacingCamera 属性 YES 代表前置的时候不是镜像
videoCamera.horizontallyMirrorFrontFacingCamera = YES;
videoCamera.horizontallyMirrorRearFacingCamera = NO;
/// 裁剪滤镜
cropFilter = [[GPUImageCropFilter alloc] initWithCropRegion:CGRectMake(0, (1280-720)/2/1280.0, 1,720/1280.0)];
/// 自定义的美颜滤镜
beautyFilter = [[GPUImageBeautyFilter alloc] init];
/// 滤镜分组
filterGroup = [[GPUImageFilterGroup alloc] init];
/// 没有开美颜
if (!isBeauty) {
[videoCamera addTarget:cropFilter];
/// 预览层 /*预览层可以省略*/
[cropFilter addTarget:preView];
}
else{/// 美颜和裁剪滤镜都加到滤镜组里
[cropFilter addTarget:beautyFilter];
[filterGroup addTarget:cropFilter];
[filterGroup addTarget:beautyFilter];
[filterGroup setInitialFilters:[NSArray arrayWithObject:cropFilter]];
[filterGroup setTerminalFilter:beautyFilter];
[filterGroup forceProcessingAtSize:preView.frame.size];
[filterGroup useNextFrameForImageCapture];
[videoCamera addTarget:filterGroup];
[filterGroup addTarget:preView];
}
/// 开始捕捉
[videoCamera startCameraCapture];
}
///开始录制 分段录制每次都要实例化一个movieWriter
- (void)startPhoto {
/// 根据时间生成路径 录制的视频会存储到该路径下 要唯一
NSString *pathToMovie = [self getVideoMergeFilePathString];
[videoArray addObject:pathToMovie];
NSLog(@"%@",pathToMovie);
unlink([pathToMovie UTF8String]);
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
// movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720, 720) fileType:AVFileTypeQuickTimeMovie outputSettings:videoSettings];
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(720, 720)];
/// audioSettings 一些音频配置
/*
AudioChannelLayout channelLayout;
memset(&channelLayout, 0, sizeof(AudioChannelLayout));
channelLayout.mChannelLayoutTag = kAudioChannelLayoutTag_Stereo;
audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,//制定编码算法
[ NSNumber numberWithInt: 2 ], AVNumberOfChannelsKey,//声道
[ NSNumber numberWithFloat: 16000.0 ], AVSampleRateKey,//采样率
[ NSData dataWithBytes:&channelLayout length: sizeof( AudioChannelLayout ) ], AVChannelLayoutKey,
[ NSNumber numberWithInt: 32000 ], AVEncoderBitRateKey,//编码率
nil];
*/
[movieWriter setHasAudioTrack:YES audioSettings:audioSettings];
movieWriter.hasAudioTrack = YES;
[videoCamera addAudioInputsAndOutputs];
videoCamera.audioEncodingTarget = movieWriter;
if (!isBeauty) {
[cropFilter addTarget:movieWriter];
}
else {
[filterGroup addTarget:movieWriter];
}
[movieWriter startRecording];
}
///完成录制
- (void)endRecording {
[movieWriter finishRecording];
[cropFilter removeTarget:movieWriter];
[beautyFilter removeTarget:movieWriter];
[filterGroup removeTarget:movieWriter];
videoCamera.audioEncodingTarget = nil;
// [self savePhotoCmare:videoArray.lastObject];
}
///把录制好的视频写相册
- (void)savePhotoCmare:(NSString *)pathToMovie {
NSURL *url = [[NSURL alloc] initFileURLWithPath:pathToMovie];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:url]) {
[library writeVideoAtPathToSavedPhotosAlbum:url
completionBlock:^(NSURL *assetURL, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
});
}];
}
}
标签:
原文地址:http://www.cnblogs.com/xia0huihui/p/5430737.html