30

I'm making a player and I want to list all files and in front of all files I want to present the duration of the video.

The only problem is that I'm not getting the right video duration, sometimes it return a duration completely wrong.

I've tried the below solution:

let asset = AVAsset(url: "video.mp4")

let duration = asset.duration.seconds

So that it, the time sometimes give a value sometimes another. if someone know a possible solution I'm glad to heard.

I have update the code using one possible solution but it didn't work well,

let asset = AVAsset(url: url)

let duration = asset.duration

let durationTime = CMTimeGetSeconds(duration)

let minutes = Double(durationTime / 60)

I've tried with a video of 11:47 minutes of duration and it returns me = 11:78, how could a video have 11 minutes and 78 seconds?

So I think the problem is with the video, and I picked another video of 1:16 minutes and again the returned value is 1:26 (10 seconds wrong)

Mr. James
  • 456
  • 1
  • 5
  • 12

2 Answers2

49

This works for me:

import AVFoundation
import CoreMedia

...

    if let url = Bundle.main.url(forResource: "small", withExtension: "mp4") {
        let asset = AVAsset(url: url)

        let duration = asset.duration
        let durationTime = CMTimeGetSeconds(duration)

        print(durationTime)
    }

For the video here it prints "5.568" which is correct.

Edit from comments:

A video that returns 707 seconds when divided by 60 sec/min is 11.78. This is 11.78 minutes, or 11 minutes and 0.78min * 60sec/min = 47sec, total is 11 min 47 sec

David S.
  • 6,567
  • 1
  • 25
  • 45
  • Hey man, I copied a pasted your code, don't change one line and de duration is givin 707 for a video of 11 minutes, so I divided 707 by 60 and it return 11.78, something is wrong. your answer is good but dint't solve my problem , i will keep trying. thxs – Mr. James May 30 '17 at 18:41
  • 2
    If the video is 11 minutes and 47 seconds long then 707 is correct. 707 seconds is 11.78 minutes. – David S. May 30 '17 at 19:29
  • But the correct time is 11:47, and how to I get this (11:47), I don't know if I'm not understand something or missing something, can you explain @DavidShaw – Mr. James May 30 '17 at 19:35
  • 9
    You don't seem to understand the time conversion. 707 seconds is 11.78 minutes. 0.78 minutes is NOT 78 seconds. 0.78min * 60sec/min = 47 sec – David S. May 30 '17 at 19:42
  • 6
    David, I'm soo dumb, now I understand – Mr. James May 30 '17 at 19:56
  • it is what i am looking for – dinesh sharma May 31 '19 at 12:14
3
if let url = Bundle.main.url(forResource: "small", withExtension: "mp4") {
        let asset = AVAsset(url: url)

        let duration = asset.duration
        let durationTime = CMTimeGetSeconds(duration)
        let minutes = durationTime/60
        let seconds = durationTime%60
        let videoDuration = "\(minutes):\(seconds)"
        print(videoDuration)
    }
  • 1
    When 707 divide by 60 it gives 11 minutes and the remaining is seconds, so by using remainder you can get the remaining seconds of the video like 707%60. – Praveen kumar Jan 05 '21 at 04:57