首页 编程教程正文

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

piaodoo 编程教程 2020-02-02 12:31:44 740 0 python教程

这篇文章主要介绍了使用Python下载歌词并嵌入歌曲文件中的实现代码,需要借助eyed3模块,需要的朋友可以参考下

使用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强大的功能所吸引住了,不愧是安卓端的音乐播放器的王者!唯美的锁屏界面,强大的均衡器功能等等。唯一美中不足的就是歌词.如果要显示歌词的话必须安装第三方软件,或者是把歌词嵌入到音乐中。所以昨天下班之后就开始研究,所幸最后终于搞定了,先上下效果图

20151113154400981.png (175×300)

可以看到,效果还是很不错的呢。
好了,废话不多说,下面上程序
首先,必须安装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=[]     

  

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

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/2189.html

评论

搜索

游戏网站源码,织梦网站源码,wordpress,wordpress主题,wordpress下载,wordpress插件,wordpress.com,wordpress模板,wordpress教程,wordpress 主题,wordpress安装,wordpress 模板,wordpress 插件,wordpress主题下载,wordpress企业主题,wordpress seo,wordpress主题开发,wordpress theme,wordpress论坛,wordpress 企业主题,wordpress主机,wordpress中文主题,wordpress cms主题,wordpress plugin,wordpress 主题下载,wordpress 主机,wordpress空间,wordpress mu,wordpress 模版,wordpress汉化主题,wordpress淘宝客主题,wordpress 空间,wordpress代码,WORDPRESS HOSTING,wordpress优点,wordpress安卓客户端,wordpress技巧,wordpress换空间,wordpress themes,网站模板,ppt模板网站,模板网站,企业网站模板,网站设计模板,免费网站模板,个人网站模板,ppt模板下载网站,网站模板下载,公司网站模板,门户网站模板,学校网站模板,网站首页模板,网站模板免费下载,旅游网站模板,网站后台模板,免费网站模板下载,传奇网站模板,网站建设模板,外贸网站模板,网站 模板,个人主页网站模板,个人网站模板下载,政府网站模板,音乐网站模板,导航网站模板,免费企业网站模板,企业网站模板下载,手表网站模板,韩国网站模板,汽车网站模板,教育网站模板,网站后台管理模板,班级网站模板,新闻网站模板,房产中介网站模板,旅游网站模板下载,工艺品网站模板,电子商务网站模板,旅游网站设计模板,团购网站模板,flash网站模板,个人网站设计模板,婚庆网站模板,广告公司网站模板,商业网站模板,手机网站模板,免费模板网站推荐,ppt免费模板网站推荐,织梦网站模板,html网站模板建站,网站html模板,免费个人网站模板,公司网站源码,sns源码,彩票网站源码,周易网站源码,源码基地,交友源码,学校网站源码,asp.net 源码,源码天下,jsp网站源码,论坛源码下载,广告联盟源码,建站源码,delphi源码,源码爱好者,酷源码,net源码,源码超市,医疗网站源码,flash源码,搜源码,源码程序,dede源码,新闻网站源码,易语言源码大全,旅游网站源码下载,flash 源码,免费源码论坛,android游戏源码,电脑维修网站源码,30源码网,股票软件源码,卖源码,源码教程,安居客 源码,vip源码,家教源码,.net源码下载,Web源码,网络公司源码,佛教网站源码,android源码学习,房产源码,钓鱼网站源码,775源码屋,web游戏源码,成品网站 源码78w78不用下载,h5游戏网站源码,asp网站源码下载,webgame源码,电子商务网站源码,vb.net源码,乐嘿源码,8a商业源码论坛,fbreader源码,在线客服系统 源码,google源码,.net网站源码,快递查询源码,源码搜藏网,dede整站源码,周易 源码,52源码论坛,财经网站源码,织梦下载站源码,qq钓鱼网站源码,flash游戏源码,房产网源码,源码搜搜,电子商务源码,团购网站源码,团购网源码,jsp源码下载,jsp源码,h站源码,8a源码,婚纱摄影网站源码,易语言盗号源码,x站源码,qq空间psd源码,免费商业源码,笑话网站源码,源码集合,源码家园,啊哦源码,星期六源码,源码熊,阿奇源码,百分百源码网,一手日源码资源,旅行网站源码,b站工程源码泄露,新站长源码,8a商业源码,asp论坛源码,flash源码下载,404源码社区,创业网站源码,php网页源码,易支付源码,成品网站w灬源码,免费CMS成品网站源码,成品网站W灬源码1688仙踪林,成品APP短视频源码下载网站,成品网站源码1688可靠吗,免费B2B网站源码,成品APP直播源码下载,国外儿童网站源码在线,成品网站W灬源码1688,源码,成品网站w灬 源码1688,免费源码网站都有哪些,成品网站源码78W78隐藏通道1,网站源码,源码网,源码网站,源码时代,源码之家,源码下载,php源码,易语言源码,源码论坛,源码是什么,商城源码,论坛源码,源码交易,源码站,源码库,免费源码,免费网站ja**源码大全,ja**源码,成品网站w灬源码1377,a5源码,站长源码,成品网站源码78W78隐藏通道1APP,源码分享,网站源码下载,源码中国,asp源码,源码社区,企业网站源码,php源码下载,成品app直播源码搭建,在线观看视频网站源码2021,旅游网站源码,安卓源码,通达信选股公式源码,神马影院php源码,c#源码,成品网站w灬源码1688网页,php 源码,网页游戏源码,android源码下载,源码吧,视频源码大全,成品短视频APP源码搭建,asp源码下载,私服源码,电脑维修源码,个人主页源码,源码出售,php网站源码,刀客源码,网址导航源码,导航网站源码,源码天空,asp 源码,软件源码,精品源码,成品网站源码1688自动跳转,个人网站源码,源码哥,在线考试系统源码,cms源码,c# 源码,商业源码,vb源码,门户网站源码,音乐网站源码,中国源码,安卓源码下载,asp网站源码,在线客服源码,电影网站源码,免费源码下载,整站源码,源码交易网,易语言源码网,.net源码,在线客服系统源码,淘客源码,卡盟源码,网站源码出售,vb源码下载,莎莎源码,熊猫烧香源码,asp.net源码,商业源码网,外贸网站源码,61源码网,zblog模板,zblog企业模板,帝国cms模板,帝国cms插件,discuz模板