Skip to content

Commit

Permalink
点歌模式 支持子文件夹的音频文件搜索,不用拘泥于一级目录了。
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Jul 4, 2023
1 parent fea0b68 commit 200638a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,9 @@ ChatterBot 的核心思想是:基于历史对话数据,使用机器学习和
- 优化说明文档,以更利于用户通过文档解决相关问题、获得帮助。
- 微调了语音合成前的文本切分算法,会控制在10-30字,结合合成时间大概可能也许可以达到不错的连续性。

### 2023-07-05
- 点歌模式 支持子文件夹的音频文件搜索,不用拘泥于一级目录了。

</details>


Expand Down
Binary file removed song/把回忆拼好给你.mp3
Binary file not shown.
Binary file removed song/枝垂樱.mp3
Binary file not shown.
35 changes: 24 additions & 11 deletions utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,41 @@ def __init__(self, config_path):
self.only_play_audio_thread.start()


# 从指定文件夹中搜索指定文件,返回搜索到的文件路径
def search_files(self, root_dir, target_file):
matched_files = []

for root, dirs, files in os.walk(root_dir):
for file in files:
if file == target_file:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, root_dir)
relative_path = relative_path.replace("\\", "/") # 将反斜杠替换为斜杠
matched_files.append(relative_path)

return matched_files


# 获取本地音频文件夹内所有的音频文件名
def get_dir_songs_filename(self):
try:
song_path = self.config.get("choose_song", "song_path")

# 使用 glob 模块匹配指定文件夹下的所有音频文件
audio_files = glob.glob(os.path.join(song_path, '*.mp3')) + \
glob.glob(os.path.join(song_path, '*.wav')) + \
glob.glob(os.path.join(song_path, '*.flac')) + \
glob.glob(os.path.join(song_path, '*.aac')) + \
glob.glob(os.path.join(song_path, '*.ogg')) + \
glob.glob(os.path.join(song_path, '*.m4a'))

# 使用 os.walk 遍历文件夹及其子文件夹
audio_files = []
for root, dirs, files in os.walk(song_path):
for file in files:
if file.endswith(('.mp3', '.wav', '.flac', '.aac', '.ogg', '.m4a')):
audio_files.append(os.path.join(root, file))

# 提取文件名
file_names = [os.path.basename(file) for file in audio_files]
# 提取文件名(去除文件后缀)
# file_names = [os.path.splitext(os.path.basename(file))[0] for file in audio_files]
# 保留子文件夹路径
# file_names = [os.path.relpath(file, song_path) for file in audio_files]

logging.info("获取到本地音频文件名列表如下:")
logging.info(file_names)

return file_names
except Exception as e:
logging.error(e)
Expand Down
8 changes: 7 additions & 1 deletion utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,14 @@ def commit_handle(self, user_name, content):

return

resp_content = self.audio.search_files(self.choose_song_config['song_path'], song_filename)
if resp_content == []:
return
logging.info(f"匹配到的音频原相对路径:{resp_content[0]}")

# 拼接音频文件路径
resp_content = f"{self.choose_song_config['song_path']}/{song_filename}"
resp_content = f"{self.choose_song_config['song_path']}/{resp_content[0]}"
logging.info(f"匹配到的音频路径:{resp_content}")
message = {
"type": "song",
"user_name": user_name,
Expand Down

0 comments on commit 200638a

Please sign in to comment.