Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video playback GPUImage and swift

I want to process a video with the chromakey filter and the output should be reproduced on the screen.

Below the code I developed that is not showing any video and I can't understand why:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let aSelector : Selector = "start:"
    let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
    tapGesture.numberOfTapsRequired = 1
    view.addGestureRecognizer(tapGesture)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func start(sender: AnyObject) {
    NSLog("tap pressed")
    play()
}

func play(){
    let path = NSBundle.mainBundle().pathForResource("glass_buster", ofType: "mp4")
    let player = AVPlayer()
    let pathURL = NSURL.fileURLWithPath(path!)
    NSLog(pathURL.absoluteString)
    let playerItem = AVPlayerItem(URL: pathURL)
    player.replaceCurrentItemWithPlayerItem(playerItem)


    let gpuMovie = GPUImageMovie(playerItem: playerItem)
    gpuMovie.playAtActualSpeed = true
    let filter = GPUImageChromaKeyFilter()
    gpuMovie.addTarget(filter)
    gpuMovie.playAtActualSpeed = true


    gpuMovie.startProcessing()
    player.play()


}
like image 410
Kerby82 Avatar asked Oct 20 '15 19:10

Kerby82


1 Answers

You also need a GPUImageView which displays the movie and you cannot store the player in a local var. Try the following:

var player: AVPlayer! = nil
var playerItem : AVPlayerItem! = nil

func play() {
    let path = NSBundle.mainBundle().pathForResource("glass_buster", ofType: "mp4")
    player = AVPlayer()
    let pathURL = NSURL.fileURLWithPath(path!)

    playerItem = AVPlayerItem(URL: pathURL)
    player.replaceCurrentItemWithPlayerItem(playerItem)


    let gpuMovie = GPUImageMovie(playerItem: playerItem)
    gpuMovie.playAtActualSpeed = true

    let filteredView: GPUImageView = GPUImageView();
    filteredView.frame = self.view.frame
    self.view.addSubview(filteredView)

    let filter = GPUImageChromaKeyFilter()
    gpuMovie.addTarget(filter)
    gpuMovie.playAtActualSpeed = true
    filter.addTarget(filteredView)

    gpuMovie.startProcessing()
    player.play()
}
like image 155
niggeulimann Avatar answered Sep 28 '22 13:09

niggeulimann