如何在使用Objective c iPhone创建的电影中添加效果(如CurlUp,CurlDown等)

时间:2012-05-11 09:41:24

标签: iphone objective-c avassetwriter avmutablecomposition

我使用AVAssetWriter创建了一部电影,AVMutableComposition创建了图像数组和音频歌曲,我将其保存到照片库...知道我想在制作电影的同时为该图像添加效果......

是否可以在制作电影时为图像添加效果(如CurlUp,CurlDown等),如果是这样,请为我提供答案。

-(void)writeImageAsMovie:(NSArray *)array toPath:(NSString*)path size:(CGSize)size duration:(int)duration 
 {
CFDataRef imgData;
CGDataProviderRef imgDataProvider;
CGImageRef image1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *savedVideoPath1 = [documentsDirectoryPath stringByAppendingPathComponent:@"videoOutput1234videoOutput1234.mp4"];

if ([[NSFileManager defaultManager] fileExistsAtPath:savedVideoPath1])
    [[NSFileManager defaultManager] removeItemAtPath:savedVideoPath1 error:nil];

NSError *error = nil;

AVAssetWriter *videoWriter = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:path] fileType:AVFileTypeMPEG4 error:&error];


NSParameterAssert(videoWriter);

NSDictionary *videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               AVVideoCodecH264, AVVideoCodecKey,
                               [NSNumber numberWithInt:size.width], AVVideoWidthKey,
                               [NSNumber numberWithInt:size.height], AVVideoHeightKey,
                               nil];
AVAssetWriterInput* writerInput = [[AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings] retain];


AVAssetWriterInputPixelBufferAdaptor *adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput sourcePixelBufferAttributes:nil];


NSParameterAssert(writerInput);
NSParameterAssert([videoWriter canAddInput:writerInput]);

writerInput.expectsMediaDataInRealTime = YES;
[videoWriter addInput:writerInput];


[videoWriter startWriting];
[videoWriter startSessionAtSourceTime:kCMTimeZero];


CVPixelBufferRef buffer = NULL;


int frameCount = 0;

for(int i = 0; i<[array count]; i++)
{
    imgData = (CFDataRef)[array objectAtIndex:i];
    imgDataProvider = CGDataProviderCreateWithCFData (imgData);
    image1 = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);

    buffer = [self pixelBufferFromCGImage:image1];

    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;


            int64_t value = i;
            int32_t preferredTimeScale = 1;
            frameTime = CMTimeMake(value, preferredTimeScale);


            append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

        } 
        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++;
    CVBufferRelease(buffer);
}

if (AVAssetWriterStatusCompleted) {
    [writerInput markAsFinished];
    [videoWriter finishWriting];
}

}

0 个答案:

没有答案