Skip to content

Commit

Permalink
feat: Add remove mp3 id3 tag function
Browse files Browse the repository at this point in the history
Signed-off-by: bowji <[email protected]>

Signed-off-by: Bowen Ji <[email protected]>
  • Loading branch information
bowji authored and hanxi committed Jul 19, 2024
1 parent 417a5c9 commit a28a026
Show file tree
Hide file tree
Showing 7 changed files with 474 additions and 444 deletions.
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@ ffmpeg
music
test.sh
conf
setting.json
853 changes: 409 additions & 444 deletions pdm.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ class Config:
group_list: str = os.getenv(
"XIAOMUSIC_GROUP_LIST", ""
) # did1:group_name,did2:group_name
remove_id3tag: bool = (
os.getenv("XIAOMUSIC_REMOVE_ID3TAG", "false").lower() == "true"
)

def append_keyword(self, keys, action):
for key in keys.split(","):
Expand Down
7 changes: 7 additions & 0 deletions xiaomusic/static/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ <h2>小爱音箱设置面板
<option value="true" selected>true</option>
<option value="false">false</option>
</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>
<input id="httpauth_username" type="text" value=""></input>
<label for="httpauth_password">web控制台密码:</label>
Expand Down
41 changes: 41 additions & 0 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from collections.abc import AsyncIterator
from http.cookies import SimpleCookie
from urllib.parse import urlparse
from mutagen.id3 import ID3
from mutagen.mp3 import MP3
import os
import shutil

import aiohttp
import mutagen
Expand Down Expand Up @@ -295,3 +299,40 @@ def parse_str_to_dict(s, d1=",", d2=":"):
result[k] = v

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




13 changes: 13 additions & 0 deletions xiaomusic/xiaomusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
parse_cookie_string,
parse_str_to_dict,
traverse_music_directory,
remove_id3_tags,
is_mp3

)


Expand Down Expand Up @@ -106,6 +109,7 @@ def init_config(self):
self.active_cmd = self.config.active_cmd.split(",")
self.exclude_dirs = set(self.config.exclude_dirs.split(","))
self.music_path_depth = self.config.music_path_depth
self.remove_id3tag = self.config.remove_id3tag

def update_devices(self):
self.device_id_did = {} # key 为 device_id
Expand Down Expand Up @@ -373,6 +377,15 @@ def get_music_url(self, name):
if filename.startswith("/"):
filename = filename[1:]
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)
return f"http://{self.hostname}:{self.public_port}/music/{encoded_name}"

Expand Down

0 comments on commit a28a026

Please sign in to comment.