从 AVPlayer 中提取位图非常不确定
Posted
技术标签:
【中文标题】从 AVPlayer 中提取位图非常不确定【英文标题】:Extracting bitmap from AVPlayer is very uncertain 【发布时间】:2021-12-13 07:52:37 【问题描述】:我想在某个时间点从视频播放器中提取UIIMage
,并对该图像进行一些处理。
我有这个代码:
pausePlayer()
let time = player!.currentTime()
imageFromVideo(url: fileURL(for: movieName!), at: time.seconds) image in
let result = self.detectLines(image: image!
if let result = result
// Display results by handing off to the InferenceViewController.
DispatchQueue.main.async
self.drawNewResults(result: result)
和
public func imageFromVideo(url: URL, at time: TimeInterval, completion: @escaping (UIImage?) -> Void)
DispatchQueue.global(qos: .background).async
let asset = AVURLAsset(url: url)
let assetIG = AVAssetImageGenerator(asset: asset)
assetIG.appliesPreferredTrackTransform = true
assetIG.apertureMode = AVAssetImageGenerator.ApertureMode.encodedPixels
let cmTime = CMTime(seconds: time, preferredTimescale: CMTimeScale(30.0))
var newTime = CMTime(seconds: time, preferredTimescale: CMTimeScale(30.0))
let thumbnailImageRef: CGImage
do
thumbnailImageRef = try assetIG.copyCGImage(at: cmTime, actualTime: &newTime)
catch let error
print("Error: \(error)")
return completion(nil)
print("Time on click: %f", cmTime.seconds)
print("Actual time: %f", newTime.seconds)
DispatchQueue.main.async
completion(UIImage(cgImage: thumbnailImageRef))
问题是我想要图像的时间和我得到的图像之间存在巨大差距。通常它类似于 0.2s 和 0.4s,大约是 7 14 帧之间的偏移量。所以我做和展示的处理是错误的,因为我没有处理观察到的图像。
例子:
Time on click: %f 0.8333333333333334
Actual time: %f 1.0666666666666667
Time on click: %f 1.6333333333333333
Actual time: %f 2.1666666666666665
【问题讨论】:
【参考方案1】:这里建议:swift: How to take screenshot of AVPlayerLayer()
assetIG.requestedTimeToleranceAfter = CMTime.zero
assetIG.requestedTimeToleranceBefore = CMTime.zero
成功了。
【讨论】:
以上是关于从 AVPlayer 中提取位图非常不确定的主要内容,如果未能解决你的问题,请参考以下文章