对于我正在使用的应用程序,我需要提供一个功能,允许用户将过滤器应用于其视频(不是实时的,而是将过滤器应用于保存的视频和filePath)。
addFilterToVideo
在用户点击过滤器时被调用,视频合成作为参数传递给initPlayer
函数,如果点击了“ none”视频过滤器,则零通过。
每当使用新的过滤器时,我只需更改playerItem的视频组成,并且仅在第一次加载文件
func addFilterToVideo(filterName: String) {
if filterName != "" {
let filter = CIFilter(name: filterName)
if #available(iOS 9.0, *) {
let composition = AVVideoComposition(asset: (self.playerItem?.asset)!) { (request) in
let source = request.sourceImage.clampingToExtent()
filter?.setValue(source, forKey: kCIInputImageKey)
let output = filter?.outputImage!.cropping(to: request.sourceImage.extent)
request.finish(with: output!, context: FilterView.context)
}
self.selectedComposition = composition
self.initPlayer(composition: composition)
} else {
// Fallback on earlier versions
}} else {
self.selectedComposition = nil
self.initPlayer(composition: nil)
}
}
func playerSetup(){
self.playerItem = AVPlayerItem(url: URL(fileURLWithPath: self.filePath!))
self.player = AVPlayer(playerItem: playerItem)
self.playerLayer.player = self.player
self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.playerLayer.contentsGravity = kCAGravityResizeAspectFill
self.layer.addSublayer(self.playerLayer)
self.player?.play()
flag = true
}
func initPlayer(composition: AVVideoComposition?){
if composition != nil {
if !flag {
self.playerSetup()
}
playerItem?.videoComposition = composition
} else {
self.playerSetup()
}
}
所以问题在于视频正在像这样旋转: check the screen recording here 但是当我尝试使用添加到项目中的示例视频时,效果很好 check the screen recording here
所以我通过将它们导入为AVAsets来检查了preferredTransform
对于设备录制的视频文件:
(lldb)po videoTrack?.preferredTransform
- a : 0.0
- b : 1.0
- c : -1.0
- d : 0.0
- tx : 1080.0
- ty : 0.0
(lldb)po videoTrack?.naturalSize
- width : 1920.0
- height : 1080.0
对于我添加到项目中的视频
(lldb)po videoTrack?.preferredTransform
- a : 1.0
- b : 0.0
- c : 0.0
- d : 1.0
- tx : 0.0
- ty : 0.0
(lldb)po videoTrack?.naturalSize
- width : 1080.0
- height : 1920.0
所以问题出在filePath上的视频,它具有preferredTransform,应该将视频旋转90度,甚至高度和宽度都关闭。我不确定该怎么做。我曾尝试将CGAffineTransform应用于playerLayer,但没有成功,它确实旋转了视频,但长宽比仍然关闭,并且当资产具有首选的非身份转换时,这意味着什么?