python实现批量通过MP3标题重命名文件名

您所在的位置:网站首页 怎么修改mp3文件的标题和艺术家 python实现批量通过MP3标题重命名文件名

python实现批量通过MP3标题重命名文件名

2024-06-26 17:15| 来源: 网络整理| 查看: 265

python实现批量通过MP3标题重命名文件名 需求

从MP3文件详细信息中提取出标题,歌手,专辑信息,用标题重命名MP3文件,用歌手和专辑对文件进行"歌手/专辑"分组 在这里插入图片描述 在这里插入图片描述

整体思路

1.遍历文件夹下的文件 2.针对每个文件,通过win32com的组件,使用windows功能读取文件的详细信息 3.根据取到的标题对名称不符的文件进行重命名 4.部分指定歌手的歌曲按照"歌手/专辑"的形式建立文件夹保存

获取MP3详细信息代码 import os import win32com.client def getInfo(base_file): '''提取信息''' _shell = win32com.client.DispatchEx("shell.Application") _dir = _shell.NameSpace(os.path.dirname(base_file)) item = _dir.ParseName(os.path.split(base_file)[1]) title = _dir.GetDetailsOf(item, 21) # 标题 artists = _dir.GetDetailsOf(item, 13) # 歌手 album = _dir.GetDetailsOf(item, 14) # 专辑 return title, artists, album 重命名和移动的代码 def rename(file_path, title): '''重命名''' new_filel_name = title + '.mp3' new_file_path = os.path.join(os.path.dirname(file_path), new_filel_name) if file_path != new_file_path: os.rename(file_path, new_file_path) print(file_path, '已更名为:', new_file_path) else: new_file_path = file_path return new_file_path def moveByArtist(file_path, artists, album): NEW_BASE_DIR = r'F:\play\音乐' '''根据歌手分组''' artist_list = ['周杰伦', '五月天', 'Beyond', '陈奕迅', 'Joe Hisaishi', '张学友'] file_name = os.path.split(file_path)[1] if artists in artist_list: # 存在歌手数据 if not re.search(r'^?+$', album.strip()): # 移到专辑下 new_path = os.path.join(NEW_BASE_DIR, artists.strip(), album.strip(), file_name) else: # 移到歌手下 new_path = os.path.join(NEW_BASE_DIR, artists.strip(), file_name) else: # 移到其他 new_path = os.path.join(NEW_BASE_DIR, '其他', file_name) if not os.path.exists(os.path.dirname(new_path)): os.makedirs(os.path.dirname(new_path)) if file_path != new_path: shutil.move(file_path, new_path) print(file_path, '已经移动到:', new_path) return new_path 主代码 if __name__ == "__main__": directory_base = r"F:\play\音乐" fileExtList = [".mp3", ".wav"] for root, directory, file_list in os.walk(directory_base): for file_name in file_list: if os.path.splitext(file_name)[1] in fileExtList: file_path = os.path.join(root, file_name) title, artists, album = getInfo(file_path) # print(title, artists, album,sep=', ') title = title.strip().replace('?','').replace('|','') if title.strip(): # 取到title file_path = rename(file_path, title) # 重命名 moveByArtist(file_path, artists, album) # 移动 反思和后记

这是我第一篇自己写的博客,属于原创的,写的简陋,还请大家多多指点。 参考文章是通过C#实现这个功能的,我自己只会python,因此研究了下通过python调用windows API,结果被我找到了shell的引用名为"shell.Application", 因此我认为该功能对我来说实现最关键的步骤是知道shell组件在python中的调用名称。至于win32com.client模块,我以前用过它处理excel,反倒没有难度。 后续还可以继续钻研,哪些windows组件支持win32com.client的调用,并且调用这些Windows组件的名称,以便更方便的在windows下使用python,提高使用工具的效率。 如果有哪位网友知道win32com调用windows组件的组件和调用命称,也可以在下面留言区留言告诉我,不胜感激。 目前我总结的组件调用名称有以下几个:

Excel.Application: 代表excel程序Word.Application: 待表word程序Outlook.Application: outlook程序Access.Application: access程序InternetExplorer.Application: IE程序Shell.Application: shell程序ADsNameSpaces: adsi 程序 引用

如何批量实现通过MP3标题重命名文件名(C#实现,内含文件详细属性和编码对应表)

https://blog.csdn.net/qq_20183489/article/details/72496074



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3