2

I wanted to create a video from a set of images where i have taken a majority of my code with reference to:

How do I export UIImage array as a movie?

The code that i have written is :

NSError *error = nil;

NSLog(@"Gonna start writing");

writer = [[AVAssetWriter alloc] initWithURL:[NSURL fileURLWithPath:@"ravi4"] fileType:AVFileTypeQuickTimeMovie error:&error];
NSParameterAssert(writer);

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

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

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

NSParameterAssert(writerInput);
NSParameterAssert([writer canAddInput:writerInput]);
[writer addInput:writerInput];
[writer startWriting];
[writer startSessionAtSourceTime:kCMTimeZero];

UIImage *image = [UIImage imageNamed:@"back.jpg"];
UIImage *image2 = [UIImage imageNamed:@"arthas.jpg"];

UIImage *resizedImage = [ImageToVideoViewController imageWithImage:image scaledToSize:CGSizeMake(640, 480)];
UIImage *resizedImage2 = [ImageToVideoViewController imageWithImage:image2 scaledToSize:CGSizeMake(640, 480)];

CGImageRef img = [resizedImage CGImage];
CGImageRef img2 = [resizedImage2 CGImage];  

CVPixelBufferRef buffer = [ImageToVideoViewController pixelBufferFromCGImage:img size:CGSizeMake(640, 480)];
CVPixelBufferRef buffer2 = [ImageToVideoViewController pixelBufferFromCGImage:img2 size:CGSizeMake(640, 480)];

[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(0, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) {

}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(3,1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) {

}
[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(6, 1)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) 
{

}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(9, 1)];

while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) 
{

}
[adaptor appendPixelBuffer:buffer2 withPresentationTime:CMTimeMake(10, 1)];
//[adaptor appendPixelBuffer:buffer withPresentationTime:CMTimeMake(10, 2)];
while ((adaptor.assetWriterInput.readyForMoreMediaData)==NO ) 
{

}
CVBufferRelease(buffer);
CVBufferRelease(buffer2);

[writerInput markAsFinished];
//[writerInput finishWriting];
[writerInput release];

[writer endSessionAtSourceTime:CMTimeMake(10, 1)];
[writer finishWriting];
[writer release];

The code is messy as im just experimenting as to how to go about this problem

The video is coming fine except for the last image that i am attempting to display the video freezes and stops as soon as it is required to display that image but will work fine until it reaches that point

Thanks!

Community
  • 1
  • 1
Aatish Molasi
  • 2,138
  • 3
  • 20
  • 43

1 Answers1

0

The actual solution is that you need to add an extra frame on the end for the duration you want the final image on screen.

You effectively add the image twice, first at the start time offset for when you wish it to appear AND then again at the time offset you wish the movie to finish at.

Otherwise you are ending the movie at the same time as showing the final image.

Simon Lee
  • 22,304
  • 4
  • 41
  • 45