日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

IOS 视频分解图片、图片合成视频

發(fā)布時間:2023/12/13 综合教程 24 生活家
生活随笔 收集整理的這篇文章主要介紹了 IOS 视频分解图片、图片合成视频 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在IOS視頻處理中,視頻分解圖片和圖片合成視頻是IOS視頻處理中常常遇到的問題。這篇博客就這兩個部分對IOS視頻圖像的相互轉換做一下分析。

(1)視頻分解圖片

這里視頻分解圖片使用的是AVAssetImageGenerator。利用這個class能夠非常方便的實現(xiàn)不同一時候間戳下,視頻幀的抓取。注意一般這樣的視頻分解圖片幀的方法都是放在子線程中的,而UI更新操作都是放在主線程中的。

以下來看看核心代碼:

_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, CGImageRef
cgImage,

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];

});

}

}];

分解之后的幀效果例如以下: 圖片合成視頻效果例如以下:

<img src="http://img.blog.csdn.net/20150727095747931?

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="311" height="574" alt="">

(2)圖片合成視頻

圖片合成視頻的方法相對來說更加復雜一點。我們主要用到的class是這個:

AVAssetWriterInputPixelBufferAdaptor。

不同之處在于這里我們還要設置圖片合成視頻的各種參數(shù),比方幀率,編碼方式等等。

2.1 設置文件封裝類型

AVFileTypeQuickTimeMovie


2.2 設置圖片格式

kCVPixelFormatType_32ARGB

2.3 設置編碼方式、圖片尺寸

NSDictionary *videoSettings =
@{AVVideoCodecKey :
AVVideoCodecH264,

AVVideoWidthKey : [NSNumber
numberWithInt:(int)width],

AVVideoHeightKey : [NSNumber
numberWithInt:(int)height]};

2.4 圖片合成開始

CMTime lastTime =
CMTimeMake(i, self.frameTime.timescale);

CMTime presentTime =
CMTimeAdd(lastTime,
self.frameTime);

[self.bufferAdapter
appendPixelBuffer:sampleBuffer
withPresentationTime:presentTime];

總結

以上是生活随笔為你收集整理的IOS 视频分解图片、图片合成视频的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。