听歌小妙招
平时刷b站的时候会发现一些好听的歌曲视频,但是用b站听歌就是各种不爽,毕竟是播放视频,不仅耗费流量还不好设置循环播放。所以就想把b站里的音乐视频的音频提取出来然后导入到网易云的本地的音乐列表里面,这样就可以用网易云愉快地听歌啦。实现这个需求主要有两个过程,一是将b站音乐视频的音频提取出来,二是给提取出来的mp3音乐文件添加音乐专辑封面(没有封面的歌听起来好像少了点感觉,可以是任意一张自己喜欢的图片)。
音频提取
这个过程用到的需要用到moviepy
包库
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ moviepy
代码如下:
import os.path
from moviepy.editor import VideoFileClip
def extract_audio(video_path, dest_folder):
video_name = os.path.basename(video_path)
clip = VideoFileClip(video_path)
audio = clip.audio
dest_file_name = os.path.splitext(video_name)[0]
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
audio.write_audiofile(os.path.join(dest_folder, dest_file_name + '.mp3'))
print("保存完毕")
# 调用方法示例
video_folder = "./music_videos"
video_name = "永遇乐.mp4"
video_path = os.path.join(video_folder, video_name)
dest_folder = './music_without_cover'
extract_audio(video_path, dest_folder)
添加专辑封面
这个过程我使用mutagen
模块实现
import os.path
import shutil
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
def add_cover_image_to_mp3(music_path, cover_image_path, dest_folder1, dest_folder2):
# 加载MP3文件
audio = MP3(music_path, ID3=ID3)
# 设置封面图片
with open(cover_image_path, 'rb') as f:
audio.tags.add(
APIC(
encoding=3, # utf-8
mime='image/jpeg',
type=3, # cover image
desc=u'Cover',
data=f.read()
)
)
# 保存带有封面的音频文件
audio.save(music_path, v2_version=3)
source_folder = os.path.dirname(music_path)
shutil.copy(os.path.join(source_folder, os.path.basename(music_path)),
os.path.join(dest_folder1, os.path.basename(music_path)))
shutil.copy(os.path.join(source_folder, os.path.basename(music_path)),
os.path.join(dest_folder2, os.path.basename(music_path)))
print("带封面的mp3文件已经复制到了下载的本地音乐的目录中")
if os.path.exists(music_path):
os.remove(music_path)
# 使用方法
music_folder = "./music_without_cover"
music_name = '永遇乐.mp3'
music_path = os.path.join(music_folder, music_name)
cover_folder = "./cover_images"
cover_image_name = f"{music_name.split('.')[0]}.jpg"
cover_image_path = os.path.join(cover_folder, cover_image_name)
dest_folder1 = "带专辑封面的mp3文件"
dest_folder2 = "E:/下载的本地音乐"
add_cover_image_to_mp3(music_path, cover_image_path, dest_folder1, dest_folder2)
运行结果
import os.path
from moviepy.editor import VideoFileClip
import shutil
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error
def extract_audio(video_path, dest_folder):
video_name = os.path.basename(video_path)
clip = VideoFileClip(video_path)
audio = clip.audio
dest_file_name = os.path.splitext(video_name)[0]
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
audio.write_audiofile(os.path.join(dest_folder, dest_file_name + '.mp3'))
print("音频提取保存完毕")
# 返回生成的文件的路径
return os.path.join(dest_folder, dest_file_name + '.mp3')
def add_cover_image_to_mp3(music_path, cover_image_path, dest_folder1, dest_folder2):
# 加载MP3文件
audio = MP3(music_path, ID3=ID3)
# 设置封面图片
with open(cover_image_path, 'rb') as f:
audio.tags.add(
APIC(
encoding=3, # utf-8
mime='image/jpeg',
type=3, # cover image
desc=u'Cover',
data=f.read()
)
)
# 保存带有封面的音频文件
audio.save(music_path, v2_version=3)
source_folder = os.path.dirname(music_path)
shutil.copy(os.path.join(source_folder, os.path.basename(music_path)),
os.path.join(dest_folder1, os.path.basename(music_path)))
shutil.copy(os.path.join(source_folder, os.path.basename(music_path)),
os.path.join(dest_folder2, os.path.basename(music_path)))
print("带封面的mp3文件已经复制到了下载的本地音乐的目录中")
if os.path.exists(music_path):
os.remove(music_path)
def main():
# 调用方法示例
video_folder = "./music_videos"
video_name = "金铜仙人辞汉歌.mp4"
video_path = os.path.join(video_folder, video_name)
dest_folder = './music_without_cover'
# 调用第一个方法
audio_file_path = extract_audio(video_path, dest_folder)
print(audio_file_path)
#
# 使用方法
music_name = os.path.basename(audio_file_path)
cover_folder = "./cover_images"
cover_image_name = f"{music_name.split('.')[0]}.jpg"
cover_image_path = os.path.join(cover_folder, cover_image_name)
dest_folder1 = "带专辑封面的mp3文件"
dest_folder2 = "E:/下载的本地音乐"
# 调用第二个方法,并传入第一个方法返回的结果
add_cover_image_to_mp3(audio_file_path, cover_image_path, dest_folder1, dest_folder2)
# 运行主程序
if __name__ == "__main__":
main()
代码运行后得到的添加了音乐封面的mp3文件输出到了我预设的目标文件夹中(我这里是E:\下载的本地音乐
),然后在网易云的本地音乐中选择该文件目录即可将其中的音乐文件读取到本地音乐中。
制作添加歌词
也可以自己制作歌词文件,然后和音乐mp3文件放在同一个文件夹下,保持相同的文件名,文件扩展名为.lrc
。这样咱们的本地音乐就有歌词了。
歌词的文件格式如下:
每一行的左边是时间戳,右边是歌词。