当AVPlayer播放m3u8流时如何捕获图像?

时间:2015-04-02 02:28:49

标签: ios image screenshot avplayer m3u8

我使用AVPlayer播放m3u8文件,我想在这些代码中捕获图片:

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

但它不起作用。错误是:

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}

我该如何解决? 非常感谢。

3 个答案:

答案 0 :(得分:3)

AVAssetImageGenerator可能需要本地资产。也许您有更多的运气为AVPlayerItemVideoOutput添加AVPlayer,寻找所需的位置并在视频输出上调用copyPixelBufferForItemTime:itemTimeForDisplay:

答案 1 :(得分:1)

我使用以下代码解决了同样的问题。

您可以使用以下代码:

属性

@property (strong, nonatomic) AVPlayer *player;
@property (strong, nonatomic) AVPlayerItem *playerItem;
@property (strong, nonatomic) AVPlayerItemVideoOutput *videoOutput;

初​​始

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:_playerItem];

获取图片

CMTime currentTime = _player.currentItem.currentTime;
CVPixelBufferRef buffer = [_videoOutput copyPixelBufferForItemTime:currentTime itemTimeForDisplay:nil];
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:buffer];
UIImage *image = [UIImage imageWithCIImage:ciImage];
//Use image^^

答案 2 :(得分:0)

要从HLS视频的avplayer捕获图像,请执行以下操作:

test~wm~helo_thisisafield_bn~thisisanotherfieldbm, H4, Hello