Generating UIImage from GPUImage video frame

南楼画角 提交于 2019-12-11 05:41:21

问题


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];
                  });
}
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/24222476/generating-uiimage-from-gpuimage-video-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!