iOS的视频捕获、压缩及保存

今天接到领导任务,要做视频验证类SDK,涉及到视频压缩相关内容。刚接到任务懵逼了一秒,果断想起AVFoundation这个Framework,苹果已经封装好了一系列的视频质量相关的方法,果断调用即可。



首先要导入AVFoundation,并在info.plist文件中添加相应的权限。

info.plist

创建需要用到的属性

@property (nonatomic, strong) AVCaptureSession *captureSession;

@property (nonatomic, strong) AVCaptureDevice *videoDevice;

@property (nonatomic, strong) AVCaptureDevice *audioDevice;

@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;

@property (nonatomic, strong) AVCaptureDeviceInput *audioInput;

@property (nonatomic, strong) AVCaptureMovieFileOutput *movieFileOutput;

@property (nonatomic, strong) AVCaptureVideoPreviewLayer *movieLayer;

视频保存路径,根据需要自己修改

- (NSString *)videoPath {

    [self creatSandBoxFilePathIfNoExist];

    NSString *pathDocuments =                                                        [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,      NSUserDomainMask, YES) objectAtIndex:0];

    NSString *videoPath = [NSString stringWithFormat:@"%@/Video", pathDocuments];

    return  [videoPath stringByAppendingPathComponent:@"test001.mp4"];

}

初始化视频设备

- (void)setupCaptureSession {

    // 1.获取视频设备

    NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    for (AVCaptureDevice *device in devices) {

            if (device.position == AVCaptureDevicePositionBack) {

                    self.videoDevice = device;             break;

            }

    }

// 2.获取音频设备

    self.audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];

    // 3.创建视频输入

    NSError *error = nil;

    self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error];

    if (error) {         return;     }

    // 4.创建音频输入

    self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.audioDevice error:&error];

    if (error) {         return;     }

    // 5.创建视频输出

    self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

    // 6.建立会话

    self.captureSession = [[AVCaptureSession alloc] init];     self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;

    if ([self.captureSession canAddInput:self.videoInput]) {

            [self.captureSession addInput:self.videoInput];

        }

    if ([self.captureSession canAddInput:self.audioInput]) {

            [self.captureSession addInput:self.audioInput];

    }

    if ([self.captureSession canAddOutput:self.movieFileOutput]) {                    

    [self.captureSession addOutput:self.movieFileOutput];

    }

    // 7.预览画面

    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];

    [self.view.layer addSublayer:self.movieLayer];

}

其中self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;是用来调节视频质量的,苹果定义好了很多不同的尺寸和质量,选择自己需要的就好。

之后调用startRunning即可看见预览的界面。

- (void)startSession {

        if(![self.captureSession isRunning]) {

            [self.captureSession startRunning];

        }

}

- (void)stopSession {

    if([self.captureSession isRunning]) {

                [self.captureSession stopRunning];

        }

}

记得自定义拍摄按钮

- (void)startRecord {

    [self.movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:[self videoPath]] recordingDelegate:self];

}

- (void)stopRecord {

        if ([self.movieFileOutput isRecording]) {

                [self.movieFileOutput stopRecording];

        }

}

要获取视频路径,需要实现代理方法

-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error  {

    NSLog(@"vedioOutPut--->%@",outputFileURL);

}

到此,就可以获取视频保存的路径了。当然也可以使用

UISaveVideoAtPathToSavedPhotosAlbum([outputFileURL path], nil, nil, nil);

将视频保存至用户相册中。

你可能感兴趣的:(iOS的视频捕获、压缩及保存)