4

Ok so I am in a bind and I have been staring at this for too long to think clearly.

Basically, all I need is a method that takes in an NSArray of images and writes an mp4 or other video format to disk.

I am not worried about monitoring the user's gesture or anything like that. The images already exist on disk -- I just need to do the conversion.

I understand that I need an AVWriter and something like:

- (CVPixelBufferRef) pixelBufferFromCGImage: (CGImageRef) image
{
  NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                         [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                         nil];
  CVPixelBufferRef pxbuffer = NULL;

  CVPixelBufferCreate(kCFAllocatorDefault, CGImageGetWidth(image),
                    CGImageGetHeight(image), kCVPixelFormatType_32ARGB, (CFDictionaryRef) options,
                    &pxbuffer);

  CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);

  CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef context = CGBitmapContextCreate(pxdata, CGImageGetWidth(image),
                                             CGImageGetHeight(image), 8, 4*CGImageGetWidth(image), rgbColorSpace,
                                             kCGImageAlphaNoneSkipFirst);

  CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));

//    CGAffineTransform flipVertical = CGAffineTransformMake(
//                                                           1, 0, 0, -1, 0, CGImageGetHeight(image)
//                                                           );
//    CGContextConcatCTM(context, flipVertical);



//    CGAffineTransform flipHorizontal = CGAffineTransformMake(
//                                                             -1.0, 0.0, 0.0, 1.0, CGImageGetWidth(image), 0.0
//                                                             );
//
//    CGContextConcatCTM(context, flipHorizontal);


  CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),
                                       CGImageGetHeight(image)), image);
  CGColorSpaceRelease(rgbColorSpace);
  CGContextRelease(context);

  CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

  return pxbuffer;
}

I just can't see how to put it all into one clean helper. And before you ask I have seen: How to display video from images and the like, but just running the images in a UIImageView is not what I am looking for here. This needs to be App Store legal, so FFMPeg is not an option.

Any thoughts / guidance?

Community
  • 1
  • 1
mdominick
  • 1,319
  • 1
  • 10
  • 21
  • How about [How do I export UIImage array as a movie?](http://stackoverflow.com/questions/3741323/how-do-i-export-uiimage-array-as-a-movie?rq=1) – Kurt Revis Jan 03 '13 at 08:25
  • @KurtRevis I took a look at that and tried to work off of it but didn't get anywhere. The thing is I really just need a small helper that I can send an Array to and have it string together the video. It seems like it should be pretty simple, but evidently not... – mdominick Jan 03 '13 at 08:36
  • Ok so I found this: http://stackoverflow.com/questions/6061092/make-movie-file-with-picture-array-and-song-file-using-avasset not sure that it will work right and I only care about video but looks more like something I'd go for... – mdominick Jan 03 '13 at 08:47
  • Ok I have found this open-source project on GH that I am having some success modifying. The performance leaves something to be desired, but it seems to do the job -- I am ripping out all of the audio stuff. https://github.com/caferrara/img-to-video – mdominick Jan 03 '13 at 10:06

1 Answers1

1

https://github.com/meslater/MSImageMovieEncoder encodes images to H.264. You have to provide images as context refs or CVPixelBufferRefs (see sample code here) implementing

-(BOOL)nextFrameInCVPixelBuffer:(CVPixelBufferRef*)pixelBuf;

Each call to this method should return a buffer and YES. If there are no more images return NO, and you'll find the movie on disk.

For more details, read How do I export UIImage array as a movie? (it's basically the same code) or read the github files.

Community
  • 1
  • 1
Jano
  • 62,815
  • 21
  • 164
  • 192