-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathweb.py
111 lines (88 loc) · 3.86 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import streamlit as st
from ncmdump import dump
import os
import shutil
from datetime import datetime, timedelta
import traceback
TEMP = "./temp"
MAX_TEMP_AGE = timedelta(hours=24) # 临时文件最大保存时间
def get_temp_file_path(input_file):
"""获取临时文件路径"""
base_name = os.path.splitext(os.path.basename(input_file.name))[0]
return os.path.join(TEMP, f"{base_name}.ncm")
def cleanup_temp_files():
"""清理超过24小时的临时文件"""
if not os.path.exists(TEMP):
return
current_time = datetime.now()
for filename in os.listdir(TEMP):
filepath = os.path.join(TEMP, filename)
file_modified = datetime.fromtimestamp(os.path.getmtime(filepath))
if current_time - file_modified > MAX_TEMP_AGE:
try:
os.remove(filepath)
except Exception:
pass
def web_page():
#打开web页面时直接清理过时文件
cleanup_temp_files()
st.success("已清理过时文件")
st.title("NCM转换器")
input_files = st.file_uploader("上传音频:", type=["ncm"], accept_multiple_files=True)
if not input_files:
return
# 处理文件上传
for input_file in input_files:
try:
input_path = get_temp_file_path(input_file)
# 保存输入文件
if not os.path.exists(input_path):
with open(input_path, "wb") as f:
f.write(input_file.read())
else:
st.info(f"文件 {input_file.name} 已加载")
except Exception as e:
st.error(f"处理文件 {input_file.name} 时发生错误: {str(e)}")
continue
st.markdown("------------")
if st.button("开始转换"):
total_files = len(input_files)
progress_bar = st.progress(0)
for idx, input_file in enumerate(input_files):
try:
input_path = get_temp_file_path(input_file)
if not os.path.exists(input_path):
st.warning(f"找不到输入文件: {input_file.name}")
continue
with st.spinner(f'正在转换 {input_file.name} ({idx + 1}/{total_files})'):
try:
output_path = dump(input_path)
#得到输出文件名及文件扩展名
file_name = os.path.basename(output_path)
extension = os.path.splitext(output_path)[1][1:]
except Exception as e:
st.error(f"转换失败: {str(e)}")
continue
if os.path.exists(output_path):
# 显示音频预览
st.audio(output_path, format=f'audio/{extension}', start_time=0)
# 提供下载按钮
with open(output_path, "rb") as audio_file:
st.download_button(
label = f"点击下载 {file_name}",
data = audio_file.read(),
file_name = file_name,
mime = f"audio/{extension}"
)
else:
st.error(f"转换后的文件未生成: {file_name}")
progress_bar.progress((idx + 1) / total_files)
except Exception as e:
st.error(f"处理文件 {input_file.name} 时发生错误: {str(e)}")
traceback.print_exc()
continue
if __name__ == "__main__":
if not os.path.exists(TEMP):
os.makedirs(TEMP)
cleanup_temp_files() # 启动时清理旧的临时文件
web_page()