Get time of a frame for video --

2016-06-20  本文已影响172人  TomatosX

Today, I found that my implement was wrong to get the time of a frame. I think that since FPS represents the number of frames per second, then time of a frame should be 1 / fps, but in fact the result is wrong.

Now, let us talk about how to get time of a frame.

First, we need to create a AVURLAsset object:

let asset = AVURLAsset(URL: NSURL(fileURLWithPath: videoPath), options: nil)

We can get the fps of video from AVURLAsset object, like this :

let fps = Int32(asset.tracksWithMediaType(AVMediaTypeVideo).first!.nominalFrameRate)

The following, we should get the total number of frames :

let sumTime = Int32(asset.duration.value)  /  asset.duration.timescale;
let sumFrame = sumTime * fps

Here's sunTime is seconds, that's the total time for the video. But the type of time is Int, I used another way to get the total time of the video. like this:

let totalTime = Float(CMTimeGetSeconds(playerItem.asset.duration))

Now, we can to compute the time of a frame:

let frameTime = totalTime / Float(sumFrame)
上一篇下一篇

猜你喜欢

热点阅读