试图制作一个15秒的视频CMTimeMake

时间:2014-06-18 05:52:31

标签: ios objective-c cmtime

我试图用UIImage的数组做一个15秒的视频,数组内容的数字会改变。

NSUInteger fps = 30;
int frameCount = 0;
float numberOfSecondsPerFrame = 15/[imageArray count];
float frameDuration = fps * numberOfSecondsPerFrame;

 NSLog(@"**************************************************");
 for(UIImage * img in imageArray)
    {
    buffer = [self pixelBufferFromCGImage:[img CGImage]];

    BOOL append_ok = NO;
    int j = 0;
    while (!append_ok && j < 30) {
        if (adaptor.assetWriterInput.readyForMoreMediaData)  {
            //print out status:
            NSLog(@"Processing video frame (%d,%d)",frameCount,[imageArray count]);


            CMTime frameTime = CMTimeMake(frameCount*frameDuration,(int32_t) fps);

            NSLog(@"seconds = %f, %u, %d", CMTimeGetSeconds(frameTime),fps,j);
            append_ok = [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];
            if(!append_ok){
                NSError *error = videoWriter.error;
                if(error!=nil) {
                    NSLog(@"Unresolved error %@,%@.", error, [error userInfo]);
                }
            }
        }
        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, with error.", frameCount, j);
    }
    frameCount++;
}

我需要对每个图片持续时间显示时间= 15 / [imageArray count],但我无法弄清楚这个CMTimeMake如何工作

我知道解决我的问题很简单,但我无法弄清楚

任何提示或帮助都将非常感激

1 个答案:

答案 0 :(得分:2)

最后我自己想出来了

这是15秒内完成视频的更正版本:D

CMTime frameTime = CMTimeMake(frameCount*15, [imageArray count]);