国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-01-04 17:57:41
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了使用Python下載歌詞并嵌入歌曲文件中的實現代碼,需要借助eyed3模塊,需要的朋友可以參考下

使用python掃描本地音樂并下載歌詞

這次這個真的是干貨哦,昨晚弄了半晚上,,,,從8點吃完飯就開始寫,一直到了快12點才弄好,,,新手,傷不起呀。。。。

先簡單的說下吧,百度提供了一個音樂搜索的api,你想百度請求類似于

 

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

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

 

 
  1. This XML file does not appear to have any style information associated with it. The document tree is shown below. 
  2. <result> 
  3. <count>1</count> 
  4. <url> 
  5. <encode> 
  6. <![CDATA[ 
  7. http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$ 
  8. ]]> 
  9. </encode> 
  10. <decode> 
  11. <![CDATA[ 
  12. 12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537 
  13. ]]> 
  14. </decode> 
  15. <type>8</type> 
  16. <lrcid>2829</lrcid> 
  17. <flag>1</flag> 
  18. </url> 
  19. <durl> 
  20. <encode> 
  21. <![CDATA[ 
  22. http://zhangmenshiting2.baidu.com/data2/music/7345405/aGVnaWlmbGaeomZzrZmmnJZvmGqXbHCbl2dsZ5qXaWqSlWpsmmdrb2mXamxpbXCclGNsmW2ba25mYmxtapmZcWqTWaGemnRoX2VkbWdvaGhoZmZramluOA$ 
  23. ]]> 
  24. </encode> 
  25. <decode> 
  26. <![CDATA[ 
  27. 7345405.mp3?xcode=e6b69cf593ea22ac78e1478e78479dc19e8e4650995cb99a&mid=0.31929107437537 
  28. ]]> 
  29. </decode> 
  30. <type>8</type> 
  31. <lrcid>2829</lrcid> 
  32. <flag>1</flag> 
  33. </durl> 
  34. <p2p> 
  35. <hash>f98b6772aa97966550ec80617879becee0233bf4</hash> 
  36. <url> 
  37. <![CDATA[ ]]> 
  38. </url> 
  39. <type>mp3</type> 
  40. <size>3778335</size> 
  41. <bitrate>128</bitrate> 
  42. </p2p> 
  43. </result> 

簡單的說明下,由于我們要做的只是獲取到歌曲的lrc歌詞地址,所以有用的只有2829這個標簽。

而encode和decode里面的拼接起來就是mp3的下載地址,如本例的

 

 
  1. 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了。

