Skip to content

Commit f9c7ed9

Browse files
author
邹上豪
committed
使用歌曲嵌入图片
bug:提取时隐藏图片被歌曲本身的封面覆盖,隐藏图片的大小为歌曲和图片的大小之和
0 parents  commit f9c7ed9

6 files changed

Lines changed: 152 additions & 0 deletions

File tree

.idea/PictureInAudio.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Main.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
import tkinter as tk
3+
from tkinter import filedialog, messagebox
4+
5+
def embed_image_in_audio(audio_file, image_file):
6+
"""
7+
将图片嵌入到音频文件中,并生成嵌入后的文件
8+
"""
9+
output_file = os.path.join(os.path.dirname(audio_file), "embedded.mp3")
10+
try:
11+
with open(audio_file, "rb") as audio, open(image_file, "rb") as image:
12+
with open(output_file, "wb") as output:
13+
output.write(audio.read()) # 写入音频数据
14+
output.write(image.read()) # 写入图片数据
15+
messagebox.showinfo("完成", f"图片已嵌入!文件保存为:{output_file}")
16+
except Exception as e:
17+
messagebox.showerror("错误", f"嵌入失败:{str(e)}")
18+
19+
def extract_image_from_file(input_file):
20+
"""
21+
从伪装文件中提取图片,并生成图片文件
22+
"""
23+
output_image_file = os.path.join(os.path.dirname(input_file), "extracted_image.jpg")
24+
try:
25+
with open(input_file, "rb") as file:
26+
data = file.read()
27+
28+
# 搜索 JPG 文件头
29+
jpg_start = data.find(b'\xFF\xD8\xFF') # JPG 的文件头
30+
if jpg_start == -1:
31+
messagebox.showerror("错误", "未找到 JPG 文件头,文件可能不包含图片。")
32+
return
33+
34+
# 提取图片数据
35+
with open(output_image_file, "wb") as image_file:
36+
image_file.write(data[jpg_start:]) # 保存从图片头开始的所有数据
37+
38+
messagebox.showinfo("完成", f"图片已提取!文件保存为:{output_image_file}")
39+
except Exception as e:
40+
messagebox.showerror("错误", f"提取失败:{str(e)}")
41+
42+
def embed_action():
43+
"""选择音频和图片文件并嵌入图片"""
44+
audio_file = filedialog.askopenfilename(title="选择音频文件", filetypes=[("音频文件", "*.mp3")])
45+
if not audio_file:
46+
return
47+
48+
image_file = filedialog.askopenfilename(title="选择图片文件", filetypes=[("图片文件", "*.jpg;*.jpeg;*.png")])
49+
if not image_file:
50+
return
51+
52+
embed_image_in_audio(audio_file, image_file)
53+
54+
def extract_action():
55+
"""选择伪装文件并提取图片"""
56+
input_file = filedialog.askopenfilename(title="选择伪装文件", filetypes=[("音频文件", "*.mp3")])
57+
if not input_file:
58+
return
59+
60+
extract_image_from_file(input_file)
61+
62+
# 创建主界面
63+
root = tk.Tk()
64+
root.title("音频图片嵌入与提取工具")
65+
root.geometry("400x200")
66+
67+
# 嵌入按钮
68+
embed_button = tk.Button(root, text="嵌入图片到音频", command=embed_action, height=2, width=20)
69+
embed_button.pack(pady=20)
70+
71+
# 提取按钮
72+
extract_button = tk.Button(root, text="提取图片从音频", command=extract_action, height=2, width=20)
73+
extract_button.pack(pady=20)
74+
75+
# 运行主循环
76+
root.mainloop()

0 commit comments

Comments
 (0)