feat: Add remove mp3 id3 tag function

Signed-off-by: bowji <bowji@bowji-m-dnpx.dclife.fun>

Signed-off-by: Bowen Ji <jibwf@hotmail.com>
This commit is contained in:
bowji 2024-07-19 10:01:03 +08:00 committed by 涵曦
parent 417a5c924a
commit a28a0267e4
7 changed files with 466 additions and 436 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

1
.gitignore vendored
View File

@ -165,3 +165,4 @@ ffmpeg
music music
test.sh test.sh
conf conf
setting.json

837
pdm.lock

File diff suppressed because it is too large Load Diff

View File

@ -133,6 +133,9 @@ class Config:
group_list: str = os.getenv( group_list: str = os.getenv(
"XIAOMUSIC_GROUP_LIST", "" "XIAOMUSIC_GROUP_LIST", ""
) # did1:group_name,did2:group_name ) # did1:group_name,did2:group_name
remove_id3tag: bool = (
os.getenv("XIAOMUSIC_REMOVE_ID3TAG", "false").lower() == "true"
)
def append_keyword(self, keys, action): def append_keyword(self, keys, action):
for key in keys.split(","): for key in keys.split(","):

View File

@ -95,6 +95,13 @@ var vConsole = new window.VConsole();
<option value="true" selected>true</option> <option value="true" selected>true</option>
<option value="false">false</option> <option value="false">false</option>
</select> </select>
<label for="remove_id3tag">去除MP3 ID3v2和填充减少播放前延迟:</label>
<select id="remove_id3tag">
<option value="true" >true</option>
<option value="false" selected>false</option>
</select>
<label for="httpauth_username">web控制台账户:</label> <label for="httpauth_username">web控制台账户:</label>
<input id="httpauth_username" type="text" value=""></input> <input id="httpauth_username" type="text" value=""></input>
<label for="httpauth_password">web控制台密码:</label> <label for="httpauth_password">web控制台密码:</label>

View File

@ -12,6 +12,10 @@ import tempfile
from collections.abc import AsyncIterator from collections.abc import AsyncIterator
from http.cookies import SimpleCookie from http.cookies import SimpleCookie
from urllib.parse import urlparse from urllib.parse import urlparse
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
import os
import shutil
import aiohttp import aiohttp
import mutagen import mutagen
@ -295,3 +299,40 @@ def parse_str_to_dict(s, d1=",", d2=":"):
result[k] = v result[k] = v
return result return result
# remove mp3 file id3 tag and padding to reduce delay
def no_padding(info):
# this will remove all padding
return 0
def remove_id3_tags(filename,music_path):
file_path = music_path + "/" + filename
audio = MP3(file_path, ID3=ID3)
change = False
# 检查是否存在ID3 v2.3或v2.4标签
if audio.tags and (audio.tags.version == (2, 3, 0) or audio.tags.version == (2, 4, 0)):
# 构造新文件的路径
new_file_path = file_path + ".bak"
# 备份原始文件为新文件
shutil.copy(file_path, new_file_path)
# 删除ID3标签
audio.delete()
# 删除padding
audio.save(padding=no_padding)
# 保存修改后的文件
audio.save()
change = True
return filename,change

View File

@ -40,6 +40,9 @@ from xiaomusic.utils import (
parse_cookie_string, parse_cookie_string,
parse_str_to_dict, parse_str_to_dict,
traverse_music_directory, traverse_music_directory,
remove_id3_tags,
is_mp3
) )
@ -106,6 +109,7 @@ class XiaoMusic:
self.active_cmd = self.config.active_cmd.split(",") self.active_cmd = self.config.active_cmd.split(",")
self.exclude_dirs = set(self.config.exclude_dirs.split(",")) self.exclude_dirs = set(self.config.exclude_dirs.split(","))
self.music_path_depth = self.config.music_path_depth self.music_path_depth = self.config.music_path_depth
self.remove_id3tag = self.config.remove_id3tag
def update_devices(self): def update_devices(self):
self.device_id_did = {} # key 为 device_id self.device_id_did = {} # key 为 device_id
@ -373,6 +377,15 @@ class XiaoMusic:
if filename.startswith("/"): if filename.startswith("/"):
filename = filename[1:] filename = filename[1:]
self.log.debug(f"get_music_url local music. name:{name}, filename:{filename}") self.log.debug(f"get_music_url local music. name:{name}, filename:{filename}")
#移除MP3 ID3 v2标签和填充减少播放前延迟
if self.remove_id3tag and is_mp3(f"{self.music_path}/{filename}"):
self.log.info(f"remove_id3tag:{self.remove_id3tag}, is_mp3:True ")
filename,change = remove_id3_tags(filename,self.music_path)
if change:
self.log.info(f"ID3 tag removed, orgin mp3 file saved as bak")
else:
self.log.info(f"No ID3 tag remove needed")
encoded_name = urllib.parse.quote(filename) encoded_name = urllib.parse.quote(filename)
return f"http://{self.hostname}:{self.public_port}/music/{encoded_name}" return f"http://{self.hostname}:{self.public_port}/music/{encoded_name}"