Skip to content

Commit

Permalink
Add audio sample count calculation function while maintain precision …
Browse files Browse the repository at this point in the history
…of the timestamp

Fixes OwenYou#9
  • Loading branch information
WheheoHu committed Oct 18, 2024
1 parent 4c408be commit 7b4c8bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ V0.0.13 changelog

Refactor the time code output function to reduce code duplication

添加 Add:
- 添加`get_audio_sample_count` 方法用于正确输出TC时间戳下的音频采样数, 解决issue [#9](https://github.com/OwenYou/dftt_timecode/issues/9)

Add `get_audio_sample_count` method for correctly outputting the count of audio samples at TC timestamps,solve issue [#9](https://github.com/OwenYou/dftt_timecode/issues/9)

弃用 Deprecate:
- 使用`functools.singledispatchmethod` 代替 `dispatch.InstanceMethodDispatch`

Expand Down
7 changes: 5 additions & 2 deletions dftt_timecode/core/dftt_timecode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from fractions import Fraction
from functools import singledispatchmethod
from math import ceil
from math import ceil, floor
from copy import deepcopy

from dftt_timecode.error import *
Expand Down Expand Up @@ -608,6 +608,10 @@ def set_strict(self, strict=True) -> 'DfttTimecode':
self.__strict = strict
return self

def get_audio_sample_count(self, sample_rate: int) -> int:
numerator,denominator=self.__precise_time.as_integer_ratio()
return floor(numerator * sample_rate/denominator)

def __repr__(self):
return f'<DfttTimecode>(Timecode:{self.timecode_output(self.__type)}, Type:{self.__type},FPS:{float(self.__fps):.02f} {'DF' if self.__drop_frame == True else 'NDF'}, {'Strict' if self.__strict == True else 'Non-Strict'})'

Expand Down Expand Up @@ -828,4 +832,3 @@ def __float__(self):

def __int__(self):
return self.framecount

12 changes: 12 additions & 0 deletions test/test_dftt_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,15 @@ def test_int(tc_value,xvalue):
tc=TC(*tc_value)
assert int(tc) == xvalue


@pytest.mark.parametrize(
argnames="tc_value,sample_rate,xvalue",
argvalues=[(('00:00:01:00', 'auto', 24, False, True),48000,48000),
(('00:00:01:01', 'auto', 24, False, True),48000,50000),
(('00:00:01:01', 'auto', 24, False, True),44100,45937),
],
ids=['ideal','single_frame','24fps_44100']
)
def test_audio_sample_count(tc_value,sample_rate,xvalue):
tc=TC(*tc_value)
assert tc.get_audio_sample_count(sample_rate) == xvalue

0 comments on commit 7b4c8bc

Please sign in to comment.