3

AVAssetWriter、AVMutableComposition を使用して画像配列とオーディオ ソングを使用して 1 つのムービーを作成し、それをフォト ライブラリに保存しています...ムービーの作成中にその画像にエフェクトを追加したいことを知っています...

ムービーの作成中に画像に効果 (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];
}

}
4

0 に答える 0