我花了一整天时间,经历了很多SO答案,Apple引用,文档等,但都没有成功。
我想要一个简单的事情:我正在使用AVPlayer播放视频,我想暂停它并将当前帧设为UIImage
。那就是它。
我的视频是位于互联网上的m3u8文件,它在AVPlayerLayer
中正常播放,没有任何问题。
我尝试了什么:
AVAssetImageGenerator
。它不起作用,方法copyCGImageAtTime:actualTime: error:
返回null image ref。根据答案here AVAssetImageGenerator
不适用于流媒体视频。renderInContext:
上首先尝试了AVPlayerLayer
,但后来我意识到它并没有渲染出这种"特殊&# 34;层。然后我发现在iOS 7中引入了一种新方法 - drawViewHierarchyInRect:afterScreenUpdates:
,它应该能够渲染特殊图层,但是没有运气,仍然会在显示视频的情况下获得带有空白黑色区域的UI快照。AVPlayerItemVideoOutput
。我为AVPlayerItem
添加了视频输出,但每当我致电hasNewPixelBufferForItemTime:
时,都会返回NO
。我想问题是再次流视频和I am not alone这个问题。AVAssetReader
。我在考虑尝试,但决定在找到相关问题here后不要浪费时间。所以,有没有什么方法可以获得我现在在屏幕上看到的某些内容的快照?我无法相信这一点。
答案 0 :(得分:5)
AVAssetImageGenerator
是对视频进行快照的最佳方式,此方法异步返回UIImage
:
import AVFoundation
// ...
var player:AVPlayer? = // ...
func screenshot(handler:@escaping ((UIImage)->Void)) {
guard let player = player ,
let asset = player.currentItem?.asset else {
return
}
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
let times = [NSValue(time:player.currentTime())]
imageGenerator.generateCGImagesAsynchronously(forTimes: times) { _, image, _, _, _ in
if image != nil {
handler(UIImage(cgImage: image!))
}
}
}
(它的Swift 4.2)
答案 1 :(得分:4)
AVPlayerItemVideoOutput
对我来说很好。也许是因为我没有咨询hasNewPixelBufferForItemTime
而只是致电copyPixelBufferForItemTime
?此代码生成CVPixelBuffer
而不是UIImage
,但有describe how to do that的答案。
这个答案主要来自here
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic) AVPlayer *player;
@property (nonatomic) AVPlayerItem *playerItem;
@property (nonatomic) AVPlayerItemVideoOutput *playerOutput;
@end
@implementation ViewController
- (void)setupPlayerWithLoadedAsset:(AVAsset *)asset {
NSDictionary* settings = @{ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA) };
self.playerOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:settings];
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
[self.playerItem addOutput:self.playerOutput];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.view.frame;
[self.view.layer addSublayer:playerLayer];
[self.player play];
}
- (IBAction)grabFrame {
CVPixelBufferRef buffer = [self.playerOutput copyPixelBufferForItemTime:[self.playerItem currentTime] itemTimeForDisplay:nil];
NSLog(@"The image: %@", buffer);
}
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *someUrl = [NSURL URLWithString:@"http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:someUrl options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
NSError* error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:@"tracks" error:&error];
if (status == AVKeyValueStatusLoaded)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self setupPlayerWithLoadedAsset:asset];
});
}
else
{
NSLog(@"%@ Failed to load the tracks.", self);
}
}];
}
@end