前言
本文的文字及圖片來源於網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理。
作者:GeneralMonkey
Python解密網易雲音樂緩存文件獲取MP3
1、安裝mutagen
2、獲取緩存文件目錄文件
3、緩存文件解碼
4、獲取MP3歌曲信息
5、循環進行保存文件到指定目錄
全部源碼
1、安裝mutagen
首先進行安裝mutagen,直接命令行安裝,前提條件,你需要先安裝pip工具,如果你解密或者這個工具不懂,或者你也剛學python不久,建議去小編的Python交流.裙 :一久武其而而流一思(數字的諧音)轉換下可以找到了,里面有最新Python教程項目可拿,多跟里面的人交流,進步更快哦!
pip install mutagen
2、獲取緩存文件目錄文件
網易雲音樂客戶端中設置,找到你的音樂緩存目錄,里面有一個.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)
3、緩存文件解碼
拿到緩存文件的列表之后,我們開始進行異或運算,然后保存成mp3文件,期間需要進行獲取mp3文件信息,由於文件中有可能獲取不到歌曲的信息,我們做了一個操作,凡是獲取的不到歌曲名稱信息的文件,一律保存"未知歌曲"+序號的方式作為新文件名稱,當然你也可以自定義。
#音樂文件解碼
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:
4、獲取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])
5、循環進行保存文件到指定目錄
最后我們逐文件進行保存即可。
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 = []
#獲取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)
#音樂文件解碼
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