I have to extract a thumbnail from a video (from url) and I use this code:
NSString *stringUrl = video.stringurl;
NSURL *url = [NSURL URLWithString:stringUrl
I'm trying as well to grab a screenshot from an HLS variable bitrate stream, I.E. M3U8, and none of the methods provided here worked for me.
I did succeed at the end.
First you need to attach an AVPlayerItemVideoOutput
to your player:
self.playerAV = [AVPlayer playerWithURL:localURL];
NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] };
AVPlayerItemVideoOutput* output = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings];
[self.playerAV.currentItem addOutput:output];
Now when you wanna grab the screenshot:
CVPixelBufferRef pixelBuffer = [output copyPixelBufferForItemTime:player.currentTime itemTimeForDisplay:nil];
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];
CIContext *temporaryContext = [CIContext contextWithOptions:nil];
CGImageRef videoImage = [temporaryContext
createCGImage:ciImage
fromRect:CGRectMake(0, 0,
CVPixelBufferGetWidth(pixelBuffer),
CVPixelBufferGetHeight(pixelBuffer))];
image = [UIImage imageWithCGImage:videoImage];
image = [image cropImageToSize:maxSize withProportionDiffLargerThan:IMAGE_PROPORTION_DIFF];
if ( videoImage )
{
CGImageRelease(videoImage);
}