import
os
import
tkinter as tk
from
tkinter
import
filedialog
from
tkinter.scrolledtext
import
ScrolledText
import
threading
def
getAllFiles(path):
return
[os.path.join(path, f)
for
f
in
os.listdir(path)
if
os.path.isfile(os.path.join(path, f))]
def
isUcExtension(
file
):
return
file
.endswith(
'.uc!'
)
def
ucToFlac(
file
, output_dir):
with
open
(
file
,
'rb'
) as fSource:
content
=
bytearray(fSource.read())
for
index
in
range
(
len
(content)):
content[index] ^
=
0xA3
output_file_base
=
file
[:
-
4
]
if
output_file_base.endswith(
'.mp3'
):
output_file_name
=
output_file_base
else
:
output_file_name
=
output_file_base
+
'.mp3'
output_file
=
os.path.join(output_dir, os.path.basename(output_file_name))
with
open
(output_file,
'wb'
) as fOut:
fOut.write(content)
return
output_file
def
convertFilesThread(input_dir, output_dir):
if
not
os.path.isdir(input_dir)
or
not
os.path.isdir(output_dir):
logMessage(
"错误: 输入或输出目录无效\n"
)
return
files
=
getAllFiles(input_dir)
for
file
in
files:
if
isUcExtension(
file
):
output_file
=
ucToFlac(
file
, output_dir)
logMessage(os.path.basename(output_file)
+
' 转换成功\n'
)
def
convertFiles():
input_dir
=
input_dir_entry.get()
output_dir
=
output_dir_entry.get()
threading.Thread(target
=
convertFilesThread, args
=
(input_dir, output_dir)).start()
def
selectInputDir():
dirname
=
filedialog.askdirectory()
if
dirname:
input_dir_entry.delete(
0
, tk.END)
input_dir_entry.insert(
0
, dirname)
def
selectOutputDir():
dirname
=
filedialog.askdirectory()
if
dirname:
output_dir_entry.delete(
0
, tk.END)
output_dir_entry.insert(
0
, dirname)
def
logMessage(message):
if
log_text:
log_text.config(state
=
tk.NORMAL)
log_text.insert(tk.END, message)
log_text.config(state
=
tk.DISABLED)
log_text.see(tk.END)
root
=
tk.Tk()
root.title(
"UC文件转MP3工具"
)
tk.Label(root, text
=
"输入目录:"
).grid(row
=
0
, column
=
0
, sticky
=
'e'
)
input_dir_entry
=
tk.Entry(root, width
=
50
)
input_dir_entry.grid(row
=
0
, column
=
1
)
tk.Button(root, text
=
"选择"
, command
=
selectInputDir).grid(row
=
0
, column
=
2
)
tk.Label(root, text
=
"输出目录:"
).grid(row
=
1
, column
=
0
, sticky
=
'e'
)
output_dir_entry
=
tk.Entry(root, width
=
50
)
output_dir_entry.grid(row
=
1
, column
=
1
)
tk.Button(root, text
=
"选择"
, command
=
selectOutputDir).grid(row
=
1
, column
=
2
)
tk.Button(root, text
=
"开始转换"
, command
=
convertFiles).grid(row
=
2
, column
=
0
, columnspan
=
3
)
log_text
=
ScrolledText(root, height
=
10
)
log_text.grid(row
=
3
, column
=
0
, columnspan
=
3
, sticky
=
'nsew'
)
root.grid_rowconfigure(
3
, weight
=
1
)
root.grid_columnconfigure(
1
, weight
=
1
)
root.mainloop()