In iOS video processing, video decomposition images and image compositing videos are frequently encountered in iOS video processing, and this blog post analyzes the conversion of iOS video images to two parts.
(1) Video decomposition image
Here the video decomposition images using Avassetimagegenerator, using this class can be easily implemented under different time stamps, video frame capture. Note that this method of video decomposition of picture frames is placed in child threads, and UI update operations are placed in the main thread. Here's a look at the core code:
_imagegenerator = [[Avassetimagegenerator alloc] initwithasset:_asset];
images = [[Nsmutablearray alloc]initwithcapacity:10];
_imagegenerator.maximumsize = thumbnail_size;
Cmtime duration = _asset.duration;
Cmtimevalue intervalseconds = DURATION.VALUE/3;
Cmtime time = Kcmtimezero;
Nsmutablearray *times = [Nsmutablearray array];
for (Nsuinteger i = 0; i < 3; i++) {
[Times Addobject:[nsvalue Valuewithcmtime:time];
Time = Cmtimeadd (time, Cmtimemake (Intervalseconds, Duration.timescale));
}
[_imagegenerator generatecgimagesasynchronouslyfortimes:times completionhandler:^ (CMTime requestedTime, Cgimagerefcgimage,
Cmtime Actualtime,
Avassetimagegeneratorresult result,
Nserror *error) {
if (cgimage) {
UIImage *image = [UIImage imagewithcgimage:cgimage];
[Images Addobject:image];
}
if (Images.count = = 3) {
Dispatch_async (Dispatch_get_main_queue (), ^{
Self.imageview1.image = [images objectatindex:0];
Self.imageview2.image = [images objectatindex:1];
Self.imageview3.image = [images objectatindex:2];
});
}
}];
After the decomposition of the frame effect is as follows: Image composition video effects are as follows:
(2) Image synthesis video
The method of image synthesis video is relatively more complicated, and the main class we use is this:
Avassetwriterinputpixelbufferadaptor. The difference is that we also set up 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: [NSNumber numberwithint: (int) width],
Avvideoheightkey: [NSNumber numberwithint: (int) height]};
2.4 Image Synthesis started
Cmtime lasttime = Cmtimemake (i, Self.frameTime.timescale);
Cmtime presenttime = Cmtimeadd (Lasttime, self.frametime);
[Self.bufferadapter Appendpixelbuffer:samplebuffer Withpresentationtime:presenttime];
IOS video exploded picture, picture compositing video