Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166907
  • 博文数量: 84
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 940
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-12 20:30
文章分类

全部博文(84)

文章存档

2010年(18)

2009年(27)

2008年(39)

我的朋友
最近访客

分类: Python/Ruby

2010-01-27 19:14:23

今天看了篇文章,就写了这个脚本,今天试了一下没有错误,可能是我电脑上刚好没有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 4864])
        Mp3Art=''.join([i for i in Mp3Art if 4864])
    # 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
 
阅读(673) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~