Getting frames rate from CMFormatDescription

359 Views Asked by At

I am trying to calculate frame rate of a video format based on CMFormatDescription but I'm getting strange output that I do not know what to do with. According to the documentation, "value/timescale = seconds". This is also the answer in this question.

The code is being called while getting a video stream from FaceTime camera:

let av = device.activeFormat
let fd = av.formatDescription
print("time scale",fd.frameDuration.timescale)
print("value",fd.frameDuration.value)  
print("value/timescale=",fd.frameDuration.value)/Int64(fd.frameDuration.timescale))

This is the output:

time scale 480
value 2749654773878
value/timescale= 5728447445.579166

What am I missing? What is the frames rate?

EDIT: It seems that there is a bug or perhaps something is terribly wrong. time scale is always == height (of the format description). I tried it with a usb camera and they are always equal.

1

There are 1 best solutions below

2
Gordon Childs On

TL;DR You don't query a device's frame rate, but you can choose the frame rate you want

The CMTime value you're receiving has its flags field set to zero. This means it is invalid.

CMFormatDescriptions describe several media types, and frameDuration is not a video property. An invalid CMTime result is CMFormatDescription's way of saying "not found". I guess the timescale being the format's height is just Undefined Behaviour & I'll bet it's different on an intel mac.

This problem is easier to see in plain old C, where the sugared swift frameDuration property is called CMTimeCodeFormatDescriptionGetFrameDuration(). Frame duration is a TimeCode property (whatever that is). Mixing up your format description media types does not result in compiler errors in either language.