Foreword: this series article aims to provide convenience for the new person who just contacts short video development, write bad place, also ask each way great god to instruct. As we all know, it is very difficult to develop audio and video related projects. Based on OpenCV, OpenGL, FFMPEG and other frameworks, iOS also involves GPUImage, AVFoundation and other frameworks, and the language level involves C and C++ languages, which can be said that no one can complete the work.

Body:

Core idea: Shooting with AVFoundation is tedious, while GPUImage encapsulates this layer, so we can directly use GPUImage to complete shooting function.

Specific steps

1. Create a project. In the Infoplist file, set permissions for microphone, camera, album, and image

<! - photo album - > < key > NSPhotoLibraryUsageDescription < / key > < string > App need your approval, to access photo album < / string > <! -- Camera --> <key>NSCameraUsageDescription</key> <string>App requires your consent to access the camera </string> <! - the microphone - > < key > NSMicrophoneUsageDescription < / key > < string > App need your approval, to access the microphone < / string > <! - image - > < key > NSPhotoLibraryAddUsageDescription < / key > < string > App need your approval, can save the photos to your album < / string >Copy the code

2. It is recommended to guide GPUImage through Cocoapods based on GPUImage. After GPUImage is introduced, modify the following parts:

1. Add isNeedBreakAudioWhiter @property (nonatomic, assign) BOOL isNeedBreakAudioWhiter; Line 377 in gpuimagemoviewriter. m is modified as follows:if (CMTIME_IS_INVALID(startTime))
     {
       if (_isNeedBreakAudioWhiter) {
 
 
      }else{
 
      runSynchronouslyOnContextQueue(_movieWriterContext, ^{
      if((audioInputReadyCallback == NULL) && (assetWriter.status ! = AVAssetWriterStatusWriting)) { [assetWriter startWriting]; } [assetWriter startSessionAtSourceTime:currentSampleTime]; startTime = currentSampleTime; }); }} 3. GPUImageMovieWriter initialization set isNeedBreakAudioWhiter =YES;Copy the code

3. Initialize the camera and filterView. FilterView is used to deal with beauty

/ / to record relevant AVCaptureSessionPreset1280x720 video resolution AVCaptureDevicePositionBack rear camera videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:AVCaptureDevicePositionBack];if([videocamera. inputCamera lockForConfiguration:nil]) {// Auto focusif ([videoCamera.inputCamera isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
            [videoCamera.inputCamera setFocusMode:AVCaptureFocusModeContinuousAutoFocus]; } // Auto exposureif ([videoCamera.inputCamera isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
            [videoCamera.inputCamera setExposureMode:AVCaptureExposureModeContinuousAutoExposure]; } // Automatic white balanceif ([videoCamera.inputCamera isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]) {
            [videoCamera.inputCamera setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]; } [videoCamera.inputCamera unlockForConfiguration]; } / / output direction for vertical screen videoCamera. OutputImageOrientation = UIInterfaceOrientationPortrait; [videoCamera addAudioInputsAndOutputs]; videoCamera.horizontallyMirrorFrontFacingCamera = YES; videoCamera.horizontallyMirrorRearFacingCamera = NO; // Display view filterView =[[GPUImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; [self.view addSubview:filterView]; // filter = [[GPUImageBilateralFilter alloc] init]; // [videoCamera addTarget:filter]; // [filter addTarget:filterView]; [videoCamera addTarget:filterView]; // The camera starts [videoCamera startCameraCapture];Copy the code

4. Initialize the MovieWriter

VideoURL namely video files stored URL paths = NSSearchPathForDirectoriesInDomains NSArray * (NSDocumentDirectory NSUserDomainMask, YES); NSString *videoPath = [[paths objectAtIndex:0] stringByAppendingPathComponent@"tets.MOV"]; unlink([path UTF8String]); videoURL =[NSURL fileURLWithPath:path]; It is important to call the unlink method, otherwise the write may fail. / / write movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL: videoURL size: CGSizeMake (720.0, 1280.0)]. / / set to liveVideo movieWriter. IsNeedBreakAudioWhiter = YES; movieWriter.encodingLiveVideo = YES; movieWriter.shouldPassthroughAudio =YES; // [filter addTarget:movieWriter]; [videoCamera addTarget:movieWriter]; / / set up the voice videoCamera. AudioEncodingTarget = movieWriter;Copy the code

5. Start recording and pause

StartRecording [movieWriter startRecording]; Stop recording [movieWriter finishRecording]; [filter removeTarget:movieWriter]; videoCamera.audioEncodingTarget = nil;Copy the code

From there, we are ready for a simple video recording function, which I will cover in the next few articles as well as switching between front and rear cameras, flash, breakpoint shooting, video compositing, and more.