@@ -22,90 +22,88 @@ def generate_empty_audio(output_file, file_name):
2222 messagebox .showerror ("错误" , f"生成空音频失败:{ str (e )} " )
2323 return False
2424
25- def embed_image_in_audio ( image_file ):
25+ def embed_file_in_audio ( file_path ):
2626 """
27- 将图片嵌入到生成的无声音频文件中
27+ 将任意文件嵌入到生成的无声音频文件中
2828 """
29- file_name = os .path .splitext ( os . path . basename (image_file ))[ 0 ]
30- output_audio_file = os .path .join (os .path .dirname (image_file ), "temp_silent.mp3" )
31- if not generate_empty_audio (output_audio_file , file_name ):
29+ file_name_with_extension = os .path .basename (file_path ) # 完整文件名
30+ output_audio_file = os .path .join (os .path .dirname (file_path ), "temp_silent.mp3" )
31+ if not generate_empty_audio (output_audio_file , file_name_with_extension ):
3232 return
3333
34- output_file = os .path .join (os .path .dirname (image_file ), file_name + "(embedded).mp3" )
34+ output_file = os .path .join (os .path .dirname (file_path ), os . path . splitext ( file_name_with_extension )[ 0 ] + " (embedded).mp3" )
3535 try :
36- with open (output_audio_file , "rb" ) as audio , open (image_file , "rb" ) as image :
36+ with open (output_audio_file , "rb" ) as audio , open (file_path , "rb" ) as file :
3737 with open (output_file , "wb" ) as output :
3838 output .write (audio .read ()) # 写入音频数据
39- output .write (image .read ()) # 写入图片数据
39+ output .write (b"<FILE>" + file_name_with_extension .encode ('utf-8' ) + b"</FILE>" ) # 写入文件名,使用标签分隔
40+ output .write (file .read ()) # 写入文件数据
4041 os .remove (output_audio_file ) # 删除临时音频文件
41- messagebox .showinfo ("完成" , f"图片已嵌入 !文件保存为:{ output_file } " )
42+ messagebox .showinfo ("完成" , f"文件已嵌入 !文件保存为:{ output_file } " )
4243 except Exception as e :
4344 messagebox .showerror ("错误" , f"嵌入失败:{ str (e )} " )
4445
45- def extract_image_from_file (input_file ):
46+ def extract_file_from_audio (input_file ):
4647 """
47- 从伪装文件中提取图片,并生成图片文件
48+ 从伪装文件中提取嵌入的文件,并恢复其原始文件名
4849 """
4950 try :
5051 with open (input_file , "rb" ) as file :
5152 data = file .read ()
5253
53- # 检测图片格式
54- formats = {
55- b'\xFF \xD8 \xFF ' : ".jpg" , # JPG 文件头
56- b'\x89 PNG' : ".png" # PNG 文件头
57- }
58-
59- start_index = - 1
60- file_extension = None
61- for header , ext in formats .items ():
62- index = data .find (header )
63- if index != - 1 :
64- start_index = index
65- file_extension = ext
66- break
67-
68- if start_index == - 1 :
69- messagebox .showerror ("错误" , "未找到支持的图片文件头,文件可能不包含图片。" )
54+ # 查找文件名标签
55+ start_tag = b"<FILE>"
56+ end_tag = b"</FILE>"
57+ start_index = data .find (start_tag ) + len (start_tag )
58+ end_index = data .find (end_tag )
59+
60+ if start_index == - 1 or end_index == - 1 :
61+ messagebox .showerror ("错误" , "未找到嵌入文件的标识符。" )
7062 return
7163
72- # 提取图片数据
73- embedded_file_name = os .path .splitext (os .path .basename (input_file ))[0 ].replace ("(embedded)" , "" )
74- output_image_file = os .path .join (os .path .dirname (input_file ), f"{ embedded_file_name } { file_extension } " )
75- with open (output_image_file , "wb" ) as image_file :
76- image_file .write (data [start_index :]) # 保存从图片头开始的所有数据
64+ # 提取文件名
65+ file_name = data [start_index :end_index ].decode ('utf-8' , errors = 'ignore' ).strip ()
66+
67+ # 如果文件名为空,提供默认名称
68+ if not file_name :
69+ file_name = "extracted_file"
70+
71+ # 提取嵌入文件数据
72+ output_file = os .path .join (os .path .dirname (input_file ), file_name )
73+ with open (output_file , "wb" ) as extracted_file :
74+ extracted_file .write (data [end_index + len (end_tag ):]) # 保存嵌入的文件数据
7775
78- messagebox .showinfo ("完成" , f"图片已提取 !文件保存为:{ output_image_file } " )
76+ messagebox .showinfo ("完成" , f"文件已提取 !文件保存为:{ output_file } " )
7977 except Exception as e :
8078 messagebox .showerror ("错误" , f"提取失败:{ str (e )} " )
8179
8280def embed_action ():
83- """选择图片文件并嵌入到生成的无声音频中 """
84- image_file = filedialog .askopenfilename (title = "选择图片文件 " , filetypes = [("图片文件 " , "*.jpg;*.jpeg;*.png " )])
85- if not image_file :
81+ """选择文件并嵌入到生成的无声音频中 """
82+ file_path = filedialog .askopenfilename (title = "选择文件 " , filetypes = [("所有文件 " , "*.* " )])
83+ if not file_path :
8684 return
8785
88- embed_image_in_audio ( image_file )
86+ embed_file_in_audio ( file_path )
8987
9088def extract_action ():
91- """选择伪装文件并提取图片 """
89+ """选择伪装文件并提取嵌入的文件 """
9290 input_file = filedialog .askopenfilename (title = "选择伪装文件" , filetypes = [("音频文件" , "*.mp3" )])
9391 if not input_file :
9492 return
9593
96- extract_image_from_file (input_file )
94+ extract_file_from_audio (input_file )
9795
9896# 创建主界面
9997root = tk .Tk ()
100- root .title ("音频图片嵌入与提取工具 " )
98+ root .title ("文件嵌入与提取工具 " )
10199root .geometry ("400x200" )
102100
103101# 嵌入按钮
104- embed_button = tk .Button (root , text = "嵌入图片到音频 " , command = embed_action , height = 2 , width = 20 )
102+ embed_button = tk .Button (root , text = "嵌入文件到音频 " , command = embed_action , height = 2 , width = 20 )
105103embed_button .pack (pady = 20 )
106104
107105# 提取按钮
108- extract_button = tk .Button (root , text = "提取图片从音频 " , command = extract_action , height = 2 , width = 20 )
106+ extract_button = tk .Button (root , text = "提取文件从音频 " , command = extract_action , height = 2 , width = 20 )
109107extract_button .pack (pady = 20 )
110108
111109# 运行主循环
0 commit comments