fix: #114 修复部分 mp3 文件长度识别错误

This commit is contained in:
涵曦 2024-07-14 13:20:38 +00:00
parent d8035f0713
commit a8f88e8bfc

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import copy import copy
import difflib import difflib
import mimetypes
import os import os
import random import random
import re import re
@ -168,6 +169,13 @@ async def downloadfile(url):
return text 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): async def _get_web_music_duration(session, url, start=0, end=500):
duration = 0 duration = 0
headers = {"Range": f"bytes={start}-{end}"} headers = {"Range": f"bytes={start}-{end}"}
@ -178,6 +186,9 @@ async def _get_web_music_duration(session, url, start=0, end=500):
name = tmp.name name = tmp.name
try: try:
if is_mp3(url):
m = mutagen.mp3.MP3(name)
else:
m = mutagen.File(name) m = mutagen.File(name)
duration = m.info.length duration = m.info.length
except Exception: except Exception:
@ -220,6 +231,9 @@ async def get_web_music_duration(url, start=0, end=500):
def get_local_music_duration(filename): def get_local_music_duration(filename):
duration = 0 duration = 0
try: try:
if is_mp3(filename):
m = mutagen.mp3.MP3(filename)
else:
m = mutagen.File(filename) m = mutagen.File(filename)
duration = m.info.length duration = m.info.length
except Exception: except Exception: