In iOS video processing, video exploded images and image compositing videos are frequently encountered problems in iOS video processing. This blog on the two parts of the iOS video image of the mutual conversion to do a bit of analysis.
(1) Video decomposition image
Here the video exploded images using a Avassetimagegenerator. using this class can be very convenient to achieve a different time between the Poke, video frame capture. Note that in general, the method of decomposing picture frames is placed in a child thread, and the UI update operation is placed in the main thread.
Take a look at the core code:
_imagegenerator = [[ Avassetimagegenerator alloc " initwithasset : Span style= "COLOR: #4f8187" >_asset ];
images = [[nsmutablearrayalloc]initwithcapacity: Ten ];
_imagegenerator. MaximumSize = thumbnail_size ;
cmtime duration = _asset. duration;
cmtimevalue intervalseconds = Duration. value /3;
cmtime Time = Kcmtimezero;
nsmutablearray *times = [nsmutablearrayarray];
for (nsuinteger i =0; i < 3; i++) {
[Timesaddobject: [nsvaluevaluewithcmtime: Time]];
Time =Cmtimeadd(time, cmtimemake(intervalseconds, duration. Timescale));
}
[ _imagegenerator generatecgimagesasynchronouslyfortimes:times Completionhandler:^ ( cmtime Requestedtime, & nbsp , &NB Sp cgimageref Cgimage,
cmtime Actualtime,
Avassetimagegeneratorresult result,
nserror *error) {
if (Cgimage) {
UIImage *image = [UIImageimagewithcgimage: cgimage];
[imagesaddobject: image];
}
if (images. count = =3) {
dispatch_async(dispatch_get_main_queue(), ^{
Self. Imageview1. image = [imagesobjectatindex:0];
Self. imageview2. image = [imagesobjectatindex:1];
Self. imageview3. image = [imagesobjectatindex:2];
});
}
}];
The frame effect after decomposition is as follows: Picture compositing video effects such as the following:
Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center "width=" 311 "height=" 574 ">
(2) image synthesis Video
The method of image synthesis video is a bit more complicated. Our main use of the class is this:
Avassetwriterinputpixelbufferadaptor.
The difference is that we also have to set the various parameters of the image synthesis video, such as frame rate, encoding method and so on.
2.1 Set File Encapsulation type
Avfiletypequicktimemovie
2.2 Format Picture
Kcvpixelformattype_32argb
2.3 Setting the encoding method, image size
Nsdictionary *videosettings =@{avvideocodeckey : AVVideoCodecH264 ,
avvideowidthkey : [nsnumbernumberwithint:(int) width],
avvideoheightkey : [nsnumbernumberwithint:(int) height]};
2.4 Image Synthesis starts
cmtime lasttime =cmtimemake(i, self. Frametime. timescale);
cmtime presenttime =cmtimeadd(lasttime, self. Frametime);
[Self. Bufferadapter appendpixelbuffer: Samplebuffer withpresentationtime:p resenttime];
IOS video exploded images, image compositing videos