0

I am building an app in which I have to get a thumbnail from a recorded video. I have an array of alpha video frames. I have to merge each set of frames and then make a movie with these final frames. Here is the piece of code I am dealing with:

-(void)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(@"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];

}

The problem is its taking much time to deal with frames in the whole loop. Could anyone please help speed up the process. I already have tried ffmpeg, but I think the problem is in merging. If anyone has suggestions, please share them.

4

2 回答 2

0

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

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

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

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

于 2013-07-15T22:17:41.943 回答
0

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

我最近遇到了类似的问题,即在这样的循环中不断运行的一段代码中的泄漏。将局部变量更改为实例变量,然后仅重用该变量可以防止它为应用程序分配过多的内存。

于 2012-12-21T19:34:33.083 回答