Get time of a frame for video -- iOS


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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容