Search code examples
iosimagescreenshotavplayerm3u8

How can I capture an image when AVPlayer playing m3u8 stream?


I use AVPlayer to play a m3u8 file, and I want to capture an image in these code:

AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:self.player.currentItem.asset];
gen.appliesPreferredTrackTransform = YES;
NSError *error = nil;
CMTime actualTime;
CMTime now = self.player.currentTime;
[gen setRequestedTimeToleranceAfter:kCMTimeZero];
[gen setRequestedTimeToleranceBefore:kCMTimeZero];
CGImageRef image = [gen copyCGImageAtTime:now actualTime:&actualTime error:&error];
UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
NSLog(@"%f , %f",CMTimeGetSeconds(now),CMTimeGetSeconds(actualTime));

NSLog(@"%@",error);
if (image) {
    CFRelease(image);
}

but it does not work. And the error is:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x7fadf25f59f0 {NSUnderlyingError=0x7fadf25f1670 "The operation couldn’t be completed. (OSStatus error -12782.)", NSLocalizedFailureReason=An unknown error occurred (-12782), NSLocalizedDescription=The operation could not be completed}

How can I solve it?
Thanks a lot.


Solution

  • AVAssetImageGenerator may require local assets. Maybe you'd have more luck adding an AVPlayerItemVideoOutput to your AVPlayer, seeking to the desired spot and calling copyPixelBufferForItemTime:itemTimeForDisplay: on the videoOutput.