Search code examples
iosvideoavplayerframe-rateswift5

AVPlayer SET fps


Hi i've a classic player

var player = AVPlayer(url: fileUrl)
player.play() 

Is possibile to set FPS of player? For example i want to play a slow motion video (240fps) at 30fps

i try

player.play()
player.rate = 0.5

but this only play a 240fps video at 120fps.

Is possible to change the FPS during video playback

let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: false) {_ in
        player.rate = 0.5
}

this works great but cannot set 30FPS like above

Thanks!!


Solution

  • If I understand the question properly, you just want to slow the content, so setting the rate is the correct way (and you can do it also during content playback).

    If you want to go from 240 to 30 fps than it means you want to set the rate to 30/240 which means 1/8.

    So this should do:

    player.rate = Float(1)/Float(8)
    

    You should also set the playerItem audioPitchAlgorithm to something other than lowQualityZeroLatency to allow it to go below 0.5 up to 1/32

    lowQualityZeroLatency

    This algorithm is suitable for brief fast-forward and rewind effects as well as low quality voice. The rate is snapped to {0.5, 0.666667, 0.8, 1.0, 1.25, 1.5, 2.0}.

    This snipped that I tested correctly went to 1/8th of the speed.

    let playerItem = AVPlayerItem(url: URL(string:"https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8")!)
    playerItem.audioTimePitchAlgorithm = .varispeed
    let avPlayer = AVPlayer(playerItem: playerItem)
    
    let vc = AVPlayerViewController()
    vc.player = avPlayer
    
    avPlayer.rate = 0.125
    self.present(vc, animated: true)