標籤:elb 效果 settings cap rate 2.3 generate 注意 scale
在IOS視頻處理中,視頻分解圖片和圖片合成視頻是IOS視頻處理中常常遇到的問題。這篇部落格就這兩個部分對IOS視頻映像的相互轉換做一下分析。
(1)視頻分解圖片
這裡視頻分解圖片使用的是AVAssetImageGenerator。利用這個class能夠非常方便的實現不同一時候間戳下,視訊框架的抓取。注意一般這樣的視頻分解圖片幀的方法都是放在子線程中的,而UI更新操作都是放在主線程中的。
以下來看看核心代碼:
_imageGenerator = [[AVAssetImageGeneratoralloc] initWithAsset:_asset];
images = [[NSMutableArrayalloc]initWithCapacity:10];
_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));
}
[_imageGeneratorgenerateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, 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];
});
}
}];
分解之後的幀效果例如以下: 圖片合成視頻效果例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="311" height="574" >
(2)圖片合成視頻
圖片合成視頻的方法相對來說更加複雜一點。我們主要用到的class是這個:
AVAssetWriterInputPixelBufferAdaptor。
不同之處在於這裡我們還要設定圖片合成視頻的各種參數,比方幀率,編碼方式等等。
2.1 設定檔案封裝類型
AVFileTypeQuickTimeMovie
2.2 設定圖片格式
kCVPixelFormatType_32ARGB
2.3 設定編碼方式、圖片尺寸
NSDictionary *videoSettings =@{AVVideoCodecKey :AVVideoCodecH264,
AVVideoWidthKey : [NSNumbernumberWithInt:(int)width],
AVVideoHeightKey : [NSNumbernumberWithInt:(int)height]};
2.4 圖片合成開始
CMTime lastTime =CMTimeMake(i, self.frameTime.timescale);
CMTime presentTime =CMTimeAdd(lastTime, self.frameTime);
[self.bufferAdapterappendPixelBuffer:sampleBuffer withPresentationTime:presentTime];
IOS 視頻分解圖片、圖片合成視頻