zoukankan      html  css  js  c++  java
  • Python解密网易云音乐缓存文件获取MP3

    前言
    本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。
    作者: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

  • 相关阅读:
    内置函数02
    生成器
    OpenJudge 2979 陪审团的人选 / Poj 1015 Jury Compromise
    OpenJudge/Poj 1936 All in All
    模板:各类型的最大数和最小数表示
    OpenJudge/Poj 1661 帮助 Jimmy
    OpenJudge/Poj 1915 Knight Moves
    OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence
    OpenJudge/Poj 1163 The Triangle
    OpenJudge/Poj 1844 Sum
  • 原文地址:https://www.cnblogs.com/chengxuyuanaa/p/12053518.html
Copyright © 2011-2022 走看看