免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4882 | 回复: 0
打印 上一主题 下一主题

用python写的命名mp3歌曲的脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-27 19:14 |只看该作者 |倒序浏览

                今天看了篇文章,就写了这个脚本,今天试了一下没有错误,可能是我电脑上刚好没有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)
    new=mp3Art+'-'+mp3Name+'.mp3'
    if not (new=='-.mp3'or new==Mp3Dir):
        os.rename(Mp3Dir,new)
        count+=1
print '%d mp3 files renamed successfully' % count


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/82009/showart_2162444.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP