Video rotated after applying AVVideoComposition

后端 未结 5 1296
野性不改
野性不改 2021-02-09 07:00

After applying an AVVideoComposition to my AVPlayerItem, the filter I apply does work, but the video gets rotated in the AVPlayerLayer.

5条回答
  •  南旧
    南旧 (楼主)
    2021-02-09 07:22

    If You are trying to play AVMutableCompostion You should set AVAssetTrack's preferredTransform to AVMutableCompositionTrack's preferredTransform.

    let asset = AVAsset(url: url!)
    
    let composition = AVMutableComposition()
    let compositionTrack = composition.addMutableTrack(withMediaType: AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
    
    let videoTrack = asset.tracks(withMediaType: AVMediaTypeVideo).first
    
    try? compositionTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, asset.duration), of: videoTrack!, at: kCMTimeZero)
    
    compositionTrack.preferredTransform = (videoTrack?.preferredTransform)!
    
    let playerItem = AVPlayerItem(asset: composition)
    let filter = CIFilter(name: "CIColorInvert")
    playerItem.videoComposition = AVVideoComposition(asset: composition, applyingCIFiltersWithHandler: { (request: AVAsynchronousCIImageFilteringRequest) in
                filter?.setValue(request.sourceImage, forKey: kCIInputImageKey)
                request.finish(with: (filter?.outputImage)!, context: nil)
            })
     .... the rest of code
    

提交回复
热议问题