它花了很多时间来合并视频帧

时间:2012-12-21 13:29:33

标签: objective-c video ios6 ios5

我有两个视频帧阵列,每个阵列等于150-200帧。我正在合并它们以获得一组合并的帧。我使用以下代码。

NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/movie.mp4"]];
NSURL *outputURL = [NSURL fileURLWithPath:filePath];
player = [[MPMoviePlayerController alloc]initWithContentURL:outputURL];

float frame = 0.00;
int count = 1;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
docPath = [docPath stringByAppendingPathComponent:@"OutPut"];
BOOL success = [fileManager fileExistsAtPath:docPath];
if (success) {
    [fileManager removeItemAtPath:docPath error:nil];
}
[fileManager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:nil];
for (frame = frameStartTime; frame < (frameStartTime+7); frame+=0.033)
    {
    @autoreleasepool
        {
    UIImage * singleFrameImage = [player thumbnailImageAtTime:frame timeOption:MPMovieTimeOptionExact];
    [player pause];
    NSString *imageName = [NSString stringWithFormat:@"export2%d.png",count];
    UIImage *overlayImage = [UIImage imageNamed:imageName];
    count = count + 1;
    NSString *imagePath = [NSString stringWithFormat:@"outImage%f.png",frame];
   imagePath = [NSString stringWithFormat:@"%@/%@", docPath, imageName];
    if (overlayImage)
    {
        UIImage *  outImage = [self mergeImage:singleFrameImage withImage:overlayImage];
        NSData *imgData = UIImagePNGRepresentation(outImage);
       [fileManager createFileAtPath:imagePath contents:imgData attributes:nil];

    }
    else
    {
        NSData *imgData = UIImagePNGRepresentation(singleFrameImage);
         [fileManager createFileAtPath:imagePath contents:imgData attributes:nil];


    }

    [outputFramesArray addObject:imagePath];
}

}


[player release];
if([fileManager fileExistsAtPath:filePath])
{

    [fileManager removeItemAtPath:filePath error:nil];

}


NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Documents/movie1.mp4"]];
NSLog(@"filePath %@", path);

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

[self writeImageAsMovie:outputFramesArray toPath:path size:CGSizeMake(480, 320) duration:10];
NSLog(@"hello Your layering is completed");

[outputFramesArray removeAllObjects];
[outputFramesArray release];
success = [fileManager fileExistsAtPath:docPath];

if (success) {
    [fileManager removeItemAtPath:docPath error:nil];
}

/// [self performSelectorOnMainThread:@selector(CompileFilesToMakeMovieWithAudio) withObject:nil waitUntilDone:YES];
[self CompileFilesToMakeMovieWithAudio];
  // [self compileFinalOutputMovie];
}



- (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second {
    CGSize mergedSize = CGSizeMake(480,320);   
    UIGraphicsBeginImageContext(mergedSize);
    [first drawInRect:CGRectMake(0, 0, 480, 320)];
    [second drawInRect:CGRectMake(0, 0, 480, 320)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

我的问题是它需要花费很多时间来合并所有帧。

有没有办法让任务更快?

谢谢

0 个答案:

没有答案