1

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.

nmnir
  • 568
  • 1
  • 10
  • 24

1 Answers1

0

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.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159
  • TL;DR In some cases you do want to query possible frame rates. I did that test while developing a DAL plugin. According to the docs (CMIOHardwareStream.h), while discussing frame rates, "If no qualifier is used, the rates of the current format (as reported via kCMIOStreamPropertyFormatDescription) will be returned. If a qualifier is present, it contains the CMFormatDescriptionRef whose frame rates are desired." You are right about the CMTime having a false valid flag. – nmnir Jun 30 '22 at 11:01
  • Look at `AVCaptureDeviceFormat.videoSupportedFrameRateRanges` for supported device frame rates. – Rhythmic Fistman Jul 01 '22 at 13:39