-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
音乐播放功能添加p3文件格式支持,添加文件目录(含子目录)扫描和文件列表刷新功能 (#157)
Co-authored-by: hrz <[email protected]>
- Loading branch information
1 parent
1c5581b
commit bc53165
Showing
3 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import struct | ||
|
||
def decode_opus_from_file(input_file): | ||
""" | ||
从p3文件中解码 Opus 数据,并返回一个 Opus 数据包的列表以及总时长。 | ||
""" | ||
opus_datas = [] | ||
total_frames = 0 | ||
sample_rate = 16000 # 文件采样率 | ||
frame_duration_ms = 60 # 帧时长 | ||
frame_size = int(sample_rate * frame_duration_ms / 1000) | ||
|
||
with open(input_file, 'rb') as f: | ||
while True: | ||
# 读取头部(4字节):[1字节类型,1字节保留,2字节长度] | ||
header = f.read(4) | ||
if not header: | ||
break | ||
|
||
# 解包头部信息 | ||
_, _, data_len = struct.unpack('>BBH', header) | ||
|
||
# 根据头部指定的长度读取 Opus 数据 | ||
opus_data = f.read(data_len) | ||
if len(opus_data) != data_len: | ||
raise ValueError(f"Data length({len(opus_data)}) mismatch({data_len}) in the file.") | ||
|
||
opus_datas.append(opus_data) | ||
total_frames += 1 | ||
|
||
# 计算总时长 | ||
total_duration = (total_frames * frame_duration_ms) / 1000.0 | ||
return opus_datas, total_duration |