Skip to content

Commit

Permalink
feat(converts): time code 00:00:00:00 formats to frames closes #18
Browse files Browse the repository at this point in the history
fix(converts): rename files and fixes some docstring #14
  • Loading branch information
mdsanima committed Feb 23, 2022
1 parent a8100e1 commit abd260b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 42 deletions.
42 changes: 0 additions & 42 deletions src/mdsanima_dev/utils/convert.py

This file was deleted.

95 changes: 95 additions & 0 deletions src/mdsanima_dev/utils/converts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyritht © 2022 Marcin Różewski MDSANIMA


"""
Functions that may be useful in VFX and Animation Industry.
"""


def _seconds():
# setup seconds variables
sec_in_min = 60
sec_in_hrs = sec_in_min * sec_in_min

return sec_in_min, sec_in_hrs


def frames_to_time_code(frames: int, fps: float) -> str:
"""
Function converts number of frames to time code ``00:00:00:00`` format.
:param frames: Number of total frames.
:type frames: int
:param fps: Frames per seconds.
:type fps: float
:return: Time code format.
:rtype: str
:usage:
Assigning function calling to a variable:
.. code:: python
from mdsanima_dev.utils.converts import frames_to_time_code
time_code = frames_to_time_code(240, 24)
print(time_code)
"""
# assigning function calling to a variable setup seconds
sec_in_min, sec_in_hrs = _seconds()

# setup frames
frames_in_sec = fps
frames_in_hrs = frames // frames_in_sec
frame_mod_fps = frames % frames_in_sec

# calculating time code
hrs = int(frames_in_hrs // sec_in_hrs)
min = int(frames_in_hrs // sec_in_min % sec_in_min)
sec = int(frames_in_hrs % sec_in_min)
fra = int(frame_mod_fps)

# formating time code
time_code = str("%02d:%02d:%02d:%02d" % (hrs, min, sec, fra))

return time_code


def time_code_to_frames(time_code: str, fps: float) -> int:
"""
Function converts time code ``00:00:00:00`` format to frames number.
:param time_code: Number of total frames.
:type time_code: str
:param fps: Frames per seconds.
:type fps: float
:return: Frames number.
:rtype: int
:usage:
Assigning function calling to a variable:
.. code:: python
from mdsanima_dev.utils.converts import time_code_to_frames
frames = time_code_to_frames("00:00:10:00", 24)
print(frames)
"""
# assigning function calling to a variable setup seconds
sec_in_min, sec_in_hrs = _seconds()

# extract single values
tc_hrs = int(time_code.split(":")[0])
tc_min = int(time_code.split(":")[1])
tc_sec = int(time_code.split(":")[2])
tc_fra = int(time_code.split(":")[3])

# setup and calculating frames
frames_in_sec = fps
tc_sec_min = tc_min * sec_in_min
tc_sec_hrs = tc_hrs * sec_in_hrs
tc_frames = tc_sec + tc_sec_min + tc_sec_hrs
frames = round(tc_frames * frames_in_sec + tc_fra)

return frames

0 comments on commit abd260b

Please sign in to comment.