IOS video cut, rotate, add audio video, add watermark, video export

Summary

Video processing is mainly used in the following
AVMutableComposition, AVMutableVideoComposition, AVMutableAudioMix, AVMutableVideoCompositionInstruction, AVMutableVideoCompositionLayerInstruction, AVAssetExportSession etc.. The combination of AVMutableComposition can be used to operate the audio and video, AVMutableVideoComposition can be used to operate on the video, the AVMutableAudioMix class is added to the audio video, AVMutableVideoCompositionInstruction and AVMutableVideoCompositionLayerInstruction are generally used to add a watermark to the video or video rotation direction, AVAssetExportSession is used for video export operations. Attention is when the App into the background, will be limited to operating on the use of GPU code, will cause the collapse of the video processing most of these features will be used to GPU, so it is necessary to do the corresponding error prevention processing.
here I will use the official Apple Demo “AVSimpleEditoriOS” as a case to explain the case, using the Command design pattern to organize the code, which contains a number of AVSECommand base class sub class Command common attributes. This paper briefly introduces the video related operations, explains some related operations, and marked some key code, hope this paper can play a good effect, so that everyone on the video clips have a preliminary impression, then you should be modified according to the official Apple Demo content. You can download the appropriate Apple official Demo run results.

@property AVMutableComposition *mutableComposition; @property AVMutableVideoComposition *mutableVideoComposition; @property AVMutableAudioMix *mutableAudioMix; @property CALayer *watermarkLayer;
IOS video cut, rotate, add audio video, add watermark, video export
Command design pattern

Video cut

IOS video cut, rotate, add audio video, add watermark, video export
video head cut code
  1. Get video and audio resources
  2. Create AVMutableComposition object
  3. Add video resources to the AVMutableComposition object, and set the time and insertion points for the video resources
  4. To add audio resources to the AVMutableComposition object, and set the time and insertion points for the audio resource

Video rotation

When check the information about video rotation at a very good reference, here for everyone to look at Amway
http://blog.csdn.net/likendsl/article/details/7595611 is about the principle of matrix operations, matrix operation on the related content of view.

IOS video cut, rotate, add audio video, add watermark, video export
video rotation code Figure 1
IOS video cut, rotate, add audio video, add watermark, video export
video rotation code Figure 2
IOS video cut, rotate, add audio video, add watermark, video export
video rotation code Figure 3
  1. Get video and audio resources
  2. Create AVMutableComposition object
  3. Add video resources to the AVMutableComposition object, and set the time and insertion points for the video resources
  4. To add audio resources to the AVMutableComposition object, and set the time and insertion points for the audio resource
  5. Set rotation matrix transform
  6. Create AVMutableVideoComposition object
  7. Set the video to render wide and Frame
  8. Create a video composition directive AVMutableVideoCompositionInstruction, and set the instruction in the video’s action time range and rotation matrix transformation
  9. Create a video composition layer directive AVMutableVideoCompositionLayerInstruction, and set the layer instruction in the video time range and rotation matrix transformation
  10. Put the video layer command in the video command and put it into the video combination object

Here are the different rotation angle case code, hoping to help you save your time.

CGAffineTransform translateToCenter; if (self.degrees! = 0) {CGAffineTransform mixedTransform; if (self.degrees = = 90) {/ / clockwise 90 degrees NSLog (@ "video rotation of 90 degrees, home button on the left"); translateToCenter = CGAffineTransformMakeTranslation (mixedVideoTrack.naturalSize.height, 0); mixedTransform = CGAffineTransformRotate (translateToCenter, M_PI_2); waterMarkVideoComposition.renderSize = CGSizeMake (mixedVideoTrack.naturalSize.height, mixedVideoTrack.naturalSize.width);}else if (self.degrees = = 180) {/ / clockwise 180 degrees NSLog (@ "video rotation of 180 degrees, home button on tra"); NslateToCenter = CGAffineTransformMakeTranslation (mixedVideoTrack.naturalSize.width, mixedVideoTrack.naturalSize.height); mixedTransform = CGAffineTransformRotate (translateToCenter, M_PI); waterMarkVideoComposition.renderSize = CGSizeMake (mixedVideoTrack.naturalSize.width, mixedVideoTrack.naturalSize.height);}else if (self.degrees = = 270) {/ / clockwise 270 degrees NSLog (@ "video rotation of 270 degrees, home button on the right"); CGAffineTransformMakeTranslation (translateToCenter = 0, assetVideoTrack.naturalSize.width); mixedTransform = CGAffineTransformRotate (translateToCenter, M_PI_2*3.0); waterMarkVideoComposition.renderSize = CGSizeMa Ke (mixedVideoTrack.naturalSize.height, mixedVideoTrack.naturalSize.width);} AVMutableVideoCompositionInstruction *roateInstruction [AVMutableVideoCompositionInstruction = videoCompositionInstruction]; roateInstruction.timeRange = CMTimeRangeMake (kCMTimeZero, [mixComposition, duration]); AVMutableVideoCompositionLayerInstruction *roateLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:mixedVideoTrack]; [roateLayerInstruction setTransform:mixedTransform atTime:kCMTimeZero]; roateInstruction.layerInstructions = @[roateLayerInstruction]; / / video will be added to the direction of rotation of the video processing waterMa RkVideoComposition.instructions = @[roateInstruction];}

Video audio

IOS video cut, rotate, add audio video, add watermark, video export
video to add audio code Figure 1
IOS video cut, rotate, add audio video, add watermark, video export
video to add audio code Figure 2
  1. Get video and audio resources
  2. Create AVMutableComposition object
  3. Add video resources to the AVMutableComposition object, and set the time and insertion points for the video resources
  4. To add audio resources to the AVMutableComposition object, and set the time and insertion points for the audio resource
  5. Add an audio resource to be added to the AVMutableComposition object, and set the time, insert, and blend modes for the audio resource

Video watermarking

IOS video cut, rotate, add audio video, add watermark, video export
video watermark 1
IOS video cut, rotate, add audio video, add watermark, video export
video watermark 2
  1. Get video and audio resources
  2. Create AVMutableComposition object
  3. Add video resources to the AVMutableComposition object, and set the time and insertion points for the video resources
  4. To add audio resources to the AVMutableComposition object, and set the time and insertion points for the audio resource
  5. Create the video combiner object AVMutableVideoComposition and set the frame and render width high
  6. Create a video assembler command object, set the scope of the instruction
  7. Create a video assembler Layer command object, set the scope of the instruction
  8. The video assembler Layer command object is placed in the video assembler command object
  9. Video assembler command object in video combiner object
  10. Create the watermark layer Layer and set the location of the frame and watermark, and the watermark into the video combiner

Video export

IOS video cut, rotate, add audio video, add watermark, video export
video export
  1. Create output path
  2. Create AVAssetExportSession video export object based on AVMutableComposition object
  3. Set AVAssetExportSession AVMutableVideoComposition object, AVMutableAudioMix object, video export path, video export format
  4. Asynchronous export video, according to the results of the corresponding processing.

Http://blog.csdn.net/likendsl/article/details/7595611

If you think this article is helpful to you, please give me a love, let me know this article played its role! Thank you