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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,089評(píng)論 0 23
  • Andorid渠道市場(chǎng)有多分散呢?分散到比Android碎片化還嚴(yán)重,你還在為多渠道打包而頭疼嗎?美團(tuán)提供了速度快...
    非著名程序員閱讀 1,857評(píng)論 5 45
  • 本文參加# 印象青農(nóng),萌有感受#活動(dòng),本文承諾,文章內(nèi)容為原創(chuàng),且未在其他平臺(tái)發(fā)表過 大學(xué)生活豐富多彩,每...
    Aqua艾可閱讀 179評(píng)論 0 1
  • 身入翰林府,沒有白學(xué)苦。 誤入孫山界,換個(gè)方式做。 前方路途多,路路都有坡。 人生就這樣,不必?zé)蓝唷?/div>
    伊薩閱讀 273評(píng)論 20 27
  • 日記新思維:目的~目標(biāo)~想法~行動(dòng)~總結(jié) 告別這段時(shí)間,給自己做個(gè)總結(jié),重新起航。 找到一份讓自己充滿力量感的工作...
    木兮日記閱讀 271評(píng)論 3 0

友情鏈接更多精彩內(nèi)容