orchetect/TimecodeKit

`Timecode.FrameRate`: Add failable init from rational frame rate (fraction)

orchetect opened this issue · 3 comments

Timecode.FrameRate provides a fraction property that returns a numerator and denominator.

public var fraction: (numerator: Int, denominator: Int) {
switch self {
case ._23_976: return (numerator: 24000, denominator: 1001)
case ._24: return (numerator: 24, denominator: 1)
case ._24_98: return (numerator: 25000, denominator: 1001)
case ._25: return (numerator: 25, denominator: 1)
case ._29_97: return (numerator: 30000, denominator: 1001)
case ._29_97_drop: return (numerator: 30000, denominator: 1001)
case ._30: return (numerator: 30, denominator: 1)
case ._30_drop: return (numerator: 30, denominator: 1)
case ._47_952: return (numerator: 48000, denominator: 1001)
case ._48: return (numerator: 48, denominator: 1)
case ._50: return (numerator: 50, denominator: 1)
case ._59_94: return (numerator: 60000, denominator: 1001)
case ._59_94_drop: return (numerator: 60000, denominator: 1001)
case ._60: return (numerator: 60, denominator: 1)
case ._60_drop: return (numerator: 60, denominator: 1)
case ._100: return (numerator: 100, denominator: 1)
case ._119_88: return (numerator: 120_000, denominator: 1001)
case ._119_88_drop: return (numerator: 120_000, denominator: 1001)
case ._120: return (numerator: 120, denominator: 1)
case ._120_drop: return (numerator: 120, denominator: 1)
}

These values are used for encoding frame rate into various file formats such as AAF and Final Cut Pro XML.

It would make sense to add a failable init that can accept these values. Since a drop rate cannot be inferred from a fraction alone, it must be specified imperatively as a boolean flag.

extension Timecode.FrameRate {
    public init?(numerator: Int, denominator: Int, drop: Bool) { }
}

Added and pushed to main branch. Will be in next release.

Reopening.

Current implementation is limited to the fraction integer literals. The rate should be derived from evaluating the fraction instead, since different file formats (AAF, FCP XML, etc.) may encode these fractions with different powers of 10.

Implemented refactor. Will be in next release.