Python批量处理网易云音乐缓存文件获取MP3(一)

Python批量处理网易云音乐缓存文件获取MP3

  • 1、安装mutagen
  • 2、获取缓存文件目录文件
  • 3、缓存文件解码
  • 4、获取MP3歌曲信息
  • 5、循环进行保存文件到指定目录
  • 全部源码

1、安装mutagen

首先进行安装mutagen,直接命令行安装,前提条件,你需要先安装pip工具
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

================================================================
这一版生成出来的歌曲信息好多都是未命名,感觉不太好,后面借助爬虫工具更新了一下,请移步:
Python批量处理网易云音乐缓存文件获取MP3(二)

你可能感兴趣的:(Python)