1
votes

I'm trying to generate a UIImage from a video frame captured by GPUImage. I've done a lot of AVFoundation video work, but i'm new to using GPUImage. I've subclassed GPUImageVideoCamera and added this method, but the UIImage is always nil. If anyone can tell me where i've gone so horribly wrong, i'd be very appreciative!

- (void)processVideoSampleBuffer:( CMSampleBufferRef )sampleBuffer

{ [super processVideoSampleBuffer:sampleBuffer]; // to let GPUImage do it's processing first

if (!self.thumbnailGenerated)
{
    CMTime timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
    NSLog(@"%f", (float)timestamp.value / timestamp.timescale);

    self.thumbnailGenerated = YES;

    dispatch_sync(dispatch_get_main_queue(), ^
                  {
                      // generate a preview frame from the last filter in the camera filter chain

                      UIImage *thumbnailImage = [UIImage imageWithCGImage:[[self.targets lastObject] newCGImageFromCurrentlyProcessedOutput]];
                      NSString *pathToMovie = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Thumbnail.png"];
                      [UIImagePNGRepresentation(thumbnailImage) writeToFile:pathToMovie atomically:YES];
                  });
}
}
2

2 Answers

0
votes

I've used this code to generate a CGImageRef of the first frame, used for thumbnail

AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
[imageGenerator setAppliesPreferredTrackTransform:YES];
NSData *videoData = [NSData dataWithContentsOfURL:asset.URL];
CGImageRef  image = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:nil error:&error];

You can replace kCMTimeZero with some actual value to get the frame you'd like.

After that, you'll have to convert your CGImageRef to a UIImage.

0
votes

I'm not sure if that is of any help, but I'm getting thumbnail while processing video. For this purpose I'm using

 videoInput --> someMyOperations --> fileOutput
 someMyOperations --> imageOutput //imageOutput is PictureOutput()
 videoInput.start() //that needs to be called!
 imageOutput.saveNextFrameToUrl(coverUrl, format: .jpg) { file in
     //here goes the code what to do with thumbnail
     videoInput.cancel() //quite probably here you want this
 }

That's guessing - I don't see your code, but for me this works.