太多时间来获取和合并视频帧和写电影

时间:2012-12-20 13:55:28

标签: iphone objective-c video ios6

我正在构建一个应用程序,我必须从录制的视频中获取缩略图。我有一组alpha视频帧。我必须合并每组帧,然后用这些最终帧制作一部电影。以下是我正在处理的代码:

- (无效)createFrameForVideo {

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 = 14;

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];
    NSString * file = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
UIImage *overlayImage = [UIImage imageWithData:[NSData dataWithContentsOfFile:file]];

count = count + 1;
NSString *imagePath = [NSString stringWithFormat:@"%@/%@", docPath, imageName];
if (overlayImage)
    {
        UIImage *  outImage = [self mergeImage:singleFrameImage withImage:overlayImage];
        NSData *imgData = [[NSData alloc] initWithData:UIImagePNGRepresentation(outImage)];
        [fileManager createFileAtPath:imagePath contents:imgData attributes:nil];
        [imgData release];
    }
    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(@“你好你的分层完成”);     [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];

}

问题是它需要花费很多时间来处理整个循环中的帧。有谁可以帮助加快这个过程。我已经尝试了ffmpeg,但我认为问题在于合并。如果有人有任何建议,请分享。

2 个答案:

答案 0 :(得分:0)

尝试使用乐器来查看你的记忆被束缚的地方以及是否有任何泄漏。

最近我遇到了一个类似的问题,一段代码中的漏洞一直在这样的循环中运行。将局部变量更改为实例变量,然后重新使用该变量,使其不会为应用程序分配太多内存。

答案 1 :(得分:0)

您的问题是基本方法之一。如果您想获得更好的执行时间,那么您需要更改应用程序的基本工作方式。没有特定的“更改此代码”步骤可以使您现有的代码执行得更快。

你应该尝试对视频帧进行编码,然后直接写入已编译的h.264,而不是现在要创建的每个帧,然后在另一个循环中将它们组合在一起。像这样:

1:读取输入PNG 2:读取视频帧 3:组合视频帧和输入PNG 4:通过AVAsset apis将组合帧写入电影。

这样可以避免每次读取2次帧,并避免再次将所有PNG图像写入磁盘。 IO需要花费大量时间并将图像压缩到PNG需要花费大量时间。建议的方法会快得多,因为它可以避免这些不必要的步骤。