From a8f88e8bfc3fa9001b9eb9347793b7d207c8f4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Sun, 14 Jul 2024 13:20:38 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20#114=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E5=88=86=20mp3=20=E6=96=87=E4=BB=B6=E9=95=BF=E5=BA=A6=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/utils.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index b86e68f..f52e450 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -3,6 +3,7 @@ from __future__ import annotations import copy import difflib +import mimetypes import os import random import re @@ -168,6 +169,13 @@ async def downloadfile(url): return text +def is_mp3(url): + mt = mimetypes.guess_type(url) + if mt and mt[0] == "audio/mpeg": + return True + return False + + async def _get_web_music_duration(session, url, start=0, end=500): duration = 0 headers = {"Range": f"bytes={start}-{end}"} @@ -178,7 +186,10 @@ async def _get_web_music_duration(session, url, start=0, end=500): name = tmp.name try: - m = mutagen.File(name) + if is_mp3(url): + m = mutagen.mp3.MP3(name) + else: + m = mutagen.File(name) duration = m.info.length except Exception: pass @@ -220,7 +231,10 @@ async def get_web_music_duration(url, start=0, end=500): def get_local_music_duration(filename): duration = 0 try: - m = mutagen.File(filename) + if is_mp3(filename): + m = mutagen.mp3.MP3(filename) + else: + m = mutagen.File(filename) duration = m.info.length except Exception: pass