FunASR-powered video transcription, subtitle generation, and LLM-assisted clipping tool with a local Gradio UI.
Python ai-toolsai-video-editingasrauto-subtitleschinesecontent-creationfunasrfunclipgradiollmparaformerspeech-recognitionspeech-to-textsubtitles-generatortranscriptionvideo-editingvideo-processingvideo-subtitlesvideo-transcriptionwhisper-alternative
fix: normalize Pegasus timestamp ranges (#173)
zhifu gaoc02a1ef5994f
diff --git a/funclip/llm/twelvelabs_api.py b/funclip/llm/twelvelabs_api.pyindex e3a9cc8..be13eeb 100644--- a/funclip/llm/twelvelabs_api.py+++ b/funclip/llm/twelvelabs_api.py@@ -1,6 +1,11 @@import osimport timeimport logging+import re+from decimal import Decimal, ROUND_HALF_UP+++_PEGASUS_SECONDS_RANGE_RE = re.compile(r"\[(\d+(?:\.\d+)?)\s*-\s*(\d+(?:\.\d+)?)\]")# Default instruction asking Pegasus to return segments in the same@@ -17,6 +22,42 @@ PEGASUS_SYSTEM_PROMPT = ()+def _seconds_to_milliseconds(seconds):+ return int(+ (Decimal(seconds) * 1000).quantize(Decimal("1"), rounding=ROUND_HALF_UP)+ )+++def _format_srt_timestamp(milliseconds):+ hours, remainder = divmod(milliseconds, 60 * 60 * 1000)+ minutes, remainder = divmod(remainder, 60 * 1000)+ seconds, milliseconds = divmod(remainder, 1000)+ return "{:02d}:{:02d}:{:02d},{:03d}".format(+ hours, minutes, seconds, milliseconds+ )+++def _normalize_pegasus_timestamps(text):+ """Convert Pegasus decimal-second ranges to FunClip's SRT range format."""+ if not isinstance(text, str):+ return text++ def replace_range(match):+ start_millis = _seconds_to_milliseconds(match.group(1))+ end_millis = _seconds_to_milliseconds(match.group(2))+ if end_millis <= start_millis or end_millis >= 100 * 60 * 60 * 1000:+ logging.warning(+ "Ignoring invalid Pegasus timestamp range: %s", match.group(0)+ )+ return match.group(0)+ return "[{}-{}]".format(+ _format_srt_timestamp(start_millis),+ _format_srt_timestamp(end_millis),+ )++ return _PEGASUS_SECONDS_RANGE_RE.sub(replace_range, text)++def _resolve_video_context(client, video):"""Return a VideoContext for a public URL or a local file path.@@ -80,7 +121,7 @@ def call_twelvelabs_pegasus(apikey,max_tokens=max_tokens,)logging.info("TwelveLabs Pegasus inference done.")- return response.data+ return _normalize_pegasus_timestamps(response.data)if __name__ == '__main__':diff --git a/tests/test_twelvelabs_pegasus.py b/tests/test_twelvelabs_pegasus.pyindex 9406d14..2c62b3a 100644--- a/tests/test_twelvelabs_pegasus.py+++ b/tests/test_twelvelabs_pegasus.py@@ -2,6 +2,7 @@ import osimport sysimport unittestfrom pathlib import Path+from unittest.mock import patchROOT = Path(__file__).resolve().parents[1]sys.path.insert(0, str(ROOT / "funclip"))@@ -11,6 +12,7 @@ from llm.twelvelabs_api import (call_twelvelabs_pegasus,PEGASUS_SYSTEM_PROMPT,)+from utils.trans_utils import extract_timestampsclass TestResolveVideoContext(unittest.TestCase):@@ -30,6 +32,57 @@ class TestResolveVideoContext(unittest.TestCase):self.assertIn("[start_time-end_time]", PEGASUS_SYSTEM_PROMPT)+class TestPegasusTimestampNormalization(unittest.TestCase):+ def _call_with_response(self, response_text):+ with patch("twelvelabs.TwelveLabs") as client_class, patch(+ "llm.twelvelabs_api._resolve_video_context", return_value=object()+ ):+ client_class.return_value.analyze.return_value.data = response_text+ return call_twelvelabs_pegasus(+ "test-key", "https://example.com/clip.mp4"+ )++ def test_decimal_and_integer_ranges_feed_existing_parser(self):+ response = (+ "1. [12.5-15.0] first highlight\n"+ "2. [120 - 135] second highlight"+ )++ normalized = self._call_with_response(response)++ self.assertEqual(+ normalized,+ "1. [00:00:12,500-00:00:15,000] first highlight\n"+ "2. [00:02:00,000-00:02:15,000] second highlight",+ )+ self.assertEqual(+ extract_timestamps(normalized), [[12500, 15000], [120000, 135000]]+ )++ def test_existing_srt_range_is_unchanged(self):+ response = "1. [00:00:12,500-00:00:15,000] existing highlight"++ normalized = self._call_with_response(response)++ self.assertEqual(normalized, response)+ self.assertEqual(extract_timestamps(normalized), [[12500, 15000]])++ def test_invalid_ranges_do_not_create_clips(self):+ response = (+ "1. [15-12.5] reversed\n"+ "2. [8-8] empty\n"+ "3. [not-a-time] malformed\n"+ "4. [1.25-2.5] valid"+ )++ normalized = self._call_with_response(response)++ self.assertIn("[15-12.5]", normalized)+ self.assertIn("[8-8]", normalized)+ self.assertIn("[not-a-time]", normalized)+ self.assertEqual(extract_timestamps(normalized), [[1250, 2500]])++@unittest.skipUnless(os.environ.get("TWELVELABS_API_KEY"),"set TWELVELABS_API_KEY to run the live Pegasus smoke test",