优化获取音乐时长接口
This commit is contained in:
parent
7888ee7938
commit
3ef04f4159
@ -166,29 +166,44 @@ def downloadfile(url):
|
|||||||
return ("Unknow Error", "")
|
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}"}
|
headers = {"Range": f"bytes={start}-{end}"}
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
async with session.get(url, headers=headers) as response:
|
async with session.get(url, headers=headers) as response:
|
||||||
array_buffer = await response.read()
|
array_buffer = await response.read()
|
||||||
with tempfile.NamedTemporaryFile() as tmp:
|
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
||||||
tmp.write(array_buffer)
|
tmp.write(array_buffer)
|
||||||
|
name = tmp.name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
m = mutagen.File(tmp)
|
m = mutagen.File(name)
|
||||||
|
duration = m.info.length
|
||||||
except Exception:
|
except Exception:
|
||||||
headers = {"Range": f"bytes={0}-{1000}"}
|
pass
|
||||||
async with session.get(url, headers=headers) as response:
|
os.remove(name)
|
||||||
array_buffer = await response.read()
|
return duration
|
||||||
with tempfile.NamedTemporaryFile() as tmp2:
|
|
||||||
tmp2.write(array_buffer)
|
|
||||||
m = mutagen.File(tmp2)
|
async def get_web_music_duration(url, start=0, end=500):
|
||||||
return m.info.length
|
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):
|
def get_local_music_duration(filename):
|
||||||
# 获取音频文件对象
|
duration = 0
|
||||||
audio = mutagen.File(filename)
|
try:
|
||||||
# 获取播放时长
|
m = mutagen.File(filename)
|
||||||
duration = audio.info.length
|
duration = m.info.length
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
return duration
|
return duration
|
||||||
|
@ -498,20 +498,27 @@ class XiaoMusic:
|
|||||||
async def set_next_music_timeout(self):
|
async def set_next_music_timeout(self):
|
||||||
name = self.cur_music
|
name = self.cur_music
|
||||||
if self.is_web_radio_music(name):
|
if self.is_web_radio_music(name):
|
||||||
self.log.info("歌曲电台不会有下一首的定时器")
|
self.log.info("电台不会有下一首的定时器")
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.is_web_music(name):
|
if self.is_web_music(name):
|
||||||
url = self._all_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} 秒")
|
self.log.info(f"网络歌曲 {name} : {url} 的时长 {sec} 秒")
|
||||||
else:
|
else:
|
||||||
filename = self.get_filename(name)
|
filename = self.get_filename(name)
|
||||||
sec = int(get_local_music_duration(filename))
|
sec = int(get_local_music_duration(filename))
|
||||||
self.log.info(f"本地歌曲 {name} : {filename} 的时长 {sec} 秒")
|
self.log.info(f"本地歌曲 {name} : {filename} 的时长 {sec} 秒")
|
||||||
|
|
||||||
if self._next_timer:
|
if self._next_timer:
|
||||||
self._next_timer.cancel()
|
self._next_timer.cancel()
|
||||||
self.log.info("定时器已取消")
|
self.log.info("定时器已取消")
|
||||||
|
|
||||||
|
if sec <= 0:
|
||||||
|
self.log.warning("获取歌曲时长失败,不会开启下一首歌曲的定时器")
|
||||||
|
return
|
||||||
|
|
||||||
self._timeout = sec
|
self._timeout = sec
|
||||||
|
|
||||||
async def _do_next():
|
async def _do_next():
|
||||||
|
Loading…
Reference in New Issue
Block a user