美文网首页
Get time of a frame for video --

Get time of a frame for video --

作者: TomatosX | 来源:发表于2016-06-20 23:18 被阅读172次

    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)
    

    相关文章

      网友评论

          本文标题:Get time of a frame for video --

          本文链接:https://www.haomeiwen.com/subject/fnzwdttx.html