Skip to content
Closed
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
29 changes: 18 additions & 11 deletions funasr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,18 @@ def _timestamp_bounds_ms(result):
return min(start for start, _ in bounds), max(end for _, end in bounds)


def _fallback_bounds_ms(timestamps, audio_path):
"""Span for a single cue when no sentence timestamps exist: token bounds, else audio length."""
bounds = _timestamp_bounds_ms({"timestamp": timestamps})
if bounds:
return bounds
try:
import soundfile as sf
return 0, int(sf.info(audio_path).duration * 1000)
except Exception:
return 0, 0


def _format_output(text, segments, timestamps, fmt, audio_path, model_name, language, elapsed):
if fmt == "text":
return text
Expand All @@ -547,18 +559,13 @@ def _format_output(text, segments, timestamps, fmt, audio_path, model_name, lang
return format_srt(segments)
# No per-sentence timestamps: emit one valid cue spanning the known
# timestamp/audio bounds instead of a bogus 99:59:59 end time.
timestamp_bounds = _timestamp_bounds_ms({"timestamp": timestamps})
if timestamp_bounds:
start_ms, end_ms = timestamp_bounds
return f"1\n{_srt_time(start_ms)} --> {_srt_time(end_ms)}\n{text}\n"
try:
import soundfile as sf
dur_ms = int(sf.info(audio_path).duration * 1000)
except Exception:
dur_ms = 0
return f"1\n00:00:00,000 --> {_srt_time(dur_ms)}\n{text}\n"
start_ms, end_ms = _fallback_bounds_ms(timestamps, audio_path)
return f"1\n{_srt_time(start_ms)} --> {_srt_time(end_ms)}\n{text}\n"
elif fmt == "tsv":
return format_tsv(segments) if segments else f"start\tend\ttext\n0.000\t0.000\t{text}"
if segments:
return format_tsv(segments)
start_ms, end_ms = _fallback_bounds_ms(timestamps, audio_path)
return format_tsv([{"start": start_ms, "end": end_ms, "text": text}])


def _get_version():
Expand Down
42 changes: 42 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,3 +609,45 @@ def test_merge_subtitle_segments_avoids_single_character_phrase_breaks():
for boundary in boundary_offsets
)
search_from = phrase_start + 1


class NoSentenceAutoModel(DummyAutoModel):
def generate(self, **kwargs):
self.generate_kwargs = kwargs
return [
{
"text": "hello world",
"sentence_info": [],
"timestamp": [[300, 800], [900, 1700]],
}
]


def _run_cli_with_output_format(tmp_path, output_format):
audio_path = tmp_path / "sample.wav"
audio_path.write_bytes(b"not a real wav")
fake_torch = types.SimpleNamespace(
cuda=types.SimpleNamespace(is_available=lambda: False),
)
argv = ["funasr", str(audio_path), "--output-format", output_format]
out = io.StringIO()
with (
patch.object(sys, "argv", argv),
patch.dict(sys.modules, {"torch": fake_torch}),
patch("funasr.AutoModel", NoSentenceAutoModel),
redirect_stdout(out),
):
cli.main()
return out.getvalue()


def test_cli_tsv_without_sentence_info_spans_token_timestamps(tmp_path):
assert _run_cli_with_output_format(tmp_path, "tsv") == (
"start\tend\ttext\n0.300\t1.700\thello world\n"
)


def test_cli_srt_without_sentence_info_spans_token_timestamps(tmp_path):
assert _run_cli_with_output_format(tmp_path, "srt") == (
"1\n00:00:00,300 --> 00:00:01,700\nhello world\n\n"
)