好了,大概就是這樣,下面是代碼

 

 
  1. import os 
  2. import os.path 
  3. import re 
  4. import eyed3 
  5. import urllib2 
  6. import urllib 
  7. from urllib import urlencode 
  8. import sys  
  9.  
  10. import os 
  11. reload(sys)  
  12. sys.setdefaultencoding('utf8'
  13.  
  14. music_path = r"E:/music" 
  15. lrc_path = r"e:/lrc" 
  16.  
  17. os.remove('nolrc.txt'
  18. os.remove('lrcxml.txt'
  19.  
  20. the_file = open('lrcxml.txt','a'
  21. nolrc_file = open('nolrc.txt','a'
  22.  
  23. for root,dirs,files in os.walk(music_path): 
  24. for filepath in files: 
  25. the_path = os.path.join(root,filepath) 
  26. if (the_path.find("mp3") != -1): 
  27. print the_path 
  28. the_music = eyed3.load(the_path) 
  29. the_teg = the_music.tag._getAlbum() 
  30. the_artist = the_music.tag._getArtist() 
  31. the_title = the_music.tag._getTitle() 
  32. # print the_teg 
  33. # print the_title 
  34. # print the_artist 
  35. b = the_title.replace(' ','+'
  36. # print b 
  37. a = the_artist.replace(' ','+'
  38. #print urlencode(str(b)) 
  39. if isinstance(a,unicode): 
  40. a = a.encode('utf8'
  41. song_url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="+b+"$"+a+"$$ " 
  42.  
  43. the_file.write(song_url+'/n'
  44. page = urllib2.urlopen(song_url).read() 
  45. print page 
  46. theid = 0 
  47.  
  48. lrcid = re.compile('<lrcid>(.*?)</lrcid>',re.S).findall(page) 
  49. have_lrc = True 
  50. if lrcid != []: 
  51. theid = lrcid[0] 
  52.  
  53. else
  54. nolrc_file.write(the_title+'/n'
  55. have_lrc = False 
  56. print theid 
  57.  
  58.  
  59. if have_lrc: 
  60. firstid = int(theid)/100 
  61. lrcurl = "http://box.zhangmen.baidu.com/bdlrc/"+str(firstid)+"/"+theid+".lrc" 
  62. print lrcurl 
  63. lrc = urllib2.urlopen(lrcurl).read() 
  64. if(lrc.find('html')== -1): 
  65. lrcfile = open(lrc_path+"//"+the_title+".lrc",'w'
  66. lrcfile.writelines(lrc) 
  67. lrcfile.close() 
  68. else
  69. nolrc_file.write(the_title+'/n'
  70.  
  71. the_file.close() 
  72. nolrc_file.close() 
  73. print "end!" 

有用第一步請求所獲取到底是xml格式的,所以本來想著解析xml來獲取lrcid,但是在實現過程中遇到了各種問題,別的還容易,就在這一塊兒浪費的時間最長,糾結未果之后,只能改用正則表達式來獲取了。。。

使用python將歌詞嵌入歌曲中

以前一直用的是Google Play Music來作為手機的音樂播放器,可是現在谷歌被墻的這么厲害的,從PC上傳到Google Play的音樂在手機上面同步下來的話特麻煩,索性放棄之買了大名鼎鼎的Poweramp播放器,開始使用之后瞬間就被Poweramp強大的功能所吸引住了,不愧是安卓端的音樂播放器的王者!唯美的鎖屏界面,強大的均衡器功能等等。唯一美中不足的就是歌詞.如果要顯示歌詞的話必須安裝第三方軟件,或者是把歌詞嵌入到音樂中。所以昨天下班之后就開始研究,所幸最后終于搞定了,先上下效果圖

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

可以看到,效果還是很不錯的呢。

好了,廢話不多說,下面上程序

首先,必須安裝eyed3模塊,還有,我所有的歌詞都在E:/lrc這個路徑中的

 

 
  1. import threading 
  2. import time 
  3. import datetime 
  4. import re 
  5. import os 
  6. import eyed3 
  7. import sys 
  8. reload(sys) 
  9. sys.setdefaultencoding('utf8'
  10.  
  11.  
  12. def getstr(i): 
  13. if i <10: 
  14. return "0"+str(i) 
  15. else
  16. return str(i) 
  17.  
  18. musicpath=r'I:/music' 
  19.  
  20. lrcpath=r'E:/lrc' 
  21.  
  22.  
  23.  
  24. def deallrc(str): 
  25. mystr=re.sub(r'/[/d/d:/d/d./d/d/]','',str) 
  26. mystr.replace('/n',''
  27. return mystr 
  28.  
  29.  
  30.  
  31. def checklrcfile(path,timespan): 
  32. file=open(path,'r'
  33. mylrcstr='' 
  34. #print timespan 
  35. for line in file.readlines(100): 
  36. #errorlog(line) 
  37. if line.find(timespan)>0: 
  38. return deallrc(line) 
  39. else
  40. continue 
  41. return '' 
  42.  
  43.  
  44. def getlrcstr(lrc): 
  45. mylrcstr='' 
  46. #print lrc 
  47. for i in range(00,05): 
  48. for j in range(00,59): 
  49. for k in range(00,99): 
  50. timespan=getstr(i)+":"+getstr(j)+"."+getstr(k) 
  51. mylrcstr+=checklrcfile(lrc, timespan)  
  52. #print timespan 
  53. return mylrcstr 
  54.  
  55.  
  56. def getlrc(musicname): 
  57. musicname=u''.join(musicname) 
  58. musicname=musicname.encode('gb2312'
  59. for root,dirs,files in os.walk(lrcpath): 
  60. for filepath in files: 
  61. the_path = os.path.join(root,filepath) 
  62. if (the_path.find(musicname) != -1): 
  63. print the_path 
  64. return the_path 
  65.  
  66. def errorlog(path): 
  67. file=open(r'e:/nolrc.txt','a'
  68. if path is None: 
  69. path='' 
  70. path=path+'/n' 
  71. file.write(path) 
  72. file.close() 
  73.  
  74. def writetag(themusic,lrcstr): 
  75. music=eyed3.load(themusic) 
  76. lrcstr=lrcstr.decode('utf8'
  77. lrcstr=u''.join(lrcstr) 
  78. #lrcstr=unicode(lrcstr) 
  79. music.tag.lyrics.set(lrcstr) 
  80. music.tag.save() 
  81.  
  82.  
  83.  
  84.  
  85. def dealmusic(path): 
  86. print path 
  87. the_music = eyed3.load(path) 
  88. the_teg = the_music.tag._getAlbum() 
  89. the_artist = the_music.tag._getArtist() 
  90. the_title = the_music.tag._getTitle() 
  91. #print the_title 
  92.  
  93. try
  94. lrc=getlrc(the_title) 
  95. lrcstr=getlrcstr(lrc) 
  96. writetag(path, lrcstr)  
  97. except: 
  98. errorlog(path) 
  99.  
  100.  
  101.  
  102. class writelrc(threading.Thread): 
  103. def __init__(self,the_path): 
  104. threading.Thread.__init__(self) 
  105. self.thepath=the_path 
  106. def run(self): 
  107. dealmusic(self.thepath) 
  108.  
  109.  
  110. if __name__=='__main__'
  111. count=0 
  112. threads=[] 
  113. for root,dirs,files in os.walk(musicpath): 
  114. for filepath in files: 
  115. the_path = os.path.join(root,filepath) 
  116. if (the_path.find("mp3") != -1): 
  117. count+=1 
  118. threads.append(writelrc(the_path)) 
  119. if count%10==0: 
  120. for t in threads: 
  121. t.start() 
  122. for t in threads: 
  123. t.join() 
  124. threads=[]  

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 政和县| 金塔县| 鄂托克旗| 阳东县| 永定县| 平遥县| 高台县| 周宁县| 南皮县| 元朗区| 花莲县| 宾阳县| 长宁县| 长顺县| 绥宁县| 彰化市| 从化市| 珠海市| 建昌县| 惠来县| 华池县| 博客| 莱州市| 修文县| 隆安县| 临朐县| 应城市| 万宁市| 清丰县| 九江县| 姜堰市| 皋兰县| 鲁山县| 科尔| 灵璧县| 弥勒县| 六盘水市| 城固县| 青海省| 大石桥市| 洮南市|