/// 视频生成
-(void)videoGenerated
{
self.theVideoPath = [self videoSavingPath:self.videoName];
//定义视频的大小320 480 倍数
CGSize size = CGSizeMake(320,480); // 192 256
// CGSize size = CGSizeMake(320,480); // 192 256
NSError *error = nil;
// 转成UTF-8编码
unlink([self.theVideoPath UTF8String]);
NSLog(@"path->%@",self.theVideoPath);
AVAssetWriter *videoWriter = [[AVAssetWriter alloc]initWithURL:[NSURL fileURLWithPath:self.theVideoPath]fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(videoWriter);
if(error) {
NSLog(@"error =%@",[error localizedDescription]);
return;
}
//mp4的格式设置 编码格式 宽度 高度
NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264,AVVideoCodecKey,
[NSNumber numberWithInt:size.width],AVVideoWidthKey,
[NSNumber numberWithInt:size.height],AVVideoHeightKey,nil];
AVAssetWriterInput *writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
NSDictionary *sourcePixelBufferAttributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:kCVPixelFormatType_32ARGB],kCVPixelBufferPixelFormatTypeKey,nil];
// AVAssetWriterInputPixelBufferAdaptor提供CVPixelBufferPool实例,
// 可以使用分配像素缓冲区写入输出文件。使用提供的像素为缓冲池分配通常
// 是更有效的比添加像素缓冲区分配使用一个单独的池
AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:sourcePixelBufferAttributesDictionary];
NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);
if([videoWriter canAddInput:writerInput]){
NSLog(@"11111");
}else{
NSLog(@"22222");
}
[videoWriter addInput:writerInput];
[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];
//合成多张图片为一个视频文件
dispatch_queue_t dispatchQueue = dispatch_queue_create("mediaInputQueue",NULL);
int __block frame = 0;
__weak typeof(self)weakSelf = self;
[writerInput requestMediaDataWhenReadyOnQueue:dispatchQueue usingBlock:^{
while([writerInput isReadyForMoreMediaData]) {
if(++frame >= [self->_imageArray count] * 2) {
[writerInput markAsFinished];
[videoWriter finishWritingWithCompletionHandler:^{
NSLog(@"完成");
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"视频合成完毕");
[HTipManage deallocView];
if (weakSelf.imageArray.count > 0 && weakSelf.imageArray!=nil) {
[[NSNotificationCenter defaultCenter]postNotificationName:@"SavedSuccessfuly" object:[self.imageArray objectAtIndex:0]];
}
if (weakSelf.videoUrl) {
weakSelf.videoUrl(weakSelf.theVideoPath);
}
}];
}];
break;
}
CVPixelBufferRef buffer = NULL;
int idx = frame / 2;
NSLog(@"idx==%d",idx);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
buffer = (CVPixelBufferRef)[self pixelBufferFromCGImage:[[weakSelf.imageArray objectAtIndex:idx]CGImage]size:size];
if(buffer){
//设置每秒钟播放图片的个数
if(![adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(frame,25)]) {
NSLog(@"FAIL");
} else {
NSLog(@"OK");
}
CFRelease(buffer);
}
}
}];
}
多张图片生成视频
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 将做工程过程较好的代码段备份一下,下面的代码段是关于一段将多张图片生成AVI视频的C++的代码,应该能对各朋友有一...
- 这个小工具制作的目的是为了将多张图片组合后生成一张动态的GIF图片。设置界面化的操作,只需要将选中的图片导入最后直...
- 电脑图片合成视频怎么做?图片视频制作用什么软件好?现在大家的照片或图片很多,其实在电脑上把图片做成视频是非常方便的...
- 下面我来介绍一下如何使用OSS上传图片: 下载链接:下载链接 首先,我们需要注册账号,拿到秘钥: 下面是我封装的几...