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: clean SenseVoice tags and split long subtitles
游雁4a7ece922faf
diff --git a/funclip/videoclipper.py b/funclip/videoclipper.pyindex b12c3a3..9a4cdb5 100644--- a/funclip/videoclipper.py+++ b/funclip/videoclipper.py@@ -17,11 +17,16 @@ import moviepy.editor as mpyfrom moviepy.video.tools.subtitles import SubtitlesClip, TextClipfrom moviepy.editor import VideoFileClip, concatenate_videoclipsfrom moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip-from utils.subtitle_utils import generate_srt, generate_srt_clip+from utils.subtitle_utils import generate_srt, generate_srt_clip, str2listfrom utils.argparse_tools import ArgumentParser, get_commandline_argsfrom utils.trans_utils import pre_proc, proc, write_state, load_state, proc_spk, convert_pcm_to_float+MAX_SUBTITLE_DURATION_MS = 8000+MAX_SUBTITLE_TOKENS = 30+SENSEVOICE_TAG_RE = re.compile(r"<\|[^|>]+\|>")++def _is_valid_timestamp(timestamp):return (isinstance(timestamp, list)@@ -31,18 +36,75 @@ def _is_valid_timestamp(timestamp):)+def _clean_recognition_text(text):+ if text is None:+ return ""+ text = SENSEVOICE_TAG_RE.sub("", str(text))+ text = re.sub(r"\s+", " ", text).strip()+ return text.strip("“”")+++def _split_long_sentence(sent):+ timestamp = sent.get("timestamp")+ if not _is_valid_timestamp(timestamp):+ return []++ cleaned_text = _clean_recognition_text(sent.get("text"))+ normalized = dict(sent)+ normalized["text"] = cleaned_text+ normalized["timestamp"] = timestamp++ tokens = str2list(cleaned_text)+ if len(timestamp) <= 1 or len(tokens) != len(timestamp):+ return [normalized]++ chunks = []+ start = 0+ for idx in range(len(tokens)):+ duration = timestamp[idx][1] - timestamp[start][0]+ token_count = idx - start + 1+ should_split = (+ idx > start+ and (+ duration >= MAX_SUBTITLE_DURATION_MS+ or token_count >= MAX_SUBTITLE_TOKENS+ )+ )+ if should_split:+ chunk = dict(normalized)+ chunk["text"] = tokens[start : idx + 1]+ chunk["timestamp"] = timestamp[start : idx + 1]+ chunks.append(chunk)+ start = idx + 1++ if not chunks:+ return [normalized]++ if start < len(tokens):+ chunk = dict(normalized)+ chunk["text"] = tokens[start:]+ chunk["timestamp"] = timestamp[start:]+ chunks.append(chunk)++ return chunks++def _normalize_recognition_result(result):- text = result.get("text") or result.get("text_tn") or result.get("raw_text") or ""- raw_text = result.get("raw_text") or result.get("text_tn") or text- timestamp = result.get("timestamp") or []+ text = _clean_recognition_text(+ result.get("text") or result.get("text_tn") or result.get("raw_text") or ""+ )+ raw_text = _clean_recognition_text(+ result.get("raw_text") or result.get("text_tn") or text+ )+ timestamp = result.get("timestamp") or result.get("timestamps") or []sentence_info = []for sent in result.get("sentence_info") or []:if _is_valid_timestamp(sent.get("timestamp")):- sentence_info.append(sent)+ sentence_info.extend(_split_long_sentence(sent))if not sentence_info and text and _is_valid_timestamp(timestamp):- sentence_info = [{"text": text, "timestamp": timestamp}]+ sentence_info = _split_long_sentence({"text": text, "timestamp": timestamp})return text, raw_text, timestamp, sentence_infodiff --git a/tests/test_recognition_result_compat.py b/tests/test_recognition_result_compat.pyindex 61c6e34..8880355 100644--- a/tests/test_recognition_result_compat.py+++ b/tests/test_recognition_result_compat.py@@ -61,6 +61,43 @@ class TestRecognitionResultCompat(unittest.TestCase):)self.assertIn("测试文本", srt)+ def test_sensevoice_rich_tags_are_removed_from_text_and_srt(self):+ text, srt, state = self._recognize(+ {+ "key": "utt",+ "text": "<|zh|><|NEUTRAL|><|Speech|><|woitn|>你好世界",+ "timestamp": [[0, 300], [300, 600], [600, 900], [900, 1200]],+ "sentence_info": [+ {+ "text": "<|zh|><|NEUTRAL|><|Speech|><|woitn|>你好世界",+ "timestamp": [[0, 300], [300, 600], [600, 900], [900, 1200]],+ }+ ],+ }+ )++ self.assertEqual(text, "你好世界")+ self.assertEqual(state["sentences"][0]["text"], "你好世界")+ self.assertNotIn("<|zh|>", state["recog_res_raw"])+ self.assertNotIn("<|zh|>", srt)++ def test_long_token_level_sentence_is_split_into_subtitle_chunks(self):+ long_text = "一二三四五六七八九十甲乙"+ timestamps = [[i * 1000, (i + 1) * 1000] for i in range(len(long_text))]++ _, srt, state = self._recognize(+ {+ "key": "utt",+ "text": long_text,+ "timestamp": timestamps,+ "sentence_info": [{"text": long_text, "timestamp": timestamps}],+ }+ )++ self.assertGreater(len(state["sentences"]), 1)+ self.assertIn("00:00:00,000 --> 00:00:08,000", srt)+ self.assertIn("00:00:08,000 --> 00:00:12,000", srt)+if __name__ == "__main__":unittest.main()