diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4bd2038..2eebdc0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ Changes ======= +1.2.4 +===== + +* **Update:** It is now possible to supply a ``Fraction`` instances for the + ``framerate`` argument. + 1.2.3 ===== * **Update:** Passing ``frames=0`` will now raise a ValueError. This hopefully diff --git a/tests/test_timecode.py b/tests/test_timecode.py index 93790ad..5131616 100644 --- a/tests/test_timecode.py +++ b/tests/test_timecode.py @@ -1098,3 +1098,22 @@ def test_frames_argument_is_zero(self): str(cm.exception) ) + def test_bug_report_30(self): + """testing bug report 30 + + The claim on the bug report was to get ``00:34:45:09`` from a Timecode + with 23.976 as the frame rate (supplied with Python 3's Fraction + library) and 50000 as the total number of frames. The support for + Fraction instances was missing and it has been added. But the claim for + the resultant Timecode was wrong, the resultant Timecode should have + been ``00:34:43:07`` and that has been approved by DaVinci Resolve. + """ + from fractions import Fraction + framerate = Fraction(24000, 1001) # 23.976023976023978 + frame_idx = 50000 + + tc1 = Timecode(framerate, frames=frame_idx) + self.assertEqual( + '00:34:43:07', + tc1.__repr__() + ) diff --git a/timecode/__init__.py b/timecode/__init__.py index cc46dfc..79db04c 100644 --- a/timecode/__init__.py +++ b/timecode/__init__.py @@ -23,7 +23,7 @@ import math from decimal import Decimal, ROUND_HALF_UP -__version__ = '1.2.3' +__version__ = '1.2.4' def school_math_round(num): @@ -151,9 +151,16 @@ def framerate(self, framerate): # lint:ok if isinstance(framerate, tuple): numerator, denominator = framerate + try: + from fractions import Fraction + if isinstance(framerate, Fraction): + numerator = framerate.numerator + denominator = framerate.denominator + except ImportError: + pass + if numerator and denominator: framerate = round(float(numerator) / float(denominator), 2) - if framerate.is_integer(): framerate = int(framerate) @@ -174,7 +181,7 @@ def framerate(self, framerate): # lint:ok self.drop_frame = False else: self.drop_frame = True - elif framerate in ['23.976', '23.98']: + elif any(map(lambda x: framerate.startswith(x), ['23.976', '23.98'])): framerate = '24' self._int_framerate = 24 elif framerate in ['ms', '1000']: