Skip to content

Commit

Permalink
fix: 修复部分文件获取不到播放时长问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Aug 1, 2024
1 parent cf01039 commit 444e697
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
39 changes: 26 additions & 13 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions test/test_music_duration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import math

from xiaomusic.const import (
SUPPORT_MUSIC_TYPE,
)
from xiaomusic.utils import (
get_local_music_duration,
traverse_music_directory,
)


async def test_one_music(filename):
# 获取播放时长
duration = await get_local_music_duration(filename)
sec = math.ceil(duration)
print(f"本地歌曲 : {filename} 的时长 {duration} {sec} 秒")


async def main(directory):
# 获取所有歌曲文件
local_musics = traverse_music_directory(directory, 10, [], SUPPORT_MUSIC_TYPE)
print(local_musics)
for _, files in local_musics.items():
for file in files:
await test_one_music(file)


if __name__ == "__main__":
import asyncio

directory = "./music" # 替换为你的音乐目录路径
asyncio.run(main(directory))
9 changes: 3 additions & 6 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def _append_files_result(result, root, joinpath, files, support_extension):
result[dir_name].append(os.path.join(joinpath, file))


def traverse_music_directory(
directory, depth=10, exclude_dirs=None, support_extension=None
):
def traverse_music_directory(directory, depth, exclude_dirs, support_extension):
result = {}
for root, dirs, files in os.walk(directory, followlinks=True):
# 忽略排除的目录
Expand Down Expand Up @@ -247,10 +245,9 @@ async def get_local_music_duration(filename):
m = await loop.run_in_executor(None, mutagen.mp3.MP3, filename)
else:
m = await loop.run_in_executor(None, mutagen.File, filename)
if m and m.info:
duration = m.info.length
duration = m.info.length
except Exception as e:
logging.error(f"Error getting local music duration: {e}")
logging.error(f"Error getting local music {filename} duration: {e}")
return duration


Expand Down

0 comments on commit 444e697

Please sign in to comment.