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
  1. - (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image andSize:(CGSize) size  
  2. {  
  3.     NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:  
  4.                              [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,  
  5.                              [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,  
  6.                              nil];  
  7.     CVPixelBufferRef pxbuffer = NULL;  
  8.       
  9.     CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, size.width,  
  10.                                           size.height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,   
  11.                                           &pxbuffer);  
  12.     NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);  
  13.       
  14.     CVPixelBufferLockBaseAddress(pxbuffer, 0);  
  15.     void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);  
  16.     NSParameterAssert(pxdata != NULL);  
  17.       
  18.     CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();  
  19.     CGContextRef context = CGBitmapContextCreate(pxdata, size.width,  
  20.                                                  size.height, 8, 4*size.width, rgbColorSpace,   
  21.                                                  kCGImageAlphaNoneSkipFirst);  
  22.     NSParameterAssert(context);  
  23.     CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));  
  24.     CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),   
  25.                                            CGImageGetHeight(image)), image);  
  26.     CGColorSpaceRelease(rgbColorSpace);  
  27.     CGContextRelease(context);  
  28.       
  29.     CVPixelBufferUnlockBaseAddress(pxbuffer, 0);  
  30.       
  31.     return pxbuffer;  
  32. }  

[html] view plaincopy
  1. - (void) writeImages:(NSArray *)imagesArray ToMovieAtPath:(NSString *) path withSize:(CGSize) size   
  2.           inDuration:(float)duration byFPS:(int32_t)fps{  
  3.     //Wire the writer:  
  4.     NSError *error = nil;  
  5.     AVAssetWriter *videoWriter = [[[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path]   
  6.                                                            fileType:AVFileTypeQuickTimeMovie  
  7.                                                               error:&error] autorelease];  
  8.     NSParameterAssert(videoWriter);  
  9.       
  10.     NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:  
  11.                                    AVVideoCodecH264, AVVideoCodecKey,  
  12.                                    [NSNumber numberWithInt:size.width], AVVideoWidthKey,  
  13.                                    [NSNumber numberWithInt:size.height], AVVideoHeightKey,  
  14.                                    nil];  
  15.       
  16.     AVAssetWriterInput* videoWriterInput = [[AVAssetWriterInput  
  17.                                              assetWriterInputWithMediaType:AVMediaTypeVideo  
  18.                                              outputSettings:videoSettings] retain];  
  19.       
  20.       
  21.     AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor  
  22.                                                      assetWriterInputPixelBufferAdaptorWithAssetWriterInput:videoWriterInput  
  23.                                                      sourcePixelBufferAttributes:nil];  
  24.     NSParameterAssert(videoWriterInput);  
  25.     NSParameterAssert([videoWriter canAddInput:videoWriterInput]);  
  26.     [videoWriter addInput:videoWriterInput];  
  27.       
  28.     //Start a session:  
  29.     [videoWriter startWriting];  
  30.     [videoWriter startSessionAtSourceTime:kCMTimeZero];  
  31.       
  32.     //Write some samples:  
  33.     CVPixelBufferRef buffer = NULL;  
  34.       
  35.     int frameCount = 0;  
  36.       
  37.     int imagesCount = [imagesArray count];      
  38.     float averageTime = duration/imagesCount;  
  39.     int averageFrame = (int)(averageTime * fps);  
  40.       
  41.     for(UIImage * img in imagesArray)  
  42.     {  
  43.         buffer = [self pixelBufferFromCGImage:[img CGImage] andSize:size];  
  44.           
  45.         BOOL append_ok = NO;  
  46.         int j = 0;  
  47.         while (!append_ok && j < 30)   
  48.         {  
  49.             if (adaptor.assetWriterInput.readyForMoreMediaData)   
  50.             {  
  51.                 printf("appending %d attemp %d\n", frameCount, j);  
  52.                   
  53.                 CMTime frameTime = CMTimeMake(frameCount,(int32_t) fps);  
  54.                 float frameSeconds = CMTimeGetSeconds(frameTime);  
  55.                 NSLog(@"frameCount:%d,kRecordingFPS:%d,frameSeconds:%f",frameCount,fps,frameSeconds);  
  56.                 append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];  
  57.                   
  58.                 if(buffer)  
  59.                     [NSThread sleepForTimeInterval:0.05];  
  60.             }   
  61.             else   
  62.             {  
  63.                 printf("adaptor not ready %d, %d\n", frameCount, j);  
  64.                 [NSThread sleepForTimeInterval:0.1];  
  65.             }  
  66.             j++;  
  67.         }  
  68.         if (!append_ok) {  
  69.             printf("error appending image %d times %d\n", frameCount, j);  
  70.         }  
  71.           
  72.         frameCount = frameCount + averageFrame;  
  73.     }  
  74.       
  75.     //Finish the session:  
  76.     [videoWriterInput markAsFinished];  
  77.     [videoWriter finishWriting];  
  78.     NSLog(@"finishWriting");  
  79. }  
0 0