我有两个视频帧数组,每个数组等于 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;
}
我的问题是合并所有帧需要很长时间。
有没有办法让任务更快?
谢谢