Skip to content

FunASR-powered video transcription, subtitle generation, and LLM-assisted clipping tool with a local Gradio UI.

fix: support Fun-ASR-Nano and SenseVoice result fields

游雁a6d9f9a7637c
diff --git a/funclip/launch.py b/funclip/launch.py
index b224af6..f27f164 100644
--- a/funclip/launch.py
+++ b/funclip/launch.py
@@ -35,7 +35,6 @@ if __name__ == "__main__":
remote_code="./model.py",
vad_model="fsmn-vad",
vad_kwargs={"max_single_segment_time": 30000},
- punc_model="ct-punc",
spk_model="cam++",
hub="hf",
)
@@ -43,7 +42,6 @@ if __name__ == "__main__":
funasr_model = AutoModel(model="iic/SenseVoiceSmall",
vad_model="fsmn-vad",
vad_kwargs={"max_single_segment_time": 30000},
- punc_model="ct-punc",
spk_model="cam++",
)
else:
diff --git a/funclip/utils/subtitle_utils.py b/funclip/utils/subtitle_utils.py
index 2484b52..eaf9c2f 100644
--- a/funclip/utils/subtitle_utils.py
+++ b/funclip/utils/subtitle_utils.py
@@ -57,6 +57,8 @@ class Text2SRT():
def generate_srt(sentence_list):
srt_total = ''
for i, sent in enumerate(sentence_list):
+ if not sent.get('timestamp'):
+ continue
t2s = Text2SRT(sent['text'], sent['timestamp'])
if 'spk' in sent:
srt_total += "{} spk{}\n{}".format(i + 1, sent['spk'], t2s.srt())
diff --git a/funclip/videoclipper.py b/funclip/videoclipper.py
index f94adb9..b12c3a3 100644
--- a/funclip/videoclipper.py
+++ b/funclip/videoclipper.py
@@ -22,6 +22,31 @@ from utils.argparse_tools import ArgumentParser, get_commandline_args
from utils.trans_utils import pre_proc, proc, write_state, load_state, proc_spk, convert_pcm_to_float
+def _is_valid_timestamp(timestamp):
+ return (
+ isinstance(timestamp, list)
+ and len(timestamp) > 0
+ and timestamp[0] is not None
+ and timestamp[-1] is not None
+ )
+
+
+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 []
+
+ sentence_info = []
+ for sent in result.get("sentence_info") or []:
+ if _is_valid_timestamp(sent.get("timestamp")):
+ sentence_info.append(sent)
+
+ if not sentence_info and text and _is_valid_timestamp(timestamp):
+ sentence_info = [{"text": text, "timestamp": timestamp}]
+
+ return text, raw_text, timestamp, sentence_info
+
+
class VideoClipper():
def __init__(self, funasr_model):
logging.warning("Initializing VideoClipper.")
@@ -53,8 +78,9 @@ class VideoClipper():
pred_timestamp=self.lang=='en',
en_post_proc=self.lang=='en',
cache={})
- res_srt = generate_srt(rec_result[0]['sentence_info'])
- state['sd_sentences'] = rec_result[0]['sentence_info']
+ res_text, raw_text, timestamp, sentence_info = _normalize_recognition_result(rec_result[0])
+ res_srt = generate_srt(sentence_info)
+ state['sd_sentences'] = sentence_info
else:
rec_result = self.funasr_model.generate(data,
return_spk_res=False,
@@ -66,11 +92,11 @@ class VideoClipper():
pred_timestamp=self.lang=='en',
en_post_proc=self.lang=='en',
cache={})
- res_srt = generate_srt(rec_result[0]['sentence_info'])
- state['recog_res_raw'] = rec_result[0]['raw_text']
- state['timestamp'] = rec_result[0]['timestamp']
- state['sentences'] = rec_result[0]['sentence_info']
- res_text = rec_result[0]['text']
+ res_text, raw_text, timestamp, sentence_info = _normalize_recognition_result(rec_result[0])
+ res_srt = generate_srt(sentence_info)
+ state['recog_res_raw'] = raw_text
+ state['timestamp'] = timestamp
+ state['sentences'] = sentence_info
return res_text, res_srt, state
def clip(self, dest_text, start_ost, end_ost, state, dest_spk=None, output_dir=None, timestamp_list=None):
diff --git a/tests/test_recognition_result_compat.py b/tests/test_recognition_result_compat.py
new file mode 100644
index 0000000..61c6e34
--- /dev/null
+++ b/tests/test_recognition_result_compat.py
@@ -0,0 +1,66 @@
+import sys
+import unittest
+from pathlib import Path
+
+import numpy as np
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT / "funclip"))
+
+from videoclipper import VideoClipper
+
+
+class DummyASRModel:
+ def __init__(self, result):
+ self.result = result
+
+ def generate(self, *args, **kwargs):
+ return [self.result]
+
+
+class TestRecognitionResultCompat(unittest.TestCase):
+ def _recognize(self, result):
+ clipper = VideoClipper(DummyASRModel(result))
+ clipper.lang = "zh"
+ audio = (16000, np.zeros(16000, dtype=np.float32))
+ return clipper.recog(audio)
+
+ def test_fun_asr_nano_result_without_sentence_info_or_raw_text_still_builds_state(self):
+ text, srt, state = self._recognize(
+ {
+ "key": "utt",
+ "text": "你好世界。",
+ "text_tn": "你好世界",
+ "timestamp": [[0, 500], [500, 1000]],
+ "sentence_info": [],
+ }
+ )
+
+ self.assertEqual(text, "你好世界。")
+ self.assertEqual(state["recog_res_raw"], "你好世界")
+ self.assertEqual(
+ state["sentences"],
+ [{"text": "你好世界。", "timestamp": [[0, 500], [500, 1000]]}],
+ )
+ self.assertIn("00:00:00,000 --> 00:00:01,000", srt)
+
+ def test_none_sentence_timestamp_falls_back_to_top_level_timestamp(self):
+ text, srt, state = self._recognize(
+ {
+ "key": "utt",
+ "text": "测试文本",
+ "raw_text": "测试文本",
+ "timestamp": [[0, 1000]],
+ "sentence_info": [{"text": "测试文本", "timestamp": None}],
+ }
+ )
+
+ self.assertEqual(text, "测试文本")
+ self.assertEqual(
+ state["sentences"], [{"text": "测试文本", "timestamp": [[0, 1000]]}]
+ )
+ self.assertIn("测试文本", srt)
+
+
+if __name__ == "__main__":
+ unittest.main()