Python写入数据到MP3文件中的方法

yipeiwu_com6年前Python基础

本文实例讲述了Python写入数据到MP3文件中的方法。分享给大家供大家参考。具体分析如下:

通过Mp3的Id3V1数据段的数据来修正Mp3文件的正确名字,但是,有时候这个数据断中的数据是空的,所以这里写一个修改Id3V1数据段的数据的函数,同样是练习。

使用方法:

writeMp3Header[ SongName] = '测试歌曲名称' 
writeMp3Header[ SongPeople] = '不得闲' 
writeMp3Header[ ZhuanJi] = '专辑' 
writeMp3Header[ Year] = '2009' 
writeMp3Header[ Bak] = '备注测试' 
setMp3Header(r'E:\test.mp3')

Python代码如下:

writeMp3Header = {
   "SongName":"",
   "SongPeople":"",
   "ZhuanJi":"",
   "Year":"",
   "Bak":""
   }
 def setMp3Header(mp3file):
   mp3Id3V1 = {    
     "SongName":-125,
     "SongPeople":-95,
     "ZhuanJi":-65,
     "Year":-35,
     "Bak":-31
     }
   tags = ['SongName','SongPeople','ZhuanJi','Bak']
   f = open(mp3file,'r+')
   try:
     f.seek(-128,2)
     try:
       tempstr = f.read(3)
       if tempstr == 'TAG':
         for tag,startPos in mp3Id3V1.items():
           if writeMp3Header[tag] != '':
             f.seek(startPos,2)
             if tag in tags:
               if len(writeMp3Header[tag]) > 30: 
                 f.write(writeMp3Header[tag][:30])
               else:
                 f.write(writeMp3Header[tag])
             print startPos,tag,writeMp3Header[tag]
       else:
         print 'is not a mp3file'
     except IOError:
       print 'read error'
   finally:
     f.close()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

运行django项目指定IP和端口的方法

运行django项目指定IP和端口的方法

一、django项目启动命令 默认IP和端口 python manage.py runserver 指定端口 python manage.py runserver 192.1...

Python批量查询关键词微信指数实例方法

Python批量查询关键词微信指数实例方法

教你用Python批量查询关键词微信指数。 前期准备安装好Python开发环境及Fiddler抓包工具。前期准备安装好Python开发环境及Fiddler抓包工具。 首先打开Fiddle...

python的pandas工具包,保存.csv文件时不要表头的实例

用pandas处理.csv文件时,有时我们希望保存的.csv文件没有表头,于是我去看了DataFrame.to_csv的document。 发现只需要再添加header=None这个参数...

pandas的连接函数concat()函数的具体使用方法

pandas的连接函数concat()函数的具体使用方法

concat()函数的具体用法 pd.concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,...

mac下如何将python2.7改为python3

mac下如何将python2.7改为python3

1.查看当前电脑python版本 python -V  // 显示2.7.x 2.用brew升级python brew update python  3.如果安装...