使用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. <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>
简单的说明下,由于我们要做的只是获取到歌曲的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('<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!"
有用第一步请求所获取到底是xml格式的,所以本来想着解析xml来获取lrcid,但是在实现过程中遇到了各种问题,别的还容易,就在这一块儿浪费的时间最长,纠结未果之后,只能改用正则表达式来获取了。。。
使用python将歌词嵌入歌曲中
以前一直用的是Google Play Music来作为手机的音乐播放器,可是现在谷歌被墙的这么厉害的,从PC上传到Google Play的音乐在手机上面同步下来的话特麻烦,索性放弃之买了大名鼎鼎的Poweramp播放器,开始使用之后瞬间就被Poweramp强大的功能所吸引住了,不愧是安卓端的音乐播放器的王者!唯美的锁屏界面,强大的均衡器功能等等。唯一美中不足的就是歌词.如果要显示歌词的话必须安装第三方软件,或者是把歌词嵌入到音乐中。所以昨天下班之后就开始研究,所幸最后终于搞定了,先上下效果图
可以看到,效果还是很不错的呢。
好了,废话不多说,下面上程序
首先,必须安装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=[]
好了,大概就是这样,大家有什么问题可以直接提出来,我会尽快回复的。

2 시간 이내에 Python의 기본 프로그래밍 개념과 기술을 배울 수 있습니다. 1. 변수 및 데이터 유형을 배우기, 2. 마스터 제어 흐름 (조건부 명세서 및 루프), 3. 기능의 정의 및 사용을 이해하십시오. 4. 간단한 예제 및 코드 스 니펫을 통해 Python 프로그래밍을 신속하게 시작하십시오.

Python은 웹 개발, 데이터 과학, 기계 학습, 자동화 및 스크립팅 분야에서 널리 사용됩니다. 1) 웹 개발에서 Django 및 Flask 프레임 워크는 개발 프로세스를 단순화합니다. 2) 데이터 과학 및 기계 학습 분야에서 Numpy, Pandas, Scikit-Learn 및 Tensorflow 라이브러리는 강력한 지원을 제공합니다. 3) 자동화 및 스크립팅 측면에서 Python은 자동화 된 테스트 및 시스템 관리와 같은 작업에 적합합니다.

2 시간 이내에 파이썬의 기본 사항을 배울 수 있습니다. 1. 변수 및 데이터 유형을 배우십시오. 이를 통해 간단한 파이썬 프로그램 작성을 시작하는 데 도움이됩니다.

10 시간 이내에 컴퓨터 초보자 프로그래밍 기본 사항을 가르치는 방법은 무엇입니까? 컴퓨터 초보자에게 프로그래밍 지식을 가르치는 데 10 시간 밖에 걸리지 않는다면 무엇을 가르치기로 선택 하시겠습니까?

Fiddlerevery Where를 사용할 때 Man-in-the-Middle Reading에 Fiddlereverywhere를 사용할 때 감지되는 방법 ...

Python 3.6에 피클 파일로드 3.6 환경 보고서 오류 : modulenotfounderror : nomodulename ...

경치 좋은 스팟 댓글 분석에서 Jieba Word 세분화 문제를 해결하는 방법은 무엇입니까? 경치가 좋은 스팟 댓글 및 분석을 수행 할 때 종종 Jieba Word 세분화 도구를 사용하여 텍스트를 처리합니다 ...

정규 표현식을 사용하여 첫 번째 닫힌 태그와 정지와 일치하는 방법은 무엇입니까? HTML 또는 기타 마크 업 언어를 다룰 때는 정규 표현식이 종종 필요합니다.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)
