


Reading and writing id3v1 information of mp3 files using Python
1.起因
一直以来疯迷“冬吴相对论”,为了整理下载他的MP3花了不少功夫,今天突然发现将电脑中的mp3导入到itunes后,文件名竟然不识别了。#_* itunes自动识别了mp3的信息内容。多次一举么,文件名挺好。事实如此,让我深感不完美。一定要将文件名也写如MP3信息中区。
网上一搜,一大把的python代码,都是用了eyeD3这个组件包。照着例子简单搞了两下就出来一个版本,运行发现latin_1啥的编码问题。OK把它的tag和id3还有frames包中的编码统统改成GBK就能解决了。但是又发现,如果文件原本没有id3v1时,获取title就直接报错了。找了两下没有发现有人提这个问题。看来只能自己动手了。那就完全不用eyeD3包了。因为id3v1确实很简单。
2.分析
百度就有说,我想写的这些信息可保存于mp3文件的尾部。
ID3V1比较简单,它是存放在MP3文件的末尾,用16进制的编辑器打开一个MP3文件,查看其末尾的128个顺序存放字节,数据结构定义如下:
char Header[3]; /标签头必须是"TAG"否则认为没有标签/
char Title[30]; /标题/
char Artist[30]; /作者/
char Album[30]; /专集/
char Year[4]; /出品年代/
char Comment[30]; /备注/
char Genre; /类型/
ID3V1的各项信息都是顺序存放,没有任何标识将其分开,比如标题信息不足30个字节,则使用'\0'补足,否则将造成信息错误。
3.解决
还好,文件结构不复杂,处理起来就相对简单。思路很简单,读取mp3文件的尾部128字节,判断一下有米有TAG,有了就把最后的128节用我们自己的信息替换掉,没有就补充128字节上去。
4.代码
最好的文档就是源码,当然我回写注释的。没有依赖eyeD3这样的包,纯手工写法。
#encoding=utf8 __author__ ='pcode@qq.com'import os importstructdefGetFiles(path):""" 读取指定目录的文件 """FileDic=[] files=os.listdir(path)for f in files: f=f[:-4]FileDic.append(f)returnFileDic,files def_GetLast128K(path,file): ff1=open(os.path.join(path,file),"rb") ff1.seek(-128,2) id3v1data=ff1.read() ff1.close()return id3v1data def_GetAllBinData(path,file): ff1=open(os.path.join(path,file),"rb") data=ff1.read() ff1.close()return data defSetTag(path,file,title,artist,album,year,comment,genre):""" 设置mp3的ID3 v1中的部分参数 char Header[3]; /*标签头必须是"TAG"否则认为没有标签*/ char Title[30]; /*标题*/ char Artist[30]; /*作者*/ char Album[30]; /*专集*/ char Year[4]; /*出品年代*/ char Comment[30]; /*备注*/ char Genre; /*类型*/ mp3文件尾部128字节为id3v1的数据,如果有数据则读取修改,无数据则补充 """ header='TAG'#组合出最后128K的id3V1的数据内容 str =struct.pack('3s30s30s30s4s30ss',header,title,artist,album,year,comment,genre)#获取原始全部数据 data=_GetAllBinData(path,file)#获取末尾的128字节数据 id3v1data=_GetLast128K(path,file)#打开原文件准备写入 ff=open(os.path.join(path,file),"wb")try:#判断是否有id3v1数据if id3v1data[0:3]!=header:#倒数128字节不是以TAG开头的说明没有#按照id3v1的结构补充上去 ff.write(data+str)else:#有的情况下要换一下 ff.write(data[0:-128]+str) ff.close()print"OK"+title except: ff.write(data)print"Error "+title finally:if ff :ff.close()if __name__=="__main__":#我存放mp3文件的目录 path=u"K:\\reading\\阅读\\相对论"#获取到文件名和文件全名 names,files=GetFiles(path)#苦力代码for i in range(len(files)):#注意编码解码 title=names[i].encode('gbk') artist=u'作者'.encode('gbk') album=u'相对论'.encode('gbk') year='' comment='' genre=''#调用函数处理SetTag(path,files[i],title,artist,album,year,comment,genre)
5.后续
使用了以后id3v1的信息全部按文件名改好了,其中的SetTag函数也可以迁移到别的程序里用来改id3v1的信息。但是写文件那里,无论是否有TAG都得重写全部文件内容。效率一般般。速度没有eyeD3这种组件快。但那时eyeD3不能支持中文,而且文件本来没id3v1信息时会出错,自己的就放心多了。 bingo 收工。

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)