用python写的命名mp3歌曲的脚本
时间:2010-01-27 来源:maonx
今天看了篇文章,就写了这个脚本,今天试了一下没有错误,可能是我电脑上刚好没有mp3的歌,只测了几首,还算成功的,但也有可能有bug,如果有人用弄出来的话,给我说下
因为有时候我下载歌的时候,mp3的歌名是乱的,今天看到了,这个脚本可以直接修改mp3为正确歌名
代码:
=================================================================================
#!/usr/bin/env python
#This shell used to rename mp3 files in a path
#usage:shellname path or run shell and input a path
import sys
import os
#This func used to return mp3 name and artist
def OpenMp3(Mp3File):
fd=open(Mp3File)
fd.seek(-128,2)
data=fd.read()
Mp3Name=Mp3Art=''
if data[:3]=='TAG':
Mp3Name=data[3:33].replace('\x00','')
Mp3Art=data[33:63].replace('\x00','')
Mp3Name=''.join([i for i in Mp3Name if 48<ord(i)<57 or ord(i)>64])
Mp3Art=''.join([i for i in Mp3Art if 48<ord(i)<57 or ord(i)>64])
# this two lines above used to del some punctuation
fd.close()
return Mp3Name,Mp3Art
if len(sys.argv)<2:
pathname=raw_input('Please input a path for mp3 file:')
else :
pathname=sys.argv[1]
if not os.path.isdir(pathname):
print 'The given path is wrong !'
exit(1)
TheDir=os.listdir(pathname)
Mp3Dir=[i for i in TheDir if '.mp3' in i] # get mp3 files list in a given path
os.chdir(pathname)
count=0
for i in range(len(Mp3Dir)): # rename mp3 file
(mp3Name,mp3Art)=OpenMp3(Mp3Dir[i])
new=mp3Art+'-'+mp3Name+'.mp3'
if not (new=='-.mp3'or new==Mp3Dir[i]):
os.rename(Mp3Dir[i],new)
count+=1
print '%d mp3 files renamed successfully' % count
#!/usr/bin/env python
#This shell used to rename mp3 files in a path
#usage:shellname path or run shell and input a path
import sys
import os
#This func used to return mp3 name and artist
def OpenMp3(Mp3File):
fd=open(Mp3File)
fd.seek(-128,2)
data=fd.read()
Mp3Name=Mp3Art=''
if data[:3]=='TAG':
Mp3Name=data[3:33].replace('\x00','')
Mp3Art=data[33:63].replace('\x00','')
Mp3Name=''.join([i for i in Mp3Name if 48<ord(i)<57 or ord(i)>64])
Mp3Art=''.join([i for i in Mp3Art if 48<ord(i)<57 or ord(i)>64])
# this two lines above used to del some punctuation
fd.close()
return Mp3Name,Mp3Art
if len(sys.argv)<2:
pathname=raw_input('Please input a path for mp3 file:')
else :
pathname=sys.argv[1]
if not os.path.isdir(pathname):
print 'The given path is wrong !'
exit(1)
TheDir=os.listdir(pathname)
Mp3Dir=[i for i in TheDir if '.mp3' in i] # get mp3 files list in a given path
os.chdir(pathname)
count=0
for i in range(len(Mp3Dir)): # rename mp3 file
(mp3Name,mp3Art)=OpenMp3(Mp3Dir[i])
new=mp3Art+'-'+mp3Name+'.mp3'
if not (new=='-.mp3'or new==Mp3Dir[i]):
os.rename(Mp3Dir[i],new)
count+=1
print '%d mp3 files renamed successfully' % count
相关阅读 更多 +