iphone ios 视频特效 将图像插入到视频中(转)
来源:互联网 发布:mysql 主死了 编辑:程序博客网 时间:2024/05/13 22:03
如果对av foundation 不熟悉得话,建议先看看wwdc 2010 关于av foundation得讲座。http://blog.csdn.net/linzhiji/article/details/6752722
[html] view plaincopy
- - (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image andSize:(CGSize) size
- {
- NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
- [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
- nil];
- CVPixelBufferRef pxbuffer = NULL;
- CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width,
- size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
- &pxbuffer);
- NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
- CVPixelBufferLockBaseAddress(pxbuffer, 0);
- void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
- NSParameterAssert(pxdata != NULL);
- CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = CGBitmapContextCreate(pxdata, size.width,
- size.height, 8, 4*size.width, rgbColorSpace,
- kCGImageAlphaNoneSkipFirst);
- NSParameterAssert(context);
- CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
- CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
- CGImageGetHeight(image)), image);
- CGColorSpaceRelease(rgbColorSpace);
- CGContextRelease(context);
- CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
- return pxbuffer;
- }
[html] view plaincopy
- - (void) writeImages:(NSArray *)imagesArray ToMovieAtPath:(NSString *) path withSize:(CGSize) size
- inDuration:(float)duration byFPS:(int32_t)fps{
- //Wire the writer:
- NSError *error = nil;
- AVAssetWriter *videoWriter = [[[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path]
- fileType:AVFileTypeQuickTimeMovie
- error:&error] autorelease];
- NSParameterAssert(videoWriter);
- NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
- AVVideoCodecH264, AVVideoCodecKey,
- [NSNumber numberWithInt:size.width], AVVideoWidthKey,
- [NSNumber numberWithInt:size.height], AVVideoHeightKey,
- nil];
- AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput
- assetWriterInputWithMediaType:AVMediaTypeVideo
- outputSettings:videoSettings] retain];
- AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor
- assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput
- sourcePixelBufferAttributes:nil];
- NSParameterAssert(videoWriterInput);
- NSParameterAssert([videoWriter canAddInput:videoWriterInput]);
- [videoWriter addInput:videoWriterInput];
- //Start a session:
- [videoWriter startWriting];
- [videoWriter startSessionAtSourceTime:kCMTimeZero];
- //Write some samples:
- CVPixelBufferRef buffer = NULL;
- int frameCount = 0;
- int imagesCount = [imagesArray count];
- float averageTime = duration/imagesCount;
- int averageFrame = (int)(averageTime * fps);
- for(UIImage * img in imagesArray)
- {
- buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:size];
- BOOL append_ok = NO;
- int j = 0;
- while (!append_ok && j < 30)
- {
- if (adaptor.assetWriterInput.readyForMoreMediaData)
- {
- printf("appending %d attemp %d\n", frameCount, j);
- CMTime frameTime = CMTimeMake(frameCount,(int32_t) fps);
- float frameSeconds = CMTimeGetSeconds(frameTime);
- NSLog(@"frameCount:%d,kRecordingFPS:%d,frameSeconds:%f",frameCount,fps,frameSeconds);
- append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
- if(buffer)
- [NSThread sleepForTimeInterval:0.05];
- }
- else
- {
- printf("adaptor not ready %d, %d\n", frameCount, j);
- [NSThread sleepForTimeInterval:0.1];
- }
- j++;
- }
- if (!append_ok) {
- printf("error appending image %d times %d\n", frameCount, j);
- }
- frameCount = frameCount + averageFrame;
- }
- //Finish the session:
- [videoWriterInput markAsFinished];
- [videoWriter finishWriting];
- NSLog(@"finishWriting");
- }
0 0
- iphone ios 视频特效 将图像插入到视频中(转)
- iphone ios 视频特效 将图像插入到视频中(转)
- iphone ios 视频特效,视频合成
- iphone ios 图像/视频处理
- iphone ios 图像/视频处理
- 如何将音视频发布到移动设备,android,ios,iphone,ipad等
- iphone 通过AVAssetWriter将录像视频写到指定文件
- iphone 通过AVAssetWriter将录像视频写到指定文件
- 将视频图像转化为灰度视频
- 课件中插入视频
- 网页中插入视频
- 网页中插入视频
- html5中插入视频
- html5中插入视频
- 网页中插入视频
- 将图像写成avi视频
- iphone ios 音频添加,合成,替换到视频(里的音轨)
- iphone ios 音频添加,合成,替换到视频(里的音轨)
- CMTimeMakeWithSeconds & CMTimeMake
- iPad CSS控制横屏/竖屏布局(Landscape/Portrait Modes)
- IOS相册功能
- 获取系统相册里面的照片
- 获取ipod里面的音乐 用AVAudioPlayer播放
- iphone ios 视频特效 将图像插入到视频中(转)
- Unity3D 游戏引擎之IOS截屏 话筒录音 录制截屏视频保存沙盒(另类实现方法 )
- 保存ipod音乐到沙盒中
- iOS的UDID废用以及UUID配合keychain的替换方案实现
- 在iOS开发中使用FMDB
- ios检测网络,无网络提示并直接进入设置页面
- Open Source Projects
- 在anyView里加badgeValue.
- tableview的数据载入完成的判断