使用Python下載歌詞并嵌入歌曲文件中的實(shí)現(xiàn)代碼
使用python掃描本地音樂(lè)并下載歌詞
這次這個(gè)真的是干貨哦,昨晚弄了半晚上,,,,從8點(diǎn)吃完飯就開(kāi)始寫(xiě),一直到了快12點(diǎn)才弄好,,,新手,傷不起呀。。。。
先簡(jiǎn)單的說(shuō)下吧,百度提供了一個(gè)音樂(lè)搜索的api,你想百度請(qǐng)求類(lèi)似于
http://box.zhangmen.baidu.com/x?op=12&count=1&title=最佳損友$$陳奕迅$$$$
的地址,百度會(huì)給你返回一段xml,如下所示
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?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537 ]]> </decode> <type>8</type> <lrcid>2829</lrcid> <flag>1</flag> </url> <durl> <encode> <![CDATA[ http://zhangmenshiting2.baidu.com/data2/music/7345405/aGVnaWlmbGaeomZzrZmmnJZvmGqXbHCbl2dsZ5qXaWqSlWpsmmdrb2mXamxpbXCclGNsmW2ba25mYmxtapmZcWqTWaGemnRoX2VkbWdvaGhoZmZramluOA$$ ]]> </encode> <decode> <![CDATA[ 7345405.mp3?xcode=e6b69cf593ea22ac78e1478e78479dc19e8e4650995cb99a&mid=0.31929107437537 ]]> </decode> <type>8</type> <lrcid>2829</lrcid> <flag>1</flag> </durl> <p2p> <hash>f98b6772aa97966550ec80617879becee0233bf4</hash> <url> <![CDATA[ ]]> </url> <type>mp3</type> <size>3778335</size> <bitrate>128</bitrate> </p2p> </result>
簡(jiǎn)單的說(shuō)明下,由于我們要做的只是獲取到歌曲的lrc歌詞地址,所以有用的只有2829這個(gè)標(biāo)簽。
而encode和decode里面的拼接起來(lái)就是mp3的下載地址,如本例的
http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537
就是下載地址,不過(guò)音質(zhì)太差,有時(shí)間在研究下這個(gè)。
繼續(xù)說(shuō)歌詞,注意lrcid標(biāo)簽里面的2829
http://box.zhangmen.baidu.com/bdlrc/ 這個(gè)是百度lrc歌詞存放地址,
然后本例的歌詞地址是http://box.zhangmen.baidu.com/bdlrc/28/2829.lrc
看到了吧,歌詞地址后面的兩個(gè)數(shù)字的計(jì)算方法是在lrcid除以100所獲得的整數(shù),就是第一個(gè)數(shù)字,然后第二個(gè)數(shù)字就是lrcid,然后后面加上后綴.lrc就搞定了
獲得lrc地址之后就簡(jiǎn)單了,只要請(qǐng)求該地址,然后將獲取到的內(nèi)容寫(xiě)入文件就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('<lrcid>(.*?)</lrcid>',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!"
有用第一步請(qǐng)求所獲取到底是xml格式的,所以本來(lái)想著解析xml來(lái)獲取lrcid,但是在實(shí)現(xiàn)過(guò)程中遇到了各種問(wèn)題,別的還容易,就在這一塊兒浪費(fèi)的時(shí)間最長(zhǎng),糾結(jié)未果之后,只能改用正則表達(dá)式來(lái)獲取了。。。
使用python將歌詞嵌入歌曲中
以前一直用的是Google Play Music來(lái)作為手機(jī)的音樂(lè)播放器,可是現(xiàn)在谷歌被墻的這么厲害的,從PC上傳到Google Play的音樂(lè)在手機(jī)上面同步下來(lái)的話特麻煩,索性放棄之買(mǎi)了大名鼎鼎的Poweramp播放器,開(kāi)始使用之后瞬間就被Poweramp強(qiáng)大的功能所吸引住了,不愧是安卓端的音樂(lè)播放器的王者!唯美的鎖屏界面,強(qiáng)大的均衡器功能等等。唯一美中不足的就是歌詞.如果要顯示歌詞的話必須安裝第三方軟件,或者是把歌詞嵌入到音樂(lè)中。所以昨天下班之后就開(kāi)始研究,所幸最后終于搞定了,先上下效果圖

可以看到,效果還是很不錯(cuò)的呢。
好了,廢話不多說(shuō),下面上程序
首先,必須安裝eyed3模塊,還有,我所有的歌詞都在E:\lrc這個(gè)路徑中的
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=[]
好了,大概就是這樣,大家有什么問(wèn)題可以直接提出來(lái),我會(huì)盡快回復(fù)的。
相關(guān)文章
python使用dataframe_image將dataframe表格轉(zhuǎn)為圖片
本文主要介紹了python使用dataframe_image將dataframe表格轉(zhuǎn)為圖片,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Python中PDF轉(zhuǎn)Word的多種實(shí)現(xiàn)方法
在日常辦公和數(shù)據(jù)處理中,經(jīng)常需要將PDF文檔轉(zhuǎn)換為Word文檔,以便進(jìn)行編輯、修改或格式調(diào)整,Python作為一種強(qiáng)大的編程語(yǔ)言,提供了多種庫(kù)和工具來(lái)實(shí)現(xiàn)這一功能,以下是對(duì)Python中PDF轉(zhuǎn)Word技術(shù)的詳細(xì)介紹,需要的朋友可以參考下2025-01-01
Pandas_cum累積計(jì)算和rolling滾動(dòng)計(jì)算的用法詳解
今天小編就為大家分享一篇Pandas_cum累積計(jì)算和rolling滾動(dòng)計(jì)算的用法詳解,具有好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
數(shù)據(jù)清洗--DataFrame中的空值處理方法
今天小編就為大家分享一篇數(shù)據(jù)清洗--DataFrame中的空值處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
為什么說(shuō)python適合寫(xiě)爬蟲(chóng)
在本文中,小編給讀者們整理的一篇關(guān)于分析為什么說(shuō)python適合寫(xiě)爬蟲(chóng)的語(yǔ)言的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-06-06
python3實(shí)現(xiàn)UDP協(xié)議的服務(wù)器和客戶(hù)端
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)UDP協(xié)議的服務(wù)器和客戶(hù)端,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
使用python爬取4K壁紙保存到本地文件夾的全過(guò)程
圖片信息豐富多彩,許多網(wǎng)站上都有大量精美的圖片資源,有時(shí)候我們可能需要批量下載這些圖片,而手動(dòng)一個(gè)個(gè)下載顯然效率太低,所以本文給大家介紹了使用python爬取4K壁紙保存到本地文件夾的全過(guò)程,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-03-03
pandas按某2列進(jìn)行分層隨機(jī)抽樣的實(shí)現(xiàn)
本文主要介紹了pandas按某2列進(jìn)行分層隨機(jī)抽樣的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Python將list中的string批量轉(zhuǎn)化成int/float的方法
今天小編就為大家分享一篇Python將list中的string批量轉(zhuǎn)化成int/float的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

