GPUImage作为一个很强大很强大很强大的图形处理工具,可以处理图片,视频.超爽的.
废话不多说,第一天大概我就用了一下滤镜功能(这里补充一下,各种特效是可以混合使用的,够你玩很久很久了)
1.首先我们需要GPUImageVideoCamera
最好不要在self.view上操作, 所以我们需要一个自己的GPUImageView来显示(这里说下, 如果应用不支持横竖屏,但是进入的时候必须横屏,可以直接修改preview的transform给人一种横屏的感觉,比你自己去调整系统的那个横竖屏要方便的多)
我选择先生成一个用以显示预览的界面
self.preview = [[GPUImageView alloc] initWithFrame:self.view.bounds];
_videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPresetiFrame960x540 cameraPosition:AVCaptureDevicePositionBack];
_videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
2.然后呢,你需要一个滤镜, 注:滤镜可以叠加
_filter = [[GPUImageTransformFilter alloc] init];
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
unlink([pathToMovie UTF8String]); // If a file already exists, AVAssetWriter won't let you record new frames, so delete the old movie
NSURL *movieURL = [NSURL fileURLWithPath:pathToMovie];
3.最后呢需要一个GPUImageMovieWriter
_movieWriter _movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(540, 960)];
_movieWriter.encodingLiveVideo = YES;
[_filter addTarget:_movieWriter];
[_videoCamera addTarget:_filter];
_videoCamera.audioEncodingTarget = _movieWriter;
GPUImageView *filterView = (GPUImageView *)self.preview;
[_filter addTarget:filterView];
[_videoCamera startCameraCapture];//此时你就可以在屏幕上看见摄像头捕捉到的数据了
4,开始录制
-(void)recordVideo:(UIButton*)button{
if (!button.selected) {
button.selected = YES;
[self changeTheOrientaionOfWriter];
[_movieWriter startRecording];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerOfRecord) userInfo:nil repeats:YES];
}else{
[_filter removeTarget:_movieWriter];
_videoCamera.audioEncodingTarget = nil;
[_movieWriter finishRecording];
[_timer invalidate];
NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.m4v"];
[self encodeVideoOrientation:[NSURL fileURLWithPath:pathToMovie]];
}
}
-(void)changeTheOrientaionOfWriter{
// CGSize movieWriteSize = CGSizeMake(480, 640);
// UIInterfaceOrientation orientation = UIInterfaceOrientationPortrait;
CGAffineTransform transform = CGAffineTransformIdentity;
switch (_orientationNew) {
case UIDeviceOrientationLandscapeLeft:
{
// orientation = UIInterfaceOrientationLandscapeRight;
transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI_2);
}
break;
case UIDeviceOrientationLandscapeRight:
{
transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI_2);
// orientation = UIInterfaceOrientationLandscapeLeft;
}
break;
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
{
// orientation = UIInterfaceOrientationPortrait;
}
break;
default:
break;
}
_movieWriter.transform = transform;
}
压缩视频,本来有一个方向需要调整,GPUImage貌似不能和系统录制的视频一样调整方向,就不多说了
-(void)encodeVideoOrientation:(NSURL*)anOutputFileURL{
AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];
AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:videoAsset
presetName:AVAssetExportPresetMediumQuality];
NSString* mp4Path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Movie.mp4"];
NSFileManager* defaultFileManager = [NSFileManager defaultManager];
if ([defaultFileManager fileExistsAtPath:mp4Path]) {
[defaultFileManager removeItemAtPath:mp4Path error:nil];
}
assetExport.outputURL = [NSURL fileURLWithPath: mp4Path];
assetExport.shouldOptimizeForNetworkUse = YES;
assetExport.outputFileType = AVFileTypeMPEG4;
// assetExport.videoComposition = [self getVideoComposition:videoAsset];
[assetExport exportAsynchronouslyWithCompletionHandler:^{
switch ([assetExport status]) {
case AVAssetExportSessionStatusFailed:
{
NSLog(@"AVAssetExportSessionStatusFailed!");
break;
}
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
{
NSLog(@"Successful!");
dispatch_async(dispatch_get_main_queue(), ^{
LKPlayVideoViewController* vc = [[LKPlayVideoViewController alloc] init];
vc.sendViewController = self.sendViewController;
[self.navigationController pushViewController:vc animated:NO];
});
}
break;
default:
break;
}
}];
}
注:其实有个很关键的地方, 就是如果横着录视频,我们需要调整输出视频的方向
_movieWriter.transform = transform; 修改这个就行了, 为此,我苦恼的很长时间