diff --git a/ChangeLog.md b/ChangeLog.md index cc3c114..7b74d78 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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` diff --git a/dftt_timecode/core/dftt_timecode.py b/dftt_timecode/core/dftt_timecode.py index 8e1f9f6..066c162 100644 --- a/dftt_timecode/core/dftt_timecode.py +++ b/dftt_timecode/core/dftt_timecode.py @@ -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 * @@ -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'(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'})' @@ -828,4 +832,3 @@ def __float__(self): def __int__(self): return self.framecount - diff --git a/test/test_dftt_timecode.py b/test/test_dftt_timecode.py index 173202e..0cf046d 100644 --- a/test/test_dftt_timecode.py +++ b/test/test_dftt_timecode.py @@ -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 \ No newline at end of file