Use Python to download lyrics and embed implementation code in song files

Source: Internet
Author: User
Tags cdata
Use Python to scan local music and download lyrics
This time this is really dry, oh, last night to get a half night,,, from 8 o ' eat the meal began to write, until the quick 12 o'clock to fix,, novice, hurt Ah ....
The first simple say, Baidu provides a music search API, you want to Baidu request similar to

http://box.zhangmen.baidu.com/x?op=12&count=1&title= best loss friend $$ Eason Chan $

Address, Baidu will give you an XML return, as shown below

This XML file does does appear to has any style information associated with it. The document tree is shown below.
 
  
   
    1
   
  
  
    
     
     
     8 2829 1 
     http://zhangmenshiting.baidu.com/data2/music/12762845/YmRqamdua21fn6NndK6ap5WXcJlrmG1xlJhobWibmGpjk5ZtmWiZcWRjZ5lqbGyelGKWlZtubGljZ5lka2uanWSXY1qin5t1YWBmZW5ocGlhaWdnbGtqbzE$ 
 
     
     12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537 
 
     
 
 
 
 
 

   
  
  
    
     
     
     8 2829 1 
     http://zhangmenshiting2.baidu.com/data2/music/7345405/aGVnaWlmbGaeomZzrZmmnJZvmGqXbHCbl2dsZ5qXaWqSlWpsmmdrb2mXamxpbXCclGNsmW2ba25mYmxtapmZcWqTWaGemnRoX2VkbWdvaGhoZmZramluOA$$ 
 
     
     7345405.mp3?xcode=e6b69cf593ea22ac78e1478e78479dc19e8e4650995cb99a&mid=0.31929107437537 
 
     
 
 
 
 
 

   
  
  
    
    
     f98b6772aa97966550ec80617879becee0233bf4
     
     
     mp3 3778335 
       
 
     
     
    
 
 
 
 
 

    -
  
 

Simply explained below, since all we have to do is get to the song's LRC lyrics address, so useful only 2829 of this tag.
and encode and decode inside the stitching up is mp3 download address, as in this example

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

is the download address, but the sound quality is too poor, there is time to study this.
Keep saying the lyrics, note the 2829 in the lrcid tag.
http://box.zhangmen.baidu.com/bdlrc/This is Baidu LRC lyrics storage address,
Then the lyrics address of this example is HTTP://BOX.ZHANGMEN.BAIDU.COM/BDLRC/28/2829.LRC
See, the two numbers after the lyrics address are calculated by dividing lrcid by 100, which is the first number, and then the second number is lrcid, followed by a suffix. The LRC is done.
It's easy to get the LRC address, just request the address, and then write the retrieved content to the file.
Okay, that's probably it, here's the code.

Import osimport os.pathimport reimport eyed3import urllib2import urllibfrom urllib import urlencodeimport sys import Osrel Oad (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 th E_path the_music = eyed3.load (the_path) The_teg = The_music.tag._getalbum () the_artist = The_music.tag._get      Artist () 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)) I F isinstance (a,unicode): a = A.encode (' UTF8 ') Song_url = "Http://box.zhangmen.baidu.com/x?op=12&count=1&am          P;title= "+b+" $$ "+a+" $ " The_file.write (song_url+ ' \ n ') page = Urllib2.urlopen (song_url). Read () print page Theid = 0 Lrci D = Re.compile ('
 
  
   
 (.*?)

  
 ', Re. S). FindAll (page) HAVE_LRC = True if lrcid! = []: Theid = lrcid[0] Else:nolrc_file.wr         Ite (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 = URL          Lib2.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 ') th E_file.close () nolrc_file.close () print "end!"

Useful first step request is obtained in the end is XML format, so originally wanted to parse XML to get lrcid, but in the implementation process encountered a variety of problems, other also easy, in this together wasted time the longest, tangled not fruit, can only use regular expression to get ...

Embed lyrics into a song using Python
Used to be Google Play music to use as a mobile phone, but now Google is so powerful wall, from the PC upload to Google Play music on the phone sync down the words of special trouble, simply give up to buy the famous Poweramp player, began to use immediately after being poweramp powerful features attracted, is worthy of Android music player of the King! Beautiful lock screen interface, powerful equalizer functions and so on. The only drawback is the lyrics. If you want to display lyrics, you must install third-party software, or embed lyrics into music. So yesterday after work began to study, fortunately finally finally settled, first up and down

Can see, the effect is very good.
Well, nonsense not much to say, below the program
First of all, the EYED3 module must be installed, and all my lyrics are E:\LRC in this path.

Import threadingimport timeimport datetimeimport reimport osimport eyed3import sysreload (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 D EALLRC (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 (+): #errorlog (line) if L  Ine.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) + ":" +get Str (j) + "." +getstr (k) Mylrcstr+=checklrcfile (LRC, TimeSpan) #print TimeSpan return Mylrcstrdef GETLRC (musicname): Musi Cname=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_pathdef E Rrorlog (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 = ey Ed3.load (path) The_teg = The_music.tag._getalbum () the_artist = The_music.tag._getartist () The_title = The_music.tag._g    Ettitle () #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=[]

Well, probably so, we have any questions can be directly proposed, I will reply as soon as possible.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.