Skip to content

Commit

Permalink
优化获取音乐时长接口
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Jun 25, 2024
1 parent 7888ee7 commit 3ef04f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
53 changes: 34 additions & 19 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,44 @@ def downloadfile(url):
return ("Unknow Error", "")


async def get_web_music_duration(url, start=0, end=500):
async def _get_web_music_duration(session, url, start=0, end=500):
duration = 0
headers = {"Range": f"bytes={start}-{end}"}
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
array_buffer = await response.read()
with tempfile.NamedTemporaryFile() as tmp:
async with session.get(url, headers=headers) as response:
array_buffer = await response.read()
with tempfile.NamedTemporaryFile(delete=False) as tmp:
tmp.write(array_buffer)
try:
m = mutagen.File(tmp)
except Exception:
headers = {"Range": f"bytes={0}-{1000}"}
async with session.get(url, headers=headers) as response:
array_buffer = await response.read()
with tempfile.NamedTemporaryFile() as tmp2:
tmp2.write(array_buffer)
m = mutagen.File(tmp2)
return m.info.length
name = tmp.name

try:
m = mutagen.File(name)
duration = m.info.length
except Exception:
pass
os.remove(name)
return duration


async def get_web_music_duration(url, start=0, end=500):
duration = 0
try:
# 设置总超时时间为3秒
timeout = aiohttp.ClientTimeout(total=3)
async with aiohttp.ClientSession(timeout=timeout) as session:
duration = await _get_web_music_duration(session, url, start=0, end=500)
if duration <= 0:
duration = await _get_web_music_duration(session, url, start=0, end=1000)
except Exception:
pass
return duration


# 获取文件播放时长
def get_local_music_duration(filename):
# 获取音频文件对象
audio = mutagen.File(filename)
# 获取播放时长
duration = audio.info.length
duration = 0
try:
m = mutagen.File(filename)
duration = m.info.length
except Exception:
pass
return duration
11 changes: 9 additions & 2 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,20 +498,27 @@ def get_next_music(self):
async def set_next_music_timeout(self):
name = self.cur_music
if self.is_web_radio_music(name):
self.log.info("歌曲电台不会有下一首的定时器")
self.log.info("电台不会有下一首的定时器")
return

if self.is_web_music(name):
url = self._all_music[name]
sec = int(await get_web_music_duration(url))
duration = await get_web_music_duration(url)
sec = int(duration)
self.log.info(f"网络歌曲 {name} : {url} 的时长 {sec} 秒")
else:
filename = self.get_filename(name)
sec = int(get_local_music_duration(filename))
self.log.info(f"本地歌曲 {name} : {filename} 的时长 {sec} 秒")

if self._next_timer:
self._next_timer.cancel()
self.log.info("定时器已取消")

if sec <= 0:
self.log.warning("获取歌曲时长失败,不会开启下一首歌曲的定时器")
return

self._timeout = sec

async def _do_next():
Expand Down

0 comments on commit 3ef04f4

Please sign in to comment.