zoukankan      html  css  js  c++  java
  • Python修改Mp3文件名称

      网上下载的很多mp3歌曲,都是用的时间ID比如20091003003.mp3这样的方式作为歌曲名称的,非常混乱!下载后,经常需要修改mp3的名字。这里我发一个我以前用Python写的,专门用来修改那些网上下载的mp3歌曲后名称不正确时候,使用该函数,使用方法
    ModifyMp3FileInfo(r'E:\音乐\12345.mp3'),就是字典与列表的用法!代码如下:

    代码
    #!修改下载的Mp3文件名称为正确的Mp3文件
    def ModifyMp3FileInfo(filename):
    mp3Id3V1
    = {
    "tag":{"valuepos":(0,3),"value":""},
    "SongName":{"valuepos":(3,33),"value":""},
    "SongPeople":{"valuepos":(33,63),"value":""},
    "Zj":{"valuepos":(63,93),"value":""},
    "Year":{"valuepos":(93,97),"value":""},
    "Bak":{"valuepos":(97,125),"value":""}
    }
    try:
    f
    = open(filename,'rb')
    f.seek(
    -128,2)
    sdata
    = f.read(3)
    if sdata == 'TAG':
    f.seek(
    -128,2)
    sdata
    = f.read(128)
    for tag,subitem in mp3Id3V1.items():
    subitem[
    "value"] = sdata[subitem["valuepos"][0]:subitem["valuepos"]

    [
    1]].replace('\00','').strip()
    print '%s='%tag,'%s'%subitem["value"],'\n'
    f.close()
    import os
    if mp3Id3V1["SongName"]["value"]!='':
    test
    = [os.path.dirname(filename),'\\']
    test.append(mp3Id3V1[
    "SongName"]["value"])
    test.append(
    '.mp3')
    newfilename
    = ''.join(test)
    print newfilename
    if os.path.exists(newfilename):
    test
    = ['Filename ',newfilename,' Has Existed']
    print ''.join(test)
    else:
    try:
    os.rename(filename,newfilename)
    except WindowsError,e:
    if e.winerror:
    print 'Modify filename failed ,maybe the file is inuse'
    else:
    print 'UnKnown error'
    else:
    print 'Is not a MP3 file'
    except IOError:
    print 'Open file failed'

    但是上面这个代码只是针对,歌曲本身记录了mp3名字信息的才能够正确修改名字,但是某些时候文件中并没有记录这些信息,一般的这些数据信息都记录在MP3的的Id3V1数据段中,所以这里再给一个直接写入信息到这个数据段中。代码:

    代码
    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()
    使用方式:

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

  • 相关阅读:
    Python自动发邮件-yagmail库
    使用Network Emulator Toolkit工具模拟网络丢包测试(上)
    Fiddler实战之使用Fiddler模拟弱网环境(限速)
    Jmeter接口测试之SSHCommand测试Linux服务器资源文件信息
    Fiddler实战之将抓到接口导入Jmeter接口测试
    Could not get lock /var/lib/dpkg/lock
    OPMS是什么?
    Docker是什么?
    Docker容器中启动OPMS项目
    Docker容器部署Tomcat服务器
  • 原文地址:https://www.cnblogs.com/DxSoft/p/1694563.html
Copyright © 2011-2022 走看看