使用AVPlayer播放m3u8视频时,实现视频截图

本文探讨了针对M3U8格式视频文件截图时遇到的问题,并提供了适用于MP4和MOV格式的有效方法。同时,介绍了一种利用AVPlayerItemVideoOutput实现M3U8格式视频截图的技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近需要一个对m3u8文件的截图,但是搜索一段时间后发现方法都类似,但都不成功。经过测试该方法:普通的mp4和mov格式视频可以通过下面的方法获取截图。但是m3u8文件则不行,总提示:

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}
下面的代码是网络上流传的方法,仅对pm4,mov格式生效。对m3u8的ts文件格式不生效。

// 根据视频的路径获取单帧图片
- (UIImage *)getThumbImage:(NSURL *)url {
    //测试视频地址
    NSURL *soundFileURL = nil;
    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"jiangwan" ofType:@"mp4"];
    if (soundFilePath.length) {
        soundFileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
    }
    
    //截第一帧
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:soundFileURL options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 1);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    return thumb;
}

无意中,网上查到资料:可以使用AVPlayerItemVideoOutput的[AVPlayerItemVideoOutput copyPixelBufferForItemTime:itemTimeForDisplay:]来实现,方法如下:

{
/*设置playitem 之后*/
        [self replaceCurrentItemWithPlayerItem:item];
        self.snapshotOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:NULL];
        [item removeOutput:self.snapshotOutput];
        [item addOutput:self.snapshotOutput];
}
/*
 * 截取当前时间点,视频截图
 */
- (UIImage *)snapshotImage {
    CMTime time = [self.snapshotOutput itemTimeForHostTime:CACurrentMediaTime()];
    if ([self.snapshotOutput hasNewPixelBufferForItemTime:time]) {        
        CVPixelBufferRef lastSnapshotPixelBuffer = [self.snapshotOutput copyPixelBufferForItemTime:time itemTimeForDisplay:NULL];
        CIImage *ciImage = [CIImage imageWithCVPixelBuffer:lastSnapshotPixelBuffer];
        CIContext *context = [CIContext contextWithOptions:NULL];
        CGRect rect = CGRectMake(0,
                                 0,
                                 CVPixelBufferGetWidth(lastSnapshotPixelBuffer),
                                 CVPixelBufferGetHeight(lastSnapshotPixelBuffer));
        CGImageRef cgImage = [context createCGImage:ciImage fromRect:rect];
        return [UIImage imageWithCGImage:cgImage];
    }
    return NULL;
}

参考文章:

http://darktechlabs.com/2016/07/15/iOS-小坑记录:如何给-AVPlayer-截图/
Real-time Video Processing Using AVPlayerItemVideoOutput

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值