iOS視頻開發(fā):視頻的錄制一

一、UIImagePickerController

UIImagePickerControllerUIKit框架里面的一個(gè)class,通過這個(gè)系統(tǒng)提供的class我們可以簡單的是實(shí)現(xiàn)拍照、錄制視頻和音頻。

三個(gè)步驟:
  1. 當(dāng)前控制器present一個(gè)UIImagePickerController
  2. 在當(dāng)前界面就可以拍照、錄制視頻和音頻
  3. 實(shí)現(xiàn)UIImagePickerControllerdelegate,在delegate可以獲取錄制的視頻和音頻,來進(jìn)行相應(yīng)的操作.
定制化UIImagePickerController
//    查看攝像頭是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {   
  return;
}
UIImagePickerController *imagePick = [[UIImagePickerController alloc]init];
imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
//    我們還可以設(shè)置照片和視頻拍攝的質(zhì)量、是否可以開啟閃光燈、是否開啟手電筒
//    還可以單獨(dú)設(shè)置只支持視頻模式
//    imagePick.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
imagePick.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//    UINavigationControllerDelegate,UIImagePickerControllerDelegate;
imagePick.delegate = self;
//拍照或者錄制結(jié)束后是否可以編輯
imagePick.allowsEditing = NO;
[self presentViewController:imagePick animated:YES completion:nil];

界面的自定義

cameraOverlayView屬性可以自定義UIImagePickerController界面頂部的控件,但是只在UIImagePickerControllermediaTypesUIImagePickerControllerSourceTypeCamera時(shí)可用。

實(shí)現(xiàn)UIImagePickerControllerdelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
    UIImage *originalImage, *editedImage, *imageToSave;
    //    處理圖片
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        editedImage = info[UIImagePickerControllerOriginalImage];
        originalImage = info[UIImagePickerControllerEditedImage];
        if (editedImage) {
            imageToSave = editedImage;
        }else{
            imageToSave = originalImage;
        }
        UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);
    }
    //處理視頻
    if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
        NSString *url = [info[UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url)) {
            UISaveVideoAtPathToSavedPhotosAlbum(url, nil, nil, nil);
        }
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    
}

二、AVCaptureSession & AVCaptureMovieFileOutput

要獲取攝像機(jī)捕捉到的視頻或者麥克風(fēng)捕捉到的音頻,我們需要對(duì)象表示inputs和outputs,并使用AVCaptureSession的實(shí)例來協(xié)調(diào)它們之間的數(shù)據(jù)流。

  1. AVCaptureDevice 對(duì)象,表示聲音或者視頻采集設(shè)備,對(duì)應(yīng)攝像頭和麥克風(fēng)。
  2. AVCaptureInput的子類,配置輸入端口。
  3. AVCaptureOutput的子類, 輸出采集到的視頻或者圖像。
  4. AVCaptureSession來協(xié)調(diào)從輸入到輸出的數(shù)據(jù)流。
步驟一: 創(chuàng)建AVCaptureDevice 對(duì)象

因?yàn)槲覀冃枰浿埔曨l和音頻所以我們需要視頻的AVCaptureDevice和音頻的AVCaptureDevice。

//我們同時(shí)獲取了前攝像頭和后攝像頭因?yàn)榈葧?huì)我們要手動(dòng)切換
//獲取音頻device
-(AVCaptureDevice *)audioDevice{
    if (!_audioDevice) {
        _audioDevice = [AVCaptureDevice devicesWithMediaType:AVMediaTypeAudio].firstObject;
    }
    return _audioDevice;
}   
   
//后置攝像頭
-(AVCaptureDevice *)backVideoDevice{
    if (!_backVideoDevice) {
        _backVideoDevice = [self getDeviceBy:AVCaptureDevicePositionBack];
        if ([self.currentVideoDevice isTorchAvailable] && [_backVideoDevice isTorchModeSupported:AVCaptureTorchModeOn]) {
            //可以設(shè)置是否開啟閃光燈,是否開始HDR、視頻防抖、白平衡什么的
            //設(shè)置device之前需要先 lockForConfiguration
            if ([_backVideoDevice lockForConfiguration:NULL]==YES) {
                self.currentVideoDevice.torchMode = AVCaptureTorchModeOn;
                [self.currentVideoDevice unlockForConfiguration];
            }
        }
        
    }
    }
    return _backVideoDevice;
}
//前置攝像頭
-(AVCaptureDevice *)frontVideoDevice{
    if (!_frontVideoDevice) {
        _frontVideoDevice = [self getDeviceBy:AVCaptureDevicePositionFront];
    }
    return _frontVideoDevice;
}
步驟二、配置inputs

每個(gè)AVCaptureDevice對(duì)應(yīng)一個(gè)input。

