how to add CIFilters to a video at runtime












2















I have recorded a video using avfoundation and after clicking on the video its start to play on a new viewcontroller. The problem is, I wouldn't be able to add filter effects to a playing video. Following is the code of what i have achieved.



import UIKit
import AVFoundation

class VideoPlayback: UIViewController {

let avPlayer = AVPlayer()
var avPlayerLayer: AVPlayerLayer!

var videoURL: URL!

@IBOutlet weak var videoView: UIView!

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

avPlayerLayer = AVPlayerLayer(player: avPlayer)
avPlayerLayer.frame = view.bounds
avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
videoView.layer.insertSublayer(avPlayerLayer, at: 0)

view.layoutIfNeeded()

let playerItem = AVPlayerItem(url: videoURL as URL)
avPlayer.replaceCurrentItem(with: playerItem)

avPlayer.play()

}

@objc func finishVideo()
{
print("Video Finished")
self.performSegue(withIdentifier: "unwindToFifteenSeconds", sender: self)
}

// Remove Observer
deinit {
NotificationCenter.default.removeObserver(self)
}

}


Above code is only to play a video that was captured by the camera using AVfoundation. I only need to add CIfilters to this played video.










share|improve this question



























    2















    I have recorded a video using avfoundation and after clicking on the video its start to play on a new viewcontroller. The problem is, I wouldn't be able to add filter effects to a playing video. Following is the code of what i have achieved.



    import UIKit
    import AVFoundation

    class VideoPlayback: UIViewController {

    let avPlayer = AVPlayer()
    var avPlayerLayer: AVPlayerLayer!

    var videoURL: URL!

    @IBOutlet weak var videoView: UIView!

    override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

    avPlayerLayer = AVPlayerLayer(player: avPlayer)
    avPlayerLayer.frame = view.bounds
    avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    videoView.layer.insertSublayer(avPlayerLayer, at: 0)

    view.layoutIfNeeded()

    let playerItem = AVPlayerItem(url: videoURL as URL)
    avPlayer.replaceCurrentItem(with: playerItem)

    avPlayer.play()

    }

    @objc func finishVideo()
    {
    print("Video Finished")
    self.performSegue(withIdentifier: "unwindToFifteenSeconds", sender: self)
    }

    // Remove Observer
    deinit {
    NotificationCenter.default.removeObserver(self)
    }

    }


    Above code is only to play a video that was captured by the camera using AVfoundation. I only need to add CIfilters to this played video.










    share|improve this question

























      2












      2








      2








      I have recorded a video using avfoundation and after clicking on the video its start to play on a new viewcontroller. The problem is, I wouldn't be able to add filter effects to a playing video. Following is the code of what i have achieved.



      import UIKit
      import AVFoundation

      class VideoPlayback: UIViewController {

      let avPlayer = AVPlayer()
      var avPlayerLayer: AVPlayerLayer!

      var videoURL: URL!

      @IBOutlet weak var videoView: UIView!

      override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

      avPlayerLayer = AVPlayerLayer(player: avPlayer)
      avPlayerLayer.frame = view.bounds
      avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
      videoView.layer.insertSublayer(avPlayerLayer, at: 0)

      view.layoutIfNeeded()

      let playerItem = AVPlayerItem(url: videoURL as URL)
      avPlayer.replaceCurrentItem(with: playerItem)

      avPlayer.play()

      }

      @objc func finishVideo()
      {
      print("Video Finished")
      self.performSegue(withIdentifier: "unwindToFifteenSeconds", sender: self)
      }

      // Remove Observer
      deinit {
      NotificationCenter.default.removeObserver(self)
      }

      }


      Above code is only to play a video that was captured by the camera using AVfoundation. I only need to add CIfilters to this played video.










      share|improve this question














      I have recorded a video using avfoundation and after clicking on the video its start to play on a new viewcontroller. The problem is, I wouldn't be able to add filter effects to a playing video. Following is the code of what i have achieved.



      import UIKit
      import AVFoundation

      class VideoPlayback: UIViewController {

      let avPlayer = AVPlayer()
      var avPlayerLayer: AVPlayerLayer!

      var videoURL: URL!

      @IBOutlet weak var videoView: UIView!

      override func viewDidLoad() {
      super.viewDidLoad()

      NotificationCenter.default.addObserver(self, selector: #selector(finishVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)

      avPlayerLayer = AVPlayerLayer(player: avPlayer)
      avPlayerLayer.frame = view.bounds
      avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
      videoView.layer.insertSublayer(avPlayerLayer, at: 0)

      view.layoutIfNeeded()

      let playerItem = AVPlayerItem(url: videoURL as URL)
      avPlayer.replaceCurrentItem(with: playerItem)

      avPlayer.play()

      }

      @objc func finishVideo()
      {
      print("Video Finished")
      self.performSegue(withIdentifier: "unwindToFifteenSeconds", sender: self)
      }

      // Remove Observer
      deinit {
      NotificationCenter.default.removeObserver(self)
      }

      }


      Above code is only to play a video that was captured by the camera using AVfoundation. I only need to add CIfilters to this played video.







      swift video filter avfoundation cifilter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 8:43









      Muneeb JanMuneeb Jan

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          2















          you can apply a filter to a video




          let filter = CIFilter(name: "CIGaussianBlur")!
          let asset = AVAsset(url: streamURL)
          let item = AVPlayerItem(asset: asset)
          item.videoComposition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

          // Clamp to avoid blurring transparent pixels at the image edges
          let source = request.sourceImage.clampingToExtent()
          filter.setValue(source, forKey: kCIInputImageKey)

          // Vary filter parameters based on video timing
          let seconds = CMTimeGetSeconds(request.compositionTime)
          filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)

          // Crop the blurred output to the bounds of the original image
          let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

          // Provide the filter output to the composition
          request.finish(with: output, context: nil)
          })





          share|improve this answer
























          • i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

            – Muneeb Jan
            Jan 2 at 10:33











          • you can use in "playerItem.videocompostion=...................."

            – Pratap
            Jan 2 at 10:52






          • 1





            Good answer. @Pratap

            – Sparsh
            Jan 7 at 10:55













          • the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

            – Muneeb Jan
            Jan 10 at 6:20











          • you can change filter name it is simple and accept answer

            – Pratap
            Jan 10 at 6:27











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003404%2fhow-to-add-cifilters-to-a-video-at-runtime%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2















          you can apply a filter to a video




          let filter = CIFilter(name: "CIGaussianBlur")!
          let asset = AVAsset(url: streamURL)
          let item = AVPlayerItem(asset: asset)
          item.videoComposition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

          // Clamp to avoid blurring transparent pixels at the image edges
          let source = request.sourceImage.clampingToExtent()
          filter.setValue(source, forKey: kCIInputImageKey)

          // Vary filter parameters based on video timing
          let seconds = CMTimeGetSeconds(request.compositionTime)
          filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)

          // Crop the blurred output to the bounds of the original image
          let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

          // Provide the filter output to the composition
          request.finish(with: output, context: nil)
          })





          share|improve this answer
























          • i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

            – Muneeb Jan
            Jan 2 at 10:33











          • you can use in "playerItem.videocompostion=...................."

            – Pratap
            Jan 2 at 10:52






          • 1





            Good answer. @Pratap

            – Sparsh
            Jan 7 at 10:55













          • the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

            – Muneeb Jan
            Jan 10 at 6:20











          • you can change filter name it is simple and accept answer

            – Pratap
            Jan 10 at 6:27
















          2















          you can apply a filter to a video




          let filter = CIFilter(name: "CIGaussianBlur")!
          let asset = AVAsset(url: streamURL)
          let item = AVPlayerItem(asset: asset)
          item.videoComposition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

          // Clamp to avoid blurring transparent pixels at the image edges
          let source = request.sourceImage.clampingToExtent()
          filter.setValue(source, forKey: kCIInputImageKey)

          // Vary filter parameters based on video timing
          let seconds = CMTimeGetSeconds(request.compositionTime)
          filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)

          // Crop the blurred output to the bounds of the original image
          let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

          // Provide the filter output to the composition
          request.finish(with: output, context: nil)
          })





          share|improve this answer
























          • i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

            – Muneeb Jan
            Jan 2 at 10:33











          • you can use in "playerItem.videocompostion=...................."

            – Pratap
            Jan 2 at 10:52






          • 1





            Good answer. @Pratap

            – Sparsh
            Jan 7 at 10:55













          • the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

            – Muneeb Jan
            Jan 10 at 6:20











          • you can change filter name it is simple and accept answer

            – Pratap
            Jan 10 at 6:27














          2












          2








          2








          you can apply a filter to a video




          let filter = CIFilter(name: "CIGaussianBlur")!
          let asset = AVAsset(url: streamURL)
          let item = AVPlayerItem(asset: asset)
          item.videoComposition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

          // Clamp to avoid blurring transparent pixels at the image edges
          let source = request.sourceImage.clampingToExtent()
          filter.setValue(source, forKey: kCIInputImageKey)

          // Vary filter parameters based on video timing
          let seconds = CMTimeGetSeconds(request.compositionTime)
          filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)

          // Crop the blurred output to the bounds of the original image
          let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

          // Provide the filter output to the composition
          request.finish(with: output, context: nil)
          })





          share|improve this answer














          you can apply a filter to a video




          let filter = CIFilter(name: "CIGaussianBlur")!
          let asset = AVAsset(url: streamURL)
          let item = AVPlayerItem(asset: asset)
          item.videoComposition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

          // Clamp to avoid blurring transparent pixels at the image edges
          let source = request.sourceImage.clampingToExtent()
          filter.setValue(source, forKey: kCIInputImageKey)

          // Vary filter parameters based on video timing
          let seconds = CMTimeGetSeconds(request.compositionTime)
          filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)

          // Crop the blurred output to the bounds of the original image
          let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

          // Provide the filter output to the composition
          request.finish(with: output, context: nil)
          })






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 10:20









          PratapPratap

          814




          814













          • i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

            – Muneeb Jan
            Jan 2 at 10:33











          • you can use in "playerItem.videocompostion=...................."

            – Pratap
            Jan 2 at 10:52






          • 1





            Good answer. @Pratap

            – Sparsh
            Jan 7 at 10:55













          • the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

            – Muneeb Jan
            Jan 10 at 6:20











          • you can change filter name it is simple and accept answer

            – Pratap
            Jan 10 at 6:27



















          • i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

            – Muneeb Jan
            Jan 2 at 10:33











          • you can use in "playerItem.videocompostion=...................."

            – Pratap
            Jan 2 at 10:52






          • 1





            Good answer. @Pratap

            – Sparsh
            Jan 7 at 10:55













          • the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

            – Muneeb Jan
            Jan 10 at 6:20











          • you can change filter name it is simple and accept answer

            – Pratap
            Jan 10 at 6:27

















          i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

          – Muneeb Jan
          Jan 2 at 10:33





          i don't get it, could you please tell me where i use this code, sorry for asking too much, basically i am new in ios.

          – Muneeb Jan
          Jan 2 at 10:33













          you can use in "playerItem.videocompostion=...................."

          – Pratap
          Jan 2 at 10:52





          you can use in "playerItem.videocompostion=...................."

          – Pratap
          Jan 2 at 10:52




          1




          1





          Good answer. @Pratap

          – Sparsh
          Jan 7 at 10:55







          Good answer. @Pratap

          – Sparsh
          Jan 7 at 10:55















          the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

          – Muneeb Jan
          Jan 10 at 6:20





          the code you gave works only for "CIGaussianBlur" filter. if i want to use another CIFilter, what do i do? @Pra

          – Muneeb Jan
          Jan 10 at 6:20













          you can change filter name it is simple and accept answer

          – Pratap
          Jan 10 at 6:27





          you can change filter name it is simple and accept answer

          – Pratap
          Jan 10 at 6:27




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54003404%2fhow-to-add-cifilters-to-a-video-at-runtime%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules