Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/clip-export/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions funclip/utils/subtitle_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 = []
Expand Down Expand Up @@ -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()))
Expand Down
32 changes: 32 additions & 0 deletions tests/test_subtitle_boundaries.py
Original file line number Diff line number Diff line change
@@ -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)