2

For the app I'm working on, I need to provide a functionality that lets users apply filters to their videos( not real time, applying filters on a saved video and filePath is provided).

addFilterToVideo is called when the user taps on a filter, a video composition is passed as a parameter to the initPlayer function and if the "none" video filter is tapped nil is passed

Whenever a new filter is tapped on I just change the video composition of the playerItem and the file is loaded only the first time

 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()
    }
  }

So the issue is that the video is getting rotated like this: check the screen recording here but when I tried using a sample video which I added to the project it was working fine check the screen recording here

so I checked the preferredTransform by importing them as AVAssets

For the video file recorded by the device:

(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

For the video that I added to the project

(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

so the issue is with the video at the filePath, it has a preferredTransform that should rotate the video 90 degrees and even the height and width are off. I'm not sure how to go about doing that. I tried applying CGAffineTransform to the playerLayer but It didn't work It did rotate the video but the aspect ratio is still off and what does it mean when an asset has a preferred Transform that is not identity?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anirudh Bandi
  • 1,171
  • 10
  • 20

0 Answers0