前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具备任何商业用途,版权归原做者全部,若有问题请及时联系咱们以做处理。
做者:GeneralMonkeypython
Python解密网易云音乐缓存文件获取MP3
一、安装mutagen
二、获取缓存文件目录文件
三、缓存文件解码
四、获取MP3歌曲信息
五、循环进行保存文件到指定目录
所有源码
一、安装mutagen
首先进行安装mutagen,直接命令行安装,前提条件,你须要先安装pip工具,若是你解密或者这个工具不懂,或者你也刚学python不久,建议去小编的Python交流.裙 :一久武其而而流一思(数字的谐音)转换下能够找到了,里面有最新Python教程项目可拿,多跟里面的人交流,进步更快哦!
pip install mutagen缓存
二、获取缓存文件目录文件
网易云音乐客户端中设置,找到你的音乐缓存目录,里面有一个.uc文件,经过比较,发现uc文件和mp3文件大小基本一致,网上查询得知每字节和0xa3作异或便可, ^0xa3,咱们将缓存先进行保存到一个列表中。网络
#获取uc文件列表
def listfiles(path):
for root, dirs, files in os.walk(path):
for file in files:
if os.path.splitext(file)[1] == '.uc':
fList.append(file)
print(fList)
三、缓存文件解码
拿到缓存文件的列表以后,咱们开始进行异或运算,而后保存成mp3文件,期间须要进行获取mp3文件信息,因为文件中有可能获取不到歌曲的信息,咱们作了一个操做,凡是获取的不到歌曲名称信息的文件,一概保存"未知歌曲"+序号的方式做为新文件名称,固然你也能够自定义。app
#音乐文件解码
def decodefile(filepath, newfilepath,index):
with open(filepath,'rb') as infile:
bytearr = bytearray(infile.read())
with open(newfilepath, 'wb') as outfile:
for i,j in enumerate(bytearr):
bytearr[i] = j ^ 0xa3
outfile.write(bytearr)
outfile.close()
name = wirtefilename(newfilepath)
if name is not None:
try:
os.rename(newfilepath, os.path.join(SavePath, name)+'.mp3')
except FileExistsError as e:
print("file exist")
except OSError as e:
name = "未知曲目" + str(index)
os.rename(newfilepath, os.path.join(SavePath, name) + '.mp3')
finally:
...
else:
name = "未知曲目"+str(index)
try:
os.rename(newfilepath, os.path.join(SavePath, name)+'.mp3')
except FileExistsError as e:
print("file exist")
finally:
四、获取MP3歌曲信息
利用mutagen模块中的MP3进行获取歌曲信息,部分歌曲可能获取不到信息。工具
# 获取mp3歌曲名称
def wirtefilename(musicpath):
#print(musicpath)
fileinfo = MP3(musicpath, ID3 = EasyID3)
print(fileinfo)
if fileinfo != {}:
print(fileinfo['title'][0])
name = str(fileinfo['title'][0])
五、循环进行保存文件到指定目录
最后咱们逐文件进行保存便可。学习
if __name__ == "__main__":
listfiles(CachePath)
index = 0
print(len(fList))
for i in fList:
decodefile(os.path.join(CachePath, i),os.path.join(SavePath, "Temp.mp3"), index)
index = index+1
所有源码
import os
from mutagen.mp3 import MP3
from mutagen.easyid3 import EasyID3命令行
CachePath = "F:/缓存Temp/Cache"
SavePath = "C:/Users/Administrator/Desktop/Save"
fList = []code
#获取uc文件列表
def listfiles(path):
for root, dirs, files in os.walk(path):
for file in files:
if os.path.splitext(file)[1] == '.uc':
fList.append(file)
print(fList)blog
#音乐文件解码
def decodefile(filepath, newfilepath,index):
with open(filepath,'rb') as infile:
bytearr = bytearray(infile.read())
with open(newfilepath, 'wb') as outfile:
for i,j in enumerate(bytearr):
bytearr[i] = j ^ 0xa3
outfile.write(bytearr)
outfile.close()
name = wirtefilename(newfilepath)
if name is not None:
try:
os.rename(newfilepath, os.path.join(SavePath, name)+'.mp3')
except FileExistsError as e:
print("file exist")
except OSError as e:
name = "未知曲目" + str(index)
os.rename(newfilepath, os.path.join(SavePath, name) + '.mp3')
finally:
...
else:
name = "未知曲目"+str(index)
try:
os.rename(newfilepath, os.path.join(SavePath, name)+'.mp3')
except FileExistsError as e:
print("file exist")
finally:
...教程
# 获取mp3歌曲名称
def wirtefilename(musicpath):
#print(musicpath)
fileinfo = MP3(musicpath, ID3 = EasyID3)
print(fileinfo)
if fileinfo != {}:
print(fileinfo['title'][0])
name = str(fileinfo['title'][0])
return str(name)
if __name__ == "__main__": listfiles(CachePath) index = 0 print(len(fList)) for i in fList: decodefile(os.path.join(CachePath, i),os.path.join(SavePath, "Temp.mp3"), index) index = index+1