Skip to content

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

feat:convert pcm to float

zhouzhennenge7d57431e84c
diff --git a/funclip/trans_utils.py b/funclip/trans_utils.py
index dc7438b..b54347b 100644
--- a/funclip/trans_utils.py
+++ b/funclip/trans_utils.py
@@ -79,5 +79,27 @@ def load_state(output_dir):
line = fin.read()
state['sd_sentences'] = eval(line)
return state
-
+
+import numpy as np
+def convert_pcm_to_float(data):
+ if data.dtype == np.float64:
+ return data
+ elif data.dtype == np.float32:
+ return data.astype(np.float64)
+ elif data.dtype == np.int16:
+ bit_depth = 16
+ elif data.dtype == np.int32:
+ bit_depth = 32
+ elif data.dtype == np.int8:
+ bit_depth = 8
+ else:
+ raise ValueError("Unsupported audio data type")
+
+ # Now handle the integer types
+ max_int_value = float(2 ** (bit_depth - 1))
+ if bit_depth == 8:
+ data = data - 128
+ return (data.astype(np.float64) / max_int_value)
+
+
\ No newline at end of file
diff --git a/funclip/videoclipper.py b/funclip/videoclipper.py
index 4c8ea12..4089682 100644
--- a/funclip/videoclipper.py
+++ b/funclip/videoclipper.py
@@ -12,7 +12,7 @@ from moviepy.editor import *
from moviepy.video.tools.subtitles import SubtitlesClip
from subtitle_utils import generate_srt, generate_srt_clip
from argparse_tools import ArgumentParser, get_commandline_args
-from trans_utils import pre_proc, proc, write_state, load_state, proc_spk
+from trans_utils import pre_proc, proc, write_state, load_state, proc_spk, convert_pcm_to_float
class VideoClipper():
@@ -25,6 +25,10 @@ class VideoClipper():
if state is None:
state = {}
sr, data = audio_input
+
+ # Convert to float64 consistently (includes data type checking)
+ data = convert_pcm_to_float(data)
+
# assert sr == 16000, "16kHz sample rate required, {} given.".format(sr)
if sr != 16000: # resample with librosa
data = librosa.resample(data, orig_sr=sr, target_sr=16000)
@@ -32,7 +36,6 @@ class VideoClipper():
logging.warning("Input wav shape: {}, only first channel reserved.").format(data.shape)
data = data[:,0]
state['audio_input'] = (sr, data)
- data = data.astype(np.float64)
if sd_switch == 'yes':
rec_result = self.funasr_model.generate(data, return_raw_text=True, is_final=True, hotword=hotwords)
res_srt = generate_srt(rec_result[0]['sentence_info'])