From 0999ec19950c950ff5387881e14af516a924ae47 Mon Sep 17 00:00:00 2001 From: zhifu gao Date: Wed, 2 Dec 2026 12:50:11 +0800 Subject: [PATCH] Keep exact millisecond boundaries when clipping subtitles Respect decimal clip boundaries before truncating to the SRT millisecond. Skip an empty token selection rather than indexing an empty list. Signed-off-by: zhifu gao --- .github/actions/clip-export/action.yml | 2 +- funclip/utils/subtitle_utils.py | 15 ++++++++---- tests/test_subtitle_boundaries.py | 32 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/test_subtitle_boundaries.py diff --git a/.github/actions/clip-export/action.yml b/.github/actions/clip-export/action.yml index e810ec9..77ede86 100644 --- a/.github/actions/clip-export/action.yml +++ b/.github/actions/clip-export/action.yml @@ -20,7 +20,7 @@ runs: shell: bash env: HF_HUB_OFFLINE: "1" - run: python -m pytest -q tests/test_cli_audio_clip.py tests/test_cli_video_clip.py tests/test_video_recognition_audio.py + run: python -m pytest -q tests/test_cli_audio_clip.py tests/test_cli_video_clip.py tests/test_video_recognition_audio.py tests/test_subtitle_boundaries.py - name: Test the CLI from both built archives id: archives shell: bash diff --git a/funclip/utils/subtitle_utils.py b/funclip/utils/subtitle_utils.py index eaf9c2f..03906f1 100644 --- a/funclip/utils/subtitle_utils.py +++ b/funclip/utils/subtitle_utils.py @@ -3,6 +3,12 @@ # Copyright FunASR (https://github.com/alibaba-damo-academy/FunClip). All Rights Reserved. # MIT License (https://opensource.org/licenses/MIT) import re +from decimal import Decimal + + +def _milliseconds(seconds): + # Multiplying a float first can turn 1.001 s into 1000.9999999999999 ms. + return Decimal(str(seconds)) * 1000 def time_convert(ms): ms = int(ms) @@ -46,9 +52,10 @@ def text(self): res += " " + word return res.lstrip().rstrip("、。,") def srt(self, acc_ost=0.0): + offset_ms = _milliseconds(acc_ost) return "{} --> {}\n{}\n".format( - time_convert(self.start_sec+acc_ost*1000), - time_convert(self.end_sec+acc_ost*1000), + time_convert(Decimal(str(self.start_sec)) + offset_ms), + time_convert(Decimal(str(self.end_sec)) + offset_ms), self.text()) def time(self, acc_ost=0.0): return (self.start_sec/1000+acc_ost, self.end_sec/1000+acc_ost) @@ -67,7 +74,7 @@ def generate_srt(sentence_list): return srt_total def generate_srt_clip(sentence_list, start, end, begin_index=0, time_acc_ost=0.0): - start, end = int(start * 1000), int(end * 1000) + start, end = int(_milliseconds(start)), int(_milliseconds(end)) srt_total = '' cc = 1 + begin_index subs = [] @@ -108,7 +115,7 @@ def generate_srt_clip(sentence_list, start, end, begin_index=0, time_acc_ost=0.0 # _text = " ".join(sent['text'][_start:_end]) _text = sent['text'][_start:_end] _ts = sent['timestamp'][_start:_end] - if len(ts): + if len(_ts): t2s = Text2SRT(_text, _ts, offset=start) srt_total += "{}\n{}".format(cc, t2s.srt(time_acc_ost)) subs.append((t2s.time(time_acc_ost), t2s.text())) diff --git a/tests/test_subtitle_boundaries.py b/tests/test_subtitle_boundaries.py new file mode 100644 index 0000000..4a241f1 --- /dev/null +++ b/tests/test_subtitle_boundaries.py @@ -0,0 +1,32 @@ +from funclip.utils.subtitle_utils import generate_srt_clip + + +def test_word_ending_at_exact_clip_boundary_is_included(): + sentences = [{"text": "one", "timestamp": [[0, 1001]]}] + srt, subs, next_index = generate_srt_clip(sentences, 0, 16016 / 16000) + assert srt.splitlines() == ["1", "00:00:00,000 --> 00:00:01,001", "one"] + assert subs == [((0.0, 1.001), "one")] + assert next_index == 2 + + +def test_source_start_preserves_integral_milliseconds(): + sentences = [{"text": "two", "timestamp": [[1001, 2001]]}] + srt, _, _ = generate_srt_clip(sentences, 1.001, 2.001) + assert srt.splitlines()[1] == "00:00:00,000 --> 00:00:01,000" + + +def test_output_position_of_16016_samples_is_exactly_1001_ms(): + sentences = [{"text": "two", "timestamp": [[4000, 5000]]}] + srt, _, _ = generate_srt_clip(sentences, 4, 5, time_acc_ost=16016 / 16000) + assert srt.splitlines()[1] == "00:00:01,001 --> 00:00:02,001" + + +def test_offset_of_one_sample_before_a_millisecond_is_not_rounded_up(): + sentences = [{"text": "two", "timestamp": [[4000, 5000]]}] + srt, _, _ = generate_srt_clip(sentences, 4, 5, time_acc_ost=16015 / 16000) + assert srt.splitlines()[1] == "00:00:01,000 --> 00:00:02,000" + + +def test_clip_ending_inside_first_word_has_no_finished_token(): + sentences = [{"text": "one", "timestamp": [[0, 1001]]}] + assert generate_srt_clip(sentences, 0, 16015 / 16000) == ("", [], 1)