Skip to content

Commit

Permalink
feat: 临时文件目录支持配置 #99
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi committed Nov 28, 2024
1 parent def63b2 commit 74837ba
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
11 changes: 8 additions & 3 deletions xiaomusic/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ class Config:
miio_tts_command: str = os.getenv("MIIO_TTS_CMD", "")
cookie: str = ""
verbose: bool = os.getenv("XIAOMUSIC_VERBOSE", "").lower() == "true"
music_path: str = os.getenv(
"XIAOMUSIC_MUSIC_PATH", "music"
) # 只能是music目录下的子目录
music_path: str = os.getenv("XIAOMUSIC_MUSIC_PATH", "music")
temp_path: str = os.getenv("XIAOMUSIC_TEMP_PATH", "music/tmp")
download_path: str = os.getenv("XIAOMUSIC_DOWNLOAD_PATH", "music/download")
conf_path: str = os.getenv("XIAOMUSIC_CONF_PATH", "conf")
cache_dir: str = os.getenv("XIAOMUSIC_CACHE_DIR", "cache")
Expand Down Expand Up @@ -307,6 +306,12 @@ def yt_dlp_cookies_path(self):
cookies_path = os.path.join(self.conf_path, "yt-dlp-cookie.txt")
return cookies_path

@property
def temp_dir(self):
if not os.path.exists(self.temp_path):
os.makedirs(self.temp_path)
return self.temp_path

def get_play_type_tts(self, play_type):
if play_type == PLAY_TYPE_ONE:
return self.play_type_one_tts_msg
Expand Down
4 changes: 3 additions & 1 deletion xiaomusic/static/default/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ <h2>小爱音箱设置面板
<label for="conf_path">配置文件目录:</label>
<input id="conf_path" type="text"></input>


<label for="cache_dir">缓存文件目录:</label>
<input id="cache_dir" type="text"></input>

<label for="temp_path">临时文件目录:</label>
<input id="temp_path" type="text" value="music/tmp"></input>

<label for="ffmpeg_location">ffmpeg路径:</label>
<input id="ffmpeg_location" type="text" value="./ffmpeg/bin"></input>

Expand Down
12 changes: 2 additions & 10 deletions xiaomusic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,6 @@ def no_padding(info):
return 0


def get_temp_dir(music_path: str):
# 指定临时文件的目录为 music_path 目录下的 tmp 文件夹
temp_dir = os.path.join(music_path, "tmp")
if not os.path.exists(temp_dir):
os.makedirs(temp_dir) # 确保目录存在
return temp_dir


def remove_id3_tags(input_file: str, config) -> str:
audio = MP3(input_file, ID3=ID3)

Expand All @@ -426,7 +418,7 @@ def remove_id3_tags(input_file: str, config) -> str:
return None

music_path = config.music_path
temp_dir = get_temp_dir(music_path)
temp_dir = config.temp_dir

# 构造新文件的路径
out_file_name = os.path.splitext(os.path.basename(input_file))[0]
Expand Down Expand Up @@ -459,7 +451,7 @@ def remove_id3_tags(input_file: str, config) -> str:

def convert_file_to_mp3(input_file: str, config) -> str:
music_path = config.music_path
temp_dir = get_temp_dir(music_path)
temp_dir = config.temp_dir

out_file_name = os.path.splitext(os.path.basename(input_file))[0]
out_file_path = os.path.join(temp_dir, f"{out_file_name}.mp3")
Expand Down

0 comments on commit 74837ba

Please sign in to comment.