File tree Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Expand file tree Collapse file tree 1 file changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -49,24 +49,31 @@ def get_opus_data(self, file_path):
49
49
with open (file_path , 'rb' ) as f :
50
50
opus_data = f .read ()
51
51
52
- # 获取音频时长(从API响应中获取)
52
+ # 获取音频时长
53
53
duration = self .get_audio_duration (file_path )
54
54
55
- # 按照每帧大小分割数据
56
- frame_size = 960 # opus标准帧大小
57
- opus_datas = []
55
+ # 初始化Opus编码器(用于验证帧大小)
56
+ encoder = opuslib_next .Encoder (16000 , 1 , opuslib_next .APPLICATION_AUDIO )
57
+
58
+ # 编码参数(与原wav转换保持一致)
59
+ frame_duration = 60 # 60ms per frame
60
+ frame_size = int (16000 * frame_duration / 1000 ) # 960 samples/frame
58
61
59
- # 分帧处理
62
+ opus_datas = []
60
63
current_pos = 0
64
+
61
65
while current_pos < len (opus_data ):
62
- # 读取帧长度(前2个字节)
63
- frame_length = int .from_bytes (opus_data [current_pos :current_pos + 2 ], 'little' )
64
- current_pos += 2
65
-
66
66
# 读取帧数据
67
- frame_data = opus_data [current_pos :current_pos + frame_length ]
68
- opus_datas .append (frame_data )
69
- current_pos += frame_length
67
+ frame_data = opus_data [current_pos :current_pos + frame_size ]
68
+ if len (frame_data ) < frame_size :
69
+ # 如果最后一帧不足,补零
70
+ frame_data += b'\x00 ' * (frame_size - len (frame_data ))
71
+
72
+ # 使用与原方法相同的编码方式
73
+ encoded_data = encoder .encode (frame_data , frame_size )
74
+ opus_datas .append (encoded_data )
75
+
76
+ current_pos += frame_size
70
77
71
78
return opus_datas , duration
72
79
You can’t perform that action at this time.
0 commit comments