GPUImage初探

GPUImage作為一個很強大很強大很強大的圖形處理工具,可以處理圖片,視頻.超爽的.

廢話不多說,第一天大概我就用了一下濾鏡功能(這里補充一下,各種特效是可以混合使用的,夠你玩很久很久了)

1.首先我們需要GPUImageVideoCamera

最好不要在self.view上操作, 所以我們需要一個自己的GPUImageView來顯示(這里說下, 如果應(yīng)用不支持橫豎屏,但是進(jìn)入的時候必須橫屏,可以直接修改preview的transform給人一種橫屏的感覺,比你自己去調(diào)整系統(tǒng)的那個橫豎屏要方便的多)

我選擇先生成一個用以顯示預(yù)覽的界面

?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];//此時你就可以在屏幕上看見攝像頭捕捉到的數(shù)據(jù)了

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;

}


壓縮視頻,本來有一個方向需要調(diào)整,GPUImage貌似不能和系統(tǒng)錄制的視頻一樣調(diào)整方向,就不多說了

-(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;

? ? ? ? }

? ? }];

}


注:其實有個很關(guān)鍵的地方, 就是如果橫著錄視頻,我們需要調(diào)整輸出視頻的方向

_movieWriter.transform = transform; 修改這個就行了, 為此,我苦惱的很長時間

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容