欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

使用Python下載歌詞并嵌入歌曲文件中的實現代碼

系統 2004 0

使用python掃描本地音樂并下載歌詞
這次這個真的是干貨哦,昨晚弄了半晚上,,,,從8點吃完飯就開始寫,一直到了快12點才弄好,,,新手,傷不起呀。。。。
先簡單的說下吧,百度提供了一個音樂搜索的api,你想百度請求類似于

            
http://box.zhangmen.baidu.com/x?op=12&count=1&title=最佳損友$$陳奕迅$$$$

          

的地址,百度會給你返回一段xml,如下所示

            
This XML file does not appear to have any style information associated with it. The document tree is shown below.

            
              
                1
              
              
                
                                  
                
                                  
                
                  8
                
                
                  2829
                
                
                  1
                
              
              
                
                                  
                
                                  
                
                  8
                
                
                  2829
                
                
                  1
                
              
              
                
                  f98b6772aa97966550ec80617879becee0233bf4
                
                
                                  
                
                  mp3
                
                
                  3778335
                
                
                  128
                
              
            
          

簡單的說明下,由于我們要做的只是獲取到歌曲的lrc歌詞地址,所以有用的只有2829這個標簽。
而encode和decode里面的拼接起來就是mp3的下載地址,如本例的

            
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537

          

就是下載地址,不過音質太差,有時間在研究下這個。
繼續說歌詞,注意lrcid標簽里面的2829
http://box.zhangmen.baidu.com/bdlrc/ 這個是百度lrc歌詞存放地址,
然后本例的歌詞地址是http://box.zhangmen.baidu.com/bdlrc/28/2829.lrc
看到了吧,歌詞地址后面的兩個數字的計算方法是在lrcid除以100所獲得的整數,就是第一個數字,然后第二個數字就是lrcid,然后后面加上后綴.lrc就搞定了
獲得lrc地址之后就簡單了,只要請求該地址,然后將獲取到的內容寫入文件就ok了。
好了,大概就是這樣,下面是代碼

            
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?op=12&count=1&title="+b+"$$"+a+"$$$$ "
     
      the_file.write(song_url+'\n')
      page = urllib2.urlopen(song_url).read()
      print page
      theid = 0
      
      lrcid = re.compile('
            
              (.*?)
            
            ',re.S).findall(page)
      have_lrc = True
      if lrcid != []:
        theid = lrcid[0]
        
      else:
        nolrc_file.write(the_title+'\n')
        have_lrc = False
      print theid
      
      
      if have_lrc:
        firstid = int(theid)/100
        lrcurl = "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下載歌詞并嵌入歌曲文件中的實現代碼_第1張圖片

可以看到,效果還是很不錯的呢。
好了,廢話不多說,下面上程序
首先,必須安裝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=[]     

          

??

好了,大概就是這樣,大家有什么問題可以直接提出來,我會盡快回復的。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 唐人社电亚洲一区二区三区 | 青娱乐欧美视频 | 精品啪啪 | 国产日韩精品一区二区 | 伊人二本二区 | 亚洲精品欧美一区二区三区 | 婷婷国产成人精品视频 | 亚洲蜜桃AV色情精品成人 | 国产日韩欧美在线 | 中文字幕 在线观看 | 日本aaaaa高清免费看 | xxxxhdvideosex | 久久r热这里有精品视频 | 国产欧美日本亚洲精品五区 | 免费黄色欧美视频 | 久久亚洲一区二区 | 女人一级毛片免费视频观看 | 日本不卡一区在线观看 | 91免费精品国偷自产在线在线 | 日本aⅴ在线观看 | 久久精品一区二区三区四区 | 亚洲精品国产第1页 | 91高清视频在线观看 | 久久久久久久久国产 | 222奇米影视 | 久久精品a一级国产免视看成人 | 最近最新中文字幕 | 免费国产自久久久久三四区久久 | 日本一级成人毛片免费观看 | Jizjizjizjiz日本护士水多 | 久久精品中文字幕首页 | 一级做a爱片久久 | 中文字幕成人av | 三级国产精品一区二区 | 欧美亚洲欧美区 | 99色这里只有精品 | 精品久久久久久蜜臂a∨ | 日韩精品一区二区电影 | 久久99精品视频 | 欧美一区二区三区在线可观看 | 国产一区二区欧美 |