Skip to content

Commit

Permalink
* **Update:** It is now possible to supply a Fraction instances f…
Browse files Browse the repository at this point in the history
…or the ``framerate`` argument.
  • Loading branch information
eoyilmaz committed Oct 16, 2020
1 parent 85901b0 commit 0ad0c59
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/test_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
)
13 changes: 10 additions & 3 deletions timecode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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']:
Expand Down

0 comments on commit 0ad0c59

Please sign in to comment.