今天接到領(lǐng)導(dǎo)任務(wù)汰蓉,要做視頻驗(yàn)證類(lèi)SDK,涉及到視頻壓縮相關(guān)內(nèi)容棒卷。剛接到任務(wù)懵逼了一秒顾孽,果斷想起AVFoundation這個(gè)Framework,蘋(píng)果已經(jīng)封裝好了一系列的視頻質(zhì)量相關(guān)的方法比规,果斷調(diào)用即可若厚。
首先要導(dǎo)入AVFoundation,并在info.plist文件中添加相應(yīng)的權(quán)限蜒什。
創(chuàng)建需要用到的屬性
@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;
視頻保存路徑测秸,根據(jù)需要自己修改
- (NSString *)videoPath {
? ? [self creatSandBoxFilePathIfNoExist];
? ? NSString *pathDocuments =? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,? ? ? NSUserDomainMask, YES) objectAtIndex:0];
? ? NSString *videoPath = [NSString stringWithFormat:@"%@/Video", pathDocuments];
? ? return? [videoPath stringByAppendingPathComponent:@"test001.mp4"];
}
初始化視頻設(shè)備
- (void)setupCaptureSession {
? ? // 1.獲取視頻設(shè)備
? ? NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
? ? for (AVCaptureDevice *device in devices) {
? ? ? ? ? ? if (device.position == AVCaptureDevicePositionBack) {
? ? ? ? ? ? ? ? ? ? self.videoDevice = device; ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? }
// 2.獲取音頻設(shè)備
? ? self.audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
? ? // 3.創(chuàng)建視頻輸入
? ? NSError *error = nil;
? ? self.videoInput = [AVCaptureDeviceInput deviceInputWithDevice:self.videoDevice error:&error];
? ? if (error) { ? ? ? ? return; ? ? }
? ? // 4.創(chuàng)建音頻輸入
? ? self.audioInput = [AVCaptureDeviceInput deviceInputWithDevice:self.audioDevice error:&error];
? ? if (error) { ? ? ? ? return; ? ? }
? ? // 5.創(chuàng)建視頻輸出
? ? self.movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
? ? // 6.建立會(huì)話
? ? 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.預(yù)覽畫(huà)面
? ? self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
? ? [self.view.layer addSublayer:self.movieLayer];
}
其中self.captureSession.sessionPreset = AVCaptureSessionPresetMedium;是用來(lái)調(diào)節(jié)視頻質(zhì)量的,蘋(píng)果定義好了很多不同的尺寸和質(zhì)量灾常,選擇自己需要的就好霎冯。
之后調(diào)用startRunning即可看見(jiàn)預(yù)覽的界面。
- (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];
? ? ? ? }
}
要獲取視頻路徑钞瀑,需要實(shí)現(xiàn)代理方法
-(void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error? {
? ? NSLog(@"vedioOutPut--->%@",outputFileURL);
}
到此沈撞,就可以獲取視頻保存的路徑了。當(dāng)然也可以使用
UISaveVideoAtPathToSavedPhotosAlbum([outputFileURL path], nil, nil, nil);
將視頻保存至用戶(hù)相冊(cè)中雕什。