php 音乐歌词xml,使用Python下载歌词并嵌入歌曲文件中的实现代码

import threading

import time

import datetime

import re

import os

import eyed3

import sys

reload(sys)

sys.setdefaultencoding('utf8')

def getstr(i):

if i <10:

return "0"+str(i)

else:

return str(i)

musicpath=r'I:\music'

lrcpath=r'E:\lrc'

def deallrc(str):

mystr=re.sub(r'\[\d\d:\d\d.\d\d\]','',str)

mystr.replace('\n','')

return mystr

def checklrcfile(path,timespan):

file=open(path,'r')

mylrcstr=''

#print timespan

for line in file.readlines(100):

#errorlog(line)

if line.find(timespan)>0:

return deallrc(line)

else:

continue

return ''

def getlrcstr(lrc):

mylrcstr=''

#print lrc

for i in range(00,05):

for j in range(00,59):

for k in range(00,99):

timespan=getstr(i)+":"+getstr(j)+"."+getstr(k)

mylrcstr+=checklrcfile(lrc, timespan)

#print timespan

return mylrcstr

def getlrc(musicname):

musicname=u''.join(musicname)

musicname=musicname.encode('gb2312')

for root,dirs,files in os.walk(lrcpath):

for filepath in files:

the_path = os.path.join(root,filepath)

if (the_path.find(musicname) != -1):

print the_path

return the_path

def errorlog(path):

file=open(r'e:\nolrc.txt','a')

if path is None:

path=''

path=path+'\n'

file.write(path)

file.close()

def writetag(themusic,lrcstr):

music=eyed3.load(themusic)

lrcstr=lrcstr.decode('utf8')

lrcstr=u''.join(lrcstr)

#lrcstr=unicode(lrcstr)

music.tag.lyrics.set(lrcstr)

music.tag.save()

def dealmusic(path):

print path

the_music = eyed3.load(path)

the_teg = the_music.tag._getAlbum()

the_artist = the_music.tag._getArtist()

the_title = the_music.tag._getTitle()

#print the_title

try:

lrc=getlrc(the_title)

lrcstr=getlrcstr(lrc)

writetag(path, lrcstr)

except:

errorlog(path)

class writelrc(threading.Thread):

def __init__(self,the_path):

threading.Thread.__init__(self)

self.thepath=the_path

def run(self):

dealmusic(self.thepath)

if __name__=='__main__':

count=0

threads=[]

for root,dirs,files in os.walk(musicpath):

for filepath in files:

the_path = os.path.join(root,filepath)

if (the_path.find("mp3") != -1):

count+=1

threads.append(writelrc(the_path))

if count%10==0:

for t in threads:

t.start()

for t in threads:

t.join()

threads=[]

好了,大概就是这样,大家有什么问题可以直接提出来,我会尽快回复的。

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值