logo头像
Snippet 博客主题

iOS视频开发(一)

本文于2007天之前发表,文中内容可能已经过时。

一、UIImagePickerController

UIImagePickerControllerUIKit框架里面的一个class,通过这个系统提供的class我们可以简单的是实现拍照、录制视频和音频。

三个步骤:
  1. 当前控制器present一个UIImagePickerController
  2. 在当前界面就可以拍照、录制视频和音频
  3. 实现UIImagePickerControllerdelegate,在delegate可以获取录制的视频和音频,来进行相应的操作.
定制化UIImagePickerController

//    查看摄像头是否可用
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO) {   
  return;
}
UIImagePickerController *imagePick = [[UIImagePickerController alloc]init];
imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
//    我们还可以设置照片和视频拍摄的质量、是否可以开启闪光灯、是否开启手电筒
//    还可以单独设置只支持视频模式
//    imagePick.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
imagePick.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//    UINavigationControllerDelegate,UIImagePickerControllerDelegate;
imagePick.delegate = self;
//拍照或者录制结束后是否可以编辑
imagePick.allowsEditing = NO;
[self presentViewController:imagePick animated:YES completion:nil];
界面的自定义

cameraOverlayView属性可以自定义UIImagePickerController界面顶部的控件,但是只在UIImagePickerControllermediaTypesUIImagePickerControllerSourceTypeCamera时可用。

实现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

要获取摄像机捕捉到的视频或者麦克风捕捉到的音频,我们需要对象表示inputsoutputs,并使用AVCaptureSession的实例来协调它们之间的数据流。

  1. AVCaptureDevice 对象,表示声音或者视频采集设备,对应摄像头和麦克风。
  2. AVCaptureInput的子类,配置输入端口。
  3. AVCaptureOutput的子类, 输出采集到的视频或者图像。
  4. AVCaptureSession来协调从输入到输出的数据流。
步骤一: 创建AVCaptureDevice 对象

因为我们需要录制视频和音频所以我们需要视频的AVCaptureDevice和音频的AVCaptureDevice。

//我们同时获取了前摄像头和后摄像头因为等会我们要手动切换
//获取音频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]) {
            //可以设置是否开启闪光灯,是否开始HDR、视频防抖、白平衡什么的
            //设置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

每个AVCaptureDevice对应一个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);
        //设置视频录制时间限制 kCMTimeInvalid(无限制)
        _movieFileOutput.maxRecordedDuration = kCMTimeInvalid;
        // 文件大小限制
        //_movieFileOutput.maxRecordedFileSize = 1024 * 1024;
        AVCaptureConnection *videoConnection = [_movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
        // 是否支持科学防抖
        if ([videoConnection isVideoStabilizationSupported]) {
            videoConnection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
        }
        videoConnection.videoOrientation = self.previewLayer.connection.videoOrientation;
    }
    return _movieFileOutput;
}
步骤四: 获取AVCaptureSession
- (AVCaptureSession *)session{
    if (!_session) {
        _session = [[AVCaptureSession alloc]init];
        // 设置视频质量
        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;
}

这个地方需要注意下,每次我们更改AVCaptureSession的属性的时候我们都需要:

[session beginConfiguration];
// Remove an existing capture device.
// Add a new capture device.
// Reset the preset.
[session commitConfiguration];
现在就可以录制视频并写入文件了

为了实时查看我们录制的内容,我们加一个预览层。

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

在控制器里面调用sessionstartRuning方法,这个时候只是采集到了视频显示在了预览层上面,并未开始录制。

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

点击录制视频,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];
    //写文件到指定的路径,并设置代理
    [self.movieFileOutput startRecordingToOutputFileURL:filePathUrl recordingDelegate:self];
}

设置代理,在视频录制的过程中会发生许多情况,比如说突然来电话,摄像头被其他程序占用,系统会发送相应的通知给我们。

- (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  时间限制
    AVErrorMaximumFileSizeReached  文件大小限制
    AVErrorDiskFull                磁盘已满
    AVErrorDeviceWasDisconnected   device连接失败
    AVErrorSessionWasInterrupted   被切断(比如说来电话了)
*/
    
}
上一篇