Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screenshot for AVPlayer and Video

I am trying to take a screenshot of an AVPlayer inside a bigger view. I want to build a testing framework only, so private APIs or any method is good, because the framework will not be included when releasing to the AppStore.

I have tried with using

  • UIGetScreenImage() : works well on simulator but not on device
  • snapshotviewafterscreenupdates: it shows the view but I cannot create a UIImage from that.
  • drawViewInHierarchy and renderInContext will not work with AVPlayer
  • I don't want to use AVAssetGenerator for getting image from video, it is hard to get a good coordinate or the video player as the subview of other views
like image 294
vodkhang Avatar asked Apr 25 '14 07:04

vodkhang


2 Answers

I know you don't want to use the AVAssetImageGenerator but I've also researched this extensively and I believe the only solution currently is using the AVAssetImageGenerator. It's not that difficult as you say to get the right coordinate because you should be able to get the current time of your player. In my App the following code works perfectly:

-(UIImage *)getAVPlayerScreenshot 
{
    AVURLAsset *asset = (AVURLAsset *)self.playerItem.asset;
    AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;
    imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;
    CGImageRef thumb = [imageGenerator copyCGImageAtTime:self.playerItem.currentTime
                                              actualTime:NULL
                                                   error:NULL];
    UIImage *videoImage = [UIImage imageWithCGImage:thumb];
    CGImageRelease(thumb);
    return videoImage;
}
like image 89
Bob de Graaf Avatar answered Nov 15 '22 11:11

Bob de Graaf


AVPlayer rending videos using GPU, so you cannot capture it using core graphics methods.

However that’s possible to capture images with AVAssetImageGenerator, you need specify a CMTime.


Update:

Forget to take a screenshot of the entire screen. AVPlayerItemVideoOutput is my final choice, it supports video steam.

Here is my full implementation: https://github.com/BB9z/ZFPlayer/commit/a32c7244f630e69643336b65351463e00e712c7f#diff-2d23591c151edd3536066df7c18e59deR448

like image 1
BB9z Avatar answered Nov 15 '22 10:11

BB9z