在Swift 4.2中,我试图从视频中抓取截图
func thumbnailImageFor(fileUrl:URL) -> UIImage? {
let asset = AVAsset(url: fileUrl)
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let time = CMTimeMakeWithSeconds(1.0, preferredTimescale: 600)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
print(error)
return nil
}
}但获得错误:
错误Domain=AVFoundationErrorDomain代码=-11800“操作无法完成”UserInfo={NSLocalizedFailureReason=An未知错误发生(-12792),NSLocalizedDescription=The操作无法完成,NSUnderlyingError=0x600000f46580 {Error Domain=NSOSStatusErrorDomain代码=-12792 "(null)"}}
我是不是遗漏了什么?
发布于 2018-10-02 15:23:54
我认为这可能与电影的文件类型有关,因为这样做是可行的:
import UIKit
import AVFoundation
func thumbnailImageFor(fileUrl:URL) -> UIImage? {
let video = AVURLAsset(url: fileUrl, options: [:])
let assetImgGenerate = AVAssetImageGenerator(asset: video)
assetImgGenerate.appliesPreferredTrackTransform = true
let videoDuration:CMTime = video.duration
let durationInSeconds:Float64 = CMTimeGetSeconds(videoDuration)
let numerator = Int64(1)
let denominator = videoDuration.timescale
let time = CMTimeMake(value: numerator, timescale: denominator)
do {
let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: img)
return thumbnail
} catch {
print(error)
return nil
}
}
let url: URL = URL(string: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")!
let image: UIImage? = thumbnailImageFor(fileUrl: url)
print("Image: \(image)")https://stackoverflow.com/questions/52611157
复制相似问题