Search code examples
objective-cmacosavfoundationnsimageavkit

How to render a video file of fixed length from a still image (png)


I am trying to create a video of a specified length from a still image (png). My code is based on the answers given in How do I export UIImage as a movie

My code creates a video of the correct length, but the image does appear until the last frame. I want the immage to be shown for the full duration of the video. (These videos will be place-holders in a gapless timeline.)

My Code Is:

- (NSURL*)exportImage:(NSImage*)image asMovieLength:(double)seconds {

    NSURL *outFile;
    AppDelegate *theApp;
    NSError *error=nil;
    AVAssetWriter *videoWriter;
    NSDictionary *videoSettings;
    AVAssetWriterInput* writerInput;
    CVPixelBufferRef buffer = NULL;
    AVAssetWriterInputPixelBufferAdaptor *adaptor;

    theApp = (AppDelegate*)[[NSApplication sharedApplication] delegate];

    outFile = [theApp getFilenameForFillerFile];

    videoWriter = [[AVAssetWriter alloc] initWithURL:outFile fileType:AVFileTypeMPEG4 error:&error];
    NSParameterAssert(videoWriter);

    videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:AVVideoCodecH264, AVVideoCodecKey,
                     [NSNumber numberWithInt:880], AVVideoWidthKey,
                     [NSNumber numberWithInt:880], AVVideoHeightKey,
                     nil];

    writerInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];

    adaptor = [AVAssetWriterInputPixelBufferAdaptor assetWriterInputPixelBufferAdaptorWithAssetWriterInput:writerInput
                                                                               sourcePixelBufferAttributes:nil];

    NSParameterAssert(writerInput);
    NSParameterAssert([videoWriter canAddInput:writerInput]);
    [videoWriter addInput:writerInput];

    // Start the writing session
    [videoWriter startWriting];
    [videoWriter startSessionAtSourceTime:kCMTimeZero];


    if(writerInput.readyForMoreMediaData)
    {
        CMTime zeroTime = CMTimeMake(0, 600);
        CMTime frameTime = CMTimeMake(seconds*600, 600);
        buffer = [self pixelBufferFromImage:image];

        [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];

        [writerInput markAsFinished];

        [videoWriter finishWritingWithCompletionHandler:^{
            NSLog(@"Finished writing...checking completion status...");
            if (videoWriter.status != AVAssetWriterStatusFailed && videoWriter.status == AVAssetWriterStatusCompleted)
            {
                NSLog(@"Video writing succeeded.");

            } else
            {
                NSLog(@"Video writing failed: %@", videoWriter.error);
            }

        }];

        CVPixelBufferPoolRelease(adaptor.pixelBufferPool);

        NSLog (@"Done");
    }

    return outFile;
}

How do I get the image to show from the start, and last for the specified time?


Solution

  • Following changes in your code did the trick for me.

    CMTime zeroTime = CMTimeMake(0, 600);
    CMTime frameTime = CMTimeMake((seconds * 0.5) * 600, 600);
    
    buffer = [self pixelBufferFromImage:image];
    
    [adaptor appendPixelBuffer:buffer withPresentationTime:zeroTime];
    [adaptor appendPixelBuffer:buffer withPresentationTime:frameTime];