使用python扫描本地音乐并下载歌词
这次这个真的是干货哦,昨晚弄了半晚上,,,,从8点吃完饭就开始写,一直到了快12点才弄好,,,新手,伤不起呀。。。。
先简单的说下吧,百度提供了一个音乐搜索的api,你想百度请求类似于

http://box.zhangmen.baidu.com/x"htmlcode">
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<result>
<count>1</count>
<url>
<encode>
<![CDATA[
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$
]]>
</encode>
<decode>
<![CDATA[
12762845.mp3"htmlcode">
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3"htmlcode">
import os
import os.path
import re
import eyed3
import urllib2
import urllib
from urllib import urlencode
import sys 

import os
reload(sys) 
sys.setdefaultencoding('utf8')

music_path = r"E:\music"
lrc_path = r"e:\lrc"

os.remove('nolrc.txt')
os.remove('lrcxml.txt')

the_file = open('lrcxml.txt','a')
nolrc_file = open('nolrc.txt','a')

for root,dirs,files in os.walk(music_path):
  for filepath in files:
    the_path = os.path.join(root,filepath)
    if (the_path.find("mp3") != -1):
      print the_path
      the_music = eyed3.load(the_path)
      the_teg = the_music.tag._getAlbum()
      the_artist = the_music.tag._getArtist()
      the_title = the_music.tag._getTitle()
      # print the_teg
      # print the_title
      # print the_artist
      b = the_title.replace(' ','+')
      # print b
      a = the_artist.replace(' ','+')
      #print urlencode(str(b))
      if isinstance(a,unicode):
        a = a.encode('utf8')
      song_url = "http://box.zhangmen.baidu.com/x"+b+"$$"+a+"$$$$ "
     
      the_file.write(song_url+'\n')
      page = urllib2.urlopen(song_url).read()
      print page
      theid = 0
      
      lrcid = re.compile('<lrcid>(.*"http://box.zhangmen.baidu.com/bdlrc/"+str(firstid)+"/"+theid+".lrc"
        print lrcurl
        lrc = urllib2.urlopen(lrcurl).read()
        if(lrc.find('html')== -1):
          lrcfile = open(lrc_path+"\\"+the_title+".lrc",'w')
          lrcfile.writelines(lrc)
          lrcfile.close()
        else:
          nolrc_file.write(the_title+'\n')
        
the_file.close()
nolrc_file.close()
print "end!"

有用第一步请求所获取到底是xml格式的,所以本来想着解析xml来获取lrcid,但是在实现过程中遇到了各种问题,别的还容易,就在这一块儿浪费的时间最长,纠结未果之后,只能改用正则表达式来获取了。。。

使用python将歌词嵌入歌曲中
以前一直用的是Google Play Music来作为手机的音乐播放器,可是现在谷歌被墙的这么厉害的,从PC上传到Google Play的音乐在手机上面同步下来的话特麻烦,索性放弃之买了大名鼎鼎的Poweramp播放器,开始使用之后瞬间就被Poweramp强大的功能所吸引住了,不愧是安卓端的音乐播放器的王者!唯美的锁屏界面,强大的均衡器功能等等。唯一美中不足的就是歌词.如果要显示歌词的话必须安装第三方软件,或者是把歌词嵌入到音乐中。所以昨天下班之后就开始研究,所幸最后终于搞定了,先上下效果图

使用Python下载歌词并嵌入歌曲文件中的实现代码

可以看到,效果还是很不错的呢。
好了,废话不多说,下面上程序
首先,必须安装eyed3模块,还有,我所有的歌词都在E:\lrc这个路径中的

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=[]     

  

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

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!