我正在尝试从一组图像创建视频。
我创建了一个视频,但演示时间有问题,即CMTime
。
我正在使用以下代码创建视频
int frameCount = 1;
// Adding images here to buffer
for( int i = 0; i<[ fileArray count]; i++ )
{
// Create Pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *imageTag = [fileArray objectAtIndex:i];
// Create file path
NSString *imgPath = [folderPath stringByAppendingPathComponent:imageTag];
// Get image
UIImage *img = [self getImageFromPath:imgPath];
buffer = [self pixelBufferFromCGImage:[img CGImage] size:size];
BOOL append_ok = NO;
int j = 0;
// Try 5 times if append failes
while ( !append_ok && j < 30 )
{
if (adaptor.assetWriterInput.readyForMoreMediaData)
{
printf("appending %d attemp %d\n", frameCount, j);
NSTimeInterval duration = 7.0;
if ( [mArrAudioFileNames objectAtIndex:i] != [NSNull null] )
{
// Get Audio file
NSString *docsDir = [[self dataFolderPathForAudio]
stringByAppendingPathComponent:
[mArrAudioFileNames objectAtIndex:i]];
NSURL *soundFileURL = [NSURL fileURLWithPath:docsDir];
// Create AudioPlayer
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:soundFileURL
error:&error];
// Get Audio duration
duration = [audioPlayer duration];
[audioPlayer release];
}
CMTime frameTime = CMTimeMake(frameCount,(int32_t)1);
append_ok = [adaptor appendPixelBuffer:buffer
withPresentationTime:frameTime];
[NSThread sleepForTimeInterval:0.05];
}
else
{
printf("adaptor not ready %d, %d\n", frameCount, j);
[NSThread sleepForTimeInterval:0.1];
}
j++;
}
if (!append_ok) {
printf("error appending image %d times %d\n", frameCount, j);
isError = YES;
}
frameCount++;
CVBufferRelease(buffer);
// drain the pool
[pool drain];
}
[videoWriterInput markAsFinished];
[videoWriter finishWriting];
[videoWriterInput release];
[videoWriter release];
如果数组中有 7 个图像,这将创建一个 7 秒的视频。这意味着每张图片播放 1 秒。
我的问题是:如何制作具有相同图像集的视频(假设 7 个),其中每个图像在视频中的显示时间不同?
就像第一张图片从总视频时长播放 9 秒一样,然后第二张图片播放 5 秒,第三张图片播放 20 秒。
提前致谢。