//audio input
- (AVCaptureDeviceInput *)audioInput{
    if (!_audioInput) {
        NSError *error = nil;
        _audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.audioDevice error:&error];
        
    }
    return _audioInput;
}
- (AVCaptureDeviceInput *)videoInput{
    if (!_videoInput) {
        NSError *error  = nil;
        _videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.currentVideoDevice error:&error];
        
        
    }
    return _videoInput;
}
步驟三: 寫入文件

Output有四種:

AVCaptureMovieFileOutput 寫入文件
AVCaptureVideoDataOutput 加工視頻輸出
AVCaptureAudioDataOutput 加工音頻輸出
AVCaptureStillImageOutput 捕捉輸出的圖像

寫入文件只需要AVCaptureMovieFileOutput就可以了。

// output
- (AVCaptureMovieFileOutput  *)movieFileOutput{
    if (!_movieFileOutput) {
        _movieFileOutput = [[AVCaptureMovieFileOutput  alloc]init];
        //CMTime drution = CMTimeMake(1, 60);
        //設(shè)置視頻錄制時(shí)間限制 kCMTimeInvalid(無限制)
        _movieFileOutput.maxRecordedDuration = kCMTimeInvalid;
        // 文件大小限制
        //_movieFileOutput.maxRecordedFileSize = 1024 * 1024;
        AVCaptureConnection *videoConnection = [_movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
        // 是否支持科學(xué)防抖
        if ([videoConnection isVideoStabilizationSupported]) {
            videoConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
        }
        videoConnection.videoOrientation = self.previewLayer.connection.videoOrientation;
    }
    return _movieFileOutput;
}
步驟四: 獲取AVCaptureSession
- (AVCaptureSession *)session{
    if (!_session) {
        _session = [[AVCaptureSession alloc]init];
        // 設(shè)置視頻質(zhì)量
        if ([_session canSetSessionPreset:AVCaptureSessionPresetLow]) {
            [_session setSessionPreset:AVCaptureSessionPresetLow];
        }
        //增加videoinput
        if ([_session canAddInput:self.videoInput]) {
            [_session addInput:self.videoInput];
        }
        //增加videoinput
        if ([_session canAddInput:self.audioInput]) {
            [_session addInput:self.audioInput];
        }
        //增加fileOutput
        if ([_session canAddOutput:self.movieFileOutput]) {
            [_session addOutput:self.movieFileOutput];
        }
    }
    return _session;
}

這個(gè)地方需要注意下,每次我們更改AVCaptureSession的屬性的時(shí)候我們都需要:

[session beginConfiguration];
// Remove an existing capture device.
// Add a new capture device.
// Reset the preset.
[session commitConfiguration];

現(xiàn)在就可以錄制視頻并寫入文件了

為了實(shí)時(shí)查看我們錄制的內(nèi)容,我們加一個(gè)預(yù)覽層。

-(AVCaptureVideoPreviewLayer *)previewLayer{
    if (!_previewLayer) {
        _previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
        _previewLayer.frame = [UIScreen mainScreen].bounds;
        _previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        _previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
    }
    return _previewLayer;
}

在控制器里面調(diào)用session的startRuning方法,這個(gè)時(shí)候只是采集到了視頻顯示在了預(yù)覽層上面,并未開始錄制。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.recodingView.delegate = self;
    [self.view.layer insertSublayer:self.previewLayer atIndex:0];
    [self.session startRunning];
   
   
}

點(diǎn)擊錄制視頻,recodingView是我自定義的控件。

-(void)writePath{
    if ([self.movieFileOutput isRecording] ) {
        [self.movieFileOutput stopRecording];
        return;
    }
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init ];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString * fileName = [[dateFormatter stringFromDate:[NSDate date]] stringByAppendingString:@".mov"];
    NSString * filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:fileName];
    NSURL *filePathUrl = [NSURL fileURLWithPath:filePath];
    //寫文件到指定的路徑,并設(shè)置代理
    [self.movieFileOutput startRecordingToOutputFileURL:filePathUrl recordingDelegate:self];
}

設(shè)置代理,在視頻錄制的過程中會(huì)發(fā)生許多情況,比如說突然來電話,攝像頭被其他程序占用,系統(tǒng)會(huì)發(fā)送相應(yīng)的通知給我們。

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
      fromConnections:(NSArray *)connections
                error:(NSError *)error{
    BOOL recordSuccessfully = YES;
    if ([error code] != noErr) {
        id value = [[error userInfo] objectForKey:AVErrorRecordingSuccessfullyFinishedKey];
        if (value) {
            recordSuccessfully = [value boolValue];
        }
    }
    
//   有error的話 有可能也是錄制成功了
    /*
    AVErrorMaximumDurationReached  時(shí)間限制
    AVErrorMaximumFileSizeReached  文件大小限制
    AVErrorDiskFull                磁盤已滿
    AVErrorDeviceWasDisconnected   device連接失敗
    AVErrorSessionWasInterrupted   被切斷(比如說來電話了)
*/
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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