Iphone 获取和合并视频帧以及编写电影的时间太多

Iphone 获取和合并视频帧以及编写电影的时间太多,iphone,objective-c,video,ios6,Iphone,Objective C,Video,Ios6,我正在构建一个应用程序,其中我必须从录制的视频中获取缩略图。我有一组alpha视频帧。我必须合并每一组帧,然后用这些最后的帧制作一部电影。下面是我正在处理的一段代码: -(无效)createFrameForVideo { } 问题是在整个循环中处理帧需要花费很多时间。谁能帮我加快这个过程。我已经尝试过ffmpeg,但我认为问题在于合并。如果有人有建议,请分享。尝试使用仪器查看内存被占用的位置以及是否有任何泄漏 最近,我遇到了一个类似的问题,一段代码经常在这样的循环中运行,其中存在漏洞。将局部变量

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

-(无效)createFrameForVideo {

}


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

尝试使用仪器查看内存被占用的位置以及是否有任何泄漏


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

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

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

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


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

谢谢Steven。我想你没有明白我的意思,我说的是时间,不是记忆。整个过程需要很多时间,有什么方法可以让它快速发生吗?
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];
}
success = [fileManager fileExistsAtPath:docPath];
if (success) {
    [fileManager removeItemAtPath:docPath error:nil];
}

//[self performSelectorOnMainThread:@selector(CompileFilesToMakeMovieWithAudio) withObject:Nil waitUntilDone:YES];

[self CompileFilesToMakeMovieWithAudio];
//  [self compileFinalOutputMovie];