📅  最后修改于: 2023-12-03 15:07:11.545000             🧑  作者: Mango
在这个项目中,我们将使用 Python 和一个 GUI 库(如 Tkinter 或 PyQt)来创建一个图形用户界面,允许用户选择一个歌曲,然后从中提取歌词。我们将使用一些常见的 Python 库和技术来解决这个问题,包括:
requests
库用于从互联网上下载歌曲文件beautifulsoup4
库用于从 HTML 中抽取歌词文本PIL
库用于显示歌曲的专辑艺术使用您选择的 GUI 库创建一个窗口,添加一个标签、文件选择器和一个按钮,如下所示:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
# 添加标签
label = tk.Label(root, text="选择歌曲文件:")
label.pack()
# 添加文件选择器
file_selector = filedialog.askopenfilename()
file_selector.pack()
# 添加按钮
button = tk.Button(root, text="提取歌词")
button.pack()
root.mainloop()
在按钮单击事件处理程序中,使用 requests
库从互联网下载所选文件:
import requests
def extract_lyrics():
# 获取所选文件路径
file_path = file_selector.get()
# 下载歌曲
response = requests.get(file_path)
# 保存文件
with open("song.mp3", "wb") as f:
f.write(response.content)
使用 beautifulsoup4
库从在网上搜索到的一些网站上获取歌曲文本:
from bs4 import BeautifulSoup
def extract_lyrics():
# 获取所选文件路径
file_path = file_selector.get()
# 下载歌曲
response = requests.get(file_path)
# 保存文件
with open("song.mp3", "wb") as f:
f.write(response.content)
# 获取歌词
lyrics_url = "https://www.azlyrics.com/lyrics/song.html"
lyrics_response = requests.get(lyrics_url)
soup = BeautifulSoup(lyrics_response.text, "html.parser")
lyrics = soup.find("div", {"class": "col-xs-12 col-lg-8 text-center"}).text
# 显示歌词
print(lyrics)
使用 PIL
库从歌曲文件中提取专辑艺术,并将其显示在 GUI 窗口中:
from PIL import ImageTk, Image
def extract_lyrics():
# 获取所选文件路径
file_path = file_selector.get()
# 下载歌曲
response = requests.get(file_path)
# 保存文件
with open("song.mp3", "wb") as f:
f.write(response.content)
# 获取歌词
lyrics_url = "https://www.azlyrics.com/lyrics/song.html"
lyrics_response = requests.get(lyrics_url)
soup = BeautifulSoup(lyrics_response.text, "html.parser")
lyrics = soup.find("div", {"class": "col-xs-12 col-lg-8 text-center"}).text
# 提取专辑艺术
audio = MP3("song.mp3")
tags = audio.tags
albumart = tags.get("APIC:").data
with open("albumart.jpg", "wb") as f:
f.write(albumart)
# 显示专辑艺术
image = Image.open("albumart.jpg")
image = image.resize((200, 200), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
image_label = tk.Label(root, image=image)
image_label.pack()
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
from bs4 import BeautifulSoup
from mutagen.mp3 import MP3
import requests
root = tk.Tk()
# 添加标签
label = tk.Label(root, text="选择歌曲文件:")
label.pack()
# 添加文件选择器
file_selector = filedialog.askopenfilename()
file_selector.pack()
# 添加按钮
button = tk.Button(root, text="提取歌词", command=extract_lyrics)
button.pack()
root.mainloop()
def extract_lyrics():
# 获取所选文件路径
file_path = file_selector.get()
# 下载歌曲
response = requests.get(file_path)
# 保存文件
with open("song.mp3", "wb") as f:
f.write(response.content)
# 获取歌词
lyrics_url = "https://www.azlyrics.com/lyrics/song.html"
lyrics_response = requests.get(lyrics_url)
soup = BeautifulSoup(lyrics_response.text, "html.parser")
lyrics = soup.find("div", {"class": "col-xs-12 col-lg-8 text-center"}).text
# 提取专辑艺术
audio = MP3("song.mp3")
tags = audio.tags
albumart = tags.get("APIC:").data
with open("albumart.jpg", "wb") as f:
f.write(albumart)
# 显示专辑艺术
image = Image.open("albumart.jpg")
image = image.resize((200, 200), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
image_label = tk.Label(root, image=image)
image_label.pack()
# 显示歌词
lyrics_label = tk.Label(root, text=lyrics)
lyrics_label.pack()
使用这个简单的项目,您可以创建一个 GUI 应用程序来提取歌曲中的歌词并显示专辑艺术。这里只是一个非常基本的示例,您可以根据需要进行修改和扩展。