欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 基于wx实现音乐播放

程序员文章站 2022-06-26 08:06:48
# -*- coding: utf-8 -*-"""created on sat apr 25 18:15:36 2020@author: administrator"""import request...
# -*- coding: utf-8 -*-
"""
created on sat apr 25 18:15:36 2020

@author: administrator
"""
import requests
import binascii
import json
import datetime
import traceback
from crypto.cipher import aes
from tkinter import *

fake_headers = {
  'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', # noqa
  'accept-charset': 'utf-8,*;q=0.5',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-us,en;q=0.8',
  'user-agent': 'mozilla/5.0 (windows nt 10.0; wow64; rv:60.0) gecko/20100101 firefox/60.0', # noqa
  'referer': 'https://www.google.com'
}
def netease_search(keyword) -> list:
  ''' 从网易云音乐搜索 '''
  count = 30
  eparams = {
    'method': 'post',
    'url': 'http://music.163.com/api/cloudsearch/pc',
    'params': {
      's': keyword,
      'type': 1,
      'offset': 0,
      'limit': count
    }
  }
  data = {'eparams': encode_netease_data(eparams)}

  s = requests.session()
  s.headers.update(fake_headers)
  s.headers.update({
    'referer': 'http://music.163.com/',
  })
 

  r = s.post('http://music.163.com/api/linux/forward', data=data)

  
  j = r.json()


  music_list = []
  id=0
  try:
    for m in j['result']['songs']:
      if m['privilege']['fl'] == 0:
        # 没有版权
        continue
      # 获得歌手名字
      singers = []
      for singer in m['ar']:
        singers.append(singer['name'])
      # 获得最优音质的文件大小
      if m['privilege']['fl'] >= 320000 and 'h' in m.keys() and m['h']:
        # 有时候即使>=320000,h属性依然为none
        size = m['h']['size']
      elif m['privilege']['fl'] >= 192000 and 'm' in m.keys() and m['m']:
        size = m['m']['size']
      else:
        size = m['l']['size']

      music = {
        'title': m['name'],
        'id': m['id'],
        'duration': str(datetime.timedelta(seconds=int(m['dt']/1000))),
        'singer': '、'.join(singers),
        'album': m['al']['name'],
        'size': round(size / 1048576, 2),
        'source': 'netease'
      }
      music=[]
      print(m['name'])
      music.append(id)
      music.append(m['name'])
      music.append('、'.join(singers))
      href="http://music.163.com/song/media/outer/url?id=" rel="external nofollow" +str(m['id'])+".mp3"
      print(href)
      music.append(href)
      music.append(m['id'])
      music_list.append(music)
      id=id+1
      
  except exception:
    # 如果是详细模式则输出详细错误信息
#    err = traceback.format_exc() if glovar.get_option('verbose') else str(e)
#    raise dataerror(err)
    
    pass

  return music_list





def encode_netease_data(data) -> str:
  data = json.dumps(data)
  key = binascii.unhexlify('7246674226682325323f5e6544673a51')
  encryptor = aes.new(key, aes.mode_ecb)
  # 补足data长度,使其是16的倍数
  pad = 16 - len(data) % 16
  fix = chr(pad) * pad
  byte_data = (data + fix).encode('utf-8')
  return binascii.hexlify(encryptor.encrypt(byte_data)).upper().decode()

def qq_search(keyword):
  import urllib.parse as parse
  from urllib.request import urlretrieve
  import requests
  import json
  import os
  import time
  import sys
  w=parse.urlencode({'w':keyword})
  
 
  url='https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.song&searchid=63229658163010696&t=0&aggr=1&cr=1&catzhida=1&lossless=0&flag_qc=0&p=1&n=30&%s&g_tk=5381&loginuin=0&hostuin=0&format=json&incharset=utf8&outcharset=utf-8&notice=0&platform=yqq.json&neednewcode=0'%(w)
   
  content=requests.get(url=url)
   
  str_1=content.text
   
  dict_1=json.loads(str_1)
   
  song_list=dict_1['data']['song']['list']
   
  str_3='''https://u.y.qq.com/cgi-bin/musicu.fcg?-=getplaysongvkey5559460738919986&g_tk=5381&loginuin=0&hostuin=0&format=json&incharset=utf8&outcharset=utf-8&notice=0&platform=yqq.json&neednewcode=0&data={"req":{"module":"cdn.srfcdndispatchserver","method":"getcdndispatch","param":{"guid":"1825194589","calltype":0,"userip":""}},"req_0":{"module":"vkey.getvkeyserver","method":"cgigetvkey","param":{"guid":"1825194589","songmid":["%s"],"songtype":[0],"uin":"0","loginflag":1,"platform":"20"}},"comm":{"uin":0,"format":"json","ct":24,"cv":0}}'''
   
  
  id=0
  music_list=[]
  url_list=[]
  for i in range(len(song_list)):
    
#    music_name.append(song_list[i]['name']+'-'+song_list[i]['singer'][0]['name'])
   
  #  print('{}.{}-{}'.format(i+1,song_list[i]['name'],song_list[i]['singer'][0]['name']))
    url_list.append(str_3 % (song_list[i]['mid']))
  #  
#    print(url_list[id-1])


    music=[]
#    print(m['songname'])
    music.append(id)
    music.append(song_list[i]['name'])
    music.append(song_list[i]['singer'][0]['name'])
    content_json=requests.get(url=url_list[id])
 
    dict_2=json.loads(content_json.text)
     
    url_ip=dict_2['req']['data']['freeflowsip'][1]
     
    purl=dict_2['req_0']['data']['midurlinfo'][0]['purl']
     
    href=url_ip+purl
    music.append(href)
    music.append(song_list[i]['mid'])
    music_list.append(music)
    id=id+1

    

  return music_list

import wx 
import time



  

def music_play(arr):
  print("双击函数已启动")
#  必须加上全局变量,否则不唱歌
  
  global mp3
  import mp3play
  path="http://m10.music.126.net/20200414222927/e509cfbb086d358e701e6bb5cfedfeaa/ymusic/0759/030f/540b/039ace5c004ebd382de4ca163145cf9b.mp3"
  path=arr[3]
  print("path:-->"+path)
  mp3 = mp3play.load(path)
  mp3.play()
def music_pyplay(arr):
  import os
  import time
  import pygame
  
  source_file_path=arr[3]
  pygame.mixer.init()
  pygame.mixer.music.load(source_file_path)
  pygame.mixer.music.set_volume(0.5) 
  pygame.mixer.music.play(-1)
  
  
class myframe(wx.frame): 
  def __init__(self): 
    wx.frame.__init__(self, none, -1, 'button example',  
        size=(1300, 700),pos=(-10,0)) 
    panel = wx.panel(self, -1) 
    panel.setsize(1300,700)
    panel.setbackgroundcolour("#d2b48c")
    
    
    
    
    self.sousuotxt = wx.textctrl(panel,pos=(0,0),size = (100,30)) 
    lbllist = ['网易', 'qq', '酷狗'] 
     
    self.rbox = wx.radiobox(panel, label = '', pos = (100,-15),size = (100,30), choices = lbllist,majordimension = 1, style = wx.ra_specify_rows) 
    self.rbox.bind(wx.evt_radiobox,self.onradiobox) 
    
    self.button = wx.button(panel, -1, "搜索", pos=(350, 0)) 
    self.bind(wx.evt_button, self.onclick, self.button) 
    
    self.mylove = wx.button(panel, -1, "同步我喜欢的音乐", pos=(500, 0)) 
    self.bind(wx.evt_button, self.dmylove, self.mylove) 
    
    
    languages = [] 
    for i in range(1000):
      languages.append(str(i))
    
    self.choice = wx.choice(panel,choices = languages,pos=(0,30))
    self.choice.setstringselection("0")
    
    
    self.button5 = wx.button(panel, -1, "本地文件", pos=(50, 30)) 
    self.bind(wx.evt_button, self.onbutton, self.button5) 
    
    self.button1 = wx.button(panel, -1, "播放", pos=(150, 30)) 
    self.bind(wx.evt_button, self.onclick1, self.button1) 
    
    self.button2 = wx.button(panel, -1, "暂停", pos=(250, 30)) 
    self.bind(wx.evt_button, self.onclick2, self.button2) 
    
    self.button3 = wx.button(panel, -1, "下载", pos=(350, 30)) 
    self.bind(wx.evt_button, self.onclick3, self.button3) 
    
    
    font = wx.font(15, wx.default, wx.fontstyle_normal, wx.normal) 

    self.music_id = wx.statictext(panel, label = "id", pos = (0,100),size=(30,500),style=wx.align_center) 
    self.music_id.setbackgroundcolour("#d2b48c")
    self.music_id.setfont(font)

    self.music_name = wx.statictext(panel, label = "歌名", pos = (30,100),size=(400,500),style=wx.align_center) 
    self.music_name.setbackgroundcolour("#d2b48c")
    self.music_name.setfont(font)                  
    
            
    
    
    languages = [] 
    for i in range(1,100):
      languages.append("第"+str(i)+"页")
    
    self.pagenum = wx.choice(panel,choices = languages,pos=(50,600)) 
    self.pagenum.setstringselection("第1页")
    self.button4 = wx.button(panel, -1, "跳转", pos=(150, 600)) 
    self.bind(wx.evt_button, self.onclick4, self.button4) 
    
    self.prepage = wx.button(panel, -1, "上一页", pos=(250, 600)) 
    self.bind(wx.evt_button, self.prepage, self.prepage) 
    
    self.nextpage = wx.button(panel, -1, "下一页", pos=(350, 600)) 
    self.bind(wx.evt_button, self.nextpage, self.nextpage) 
    
    
    
    
    self.path="d://downmusic/歌曲"
    self.music_arr=[]
    
    self.getfile()
    self.setdata()
    
    
    self.key1="本地音乐"
  def onradiobox(self,event):
    
#   print (self.rbox.getstringselection(),' is clicked from radio box' )
    
    pass
  def onclick(self, event): 
    self.rbox.getstringselection()
    print(self.rbox.getstringselection())
    rboxtext=self.rbox.getstringselection()
    if rboxtext=="网易":
      s=self.sousuotxt.getvalue()
      self.music_arr=netease_search(s)
      print(self.music_arr)
      self.setdata()
      print("搜索")
      self.key1="在线音乐"
    elif rboxtext=="qq":
      s=self.sousuotxt.getvalue()
      self.music_arr=qq_search(s)
      print(self.music_arr)
      self.setdata()
      print("搜索")
      self.key1="在线音乐"
    else:
      pass
    
    
      
      
      
    
      
      
    
    
  
    
    
    
#播放
  def onclick1(self, event): 
#    self.button1.setlabel("clicked") 
    print("播放")
    
    y=self.choice.getstringselection()
    for i in self.music_arr:
      if y==str(i[0]):
        if self.key1=="在线音乐":
        
          music_play(i)
          
        else:
          self.music_pyplay(i)
          
          
  def music_pyplay(self,arr):
    import os
    import time
    import pygame
    
    source_file_path=arr[3]
    pygame.mixer.init()
    pygame.mixer.music.load(source_file_path)
    pygame.mixer.music.set_volume(0.5) 
    pygame.mixer.music.play(-1)
    
    import os
    path="d://downmusic/歌词/"    
    import pygame
    import time
    import os
    f_url=""
    if arr[2]=="sen":
      
      f_url=path+str(arr[1])+".lrc"
    else:
      f_url=path+str(arr[1])+"-"+str(arr[2])+".lrc"
    
      
    f=open(f_url)
    s=""
    for i in f.readlines():
      if i[1]!="0":
        pass
      else:
        s=s+i
      
    print(s)
    f.close()
    strlrc=s
    
    f.close()
    dictlrc = {}
    # 对歌词进行按行切割
    linelistlrc = strlrc.splitlines()
    # 遍历每一行歌词
    geci=""
    for linelrc in linelistlrc:
      # 时间和歌词分开
      listlrc = linelrc.split("]")
      timelrc = listlrc[0][1:].split(':')
      # 转换时间格式
      times = float(timelrc[0]) * 60 + float(timelrc[1])
      # 把时间当做key,歌词当做value存放在字典中
      dictlrc[times] = listlrc[1]
      geci=geci+listlrc[1]+"\n"
    temptime = 0
    # 音频初始化
    
    
    root = tk() # 初始化tk()
    root.overrideredirect(true)#隐藏标题栏
    root.title("label-test")  # 设置窗口标题
    root.geometry("1300x800+0+0")  # 设置窗口大小 注意:是x 不是*
    root.resizable(width=true, height=true) # 设置窗口是否可以变化长/宽,false不可变,true可变,默认为true
    l = label(root, text="label", bg="pink", font=("arial",60), width=300, height=800)
    l["text"]="wusen"
    
    # 加载音频文件路径 (路径必须真实存在,音频文件格式支持mp3/ogg等格式)
    for key in dictlrc.keys():
      temptime = key - temptime
      # 判断是否在播放音乐
      if not pygame.mixer.music.get_busy():
        pygame.mixer.music.play()
      # 歌词显示的时间
      time.sleep(temptime)
      # 显示歌词
      print(dictlrc[key])
      
      temptime = key
      import time
    #  time.sleep(1)
    #  music_irc.setlabel(time.strftime("%h:%m:%s",time.localtime(time.time())))  
      print(time.strftime("%h:%m:%s",time.localtime(time.time())))
#      l["text"]=time.strftime("%h:%m:%s",time.localtime(time.time()))
      l["text"]=dictlrc[key]
      
      l.pack(side=top)
      l.update()
    root.destroy()
#    print(s)
  
#    暂停     
  def onclick2(self, event): 
#    self.button2.setlabel("clicked") 
    print("暂停")
#    下载
  def onclick3(self, event): 
    
    y=self.choice.getstringselection()
#    self.button3.setlabel("clicked") 
    print("下载")
    print(y)
    
    for i in self.music_arr:
      if y==str(i[0]):
        self.down_music(i)
#    跳转
  def down_music(self,arr):
  
    self.rbox.getstringselection()
    print(self.rbox.getstringselection())
    rboxtext=self.rbox.getstringselection()
    if rboxtext=="网易":
      headers={'user-agent':'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.130 safari/537.36'}
  
      import requests
      url=arr[3]
      name=str(arr[1])+"-"+str(arr[2])
      res=requests.get(url,headers=headers)
      print(res)
      m=res.content
      mv=open("d://downmusic/歌曲/"+name+".mp3","wb")
      mv.write(m)
      mv.close()
      #  下载歌词
      import requests
      import json
      import os
      url = 'http://music.163.com/api/song/lyric?'+ 'id=' + str(arr[4])+ '&lv=1&kv=1&tv=-1'
      r = requests.get(url,headers=headers)
      json_obj = r.text
      print(r.text)
      print(url)
      j = json.loads(json_obj)
      print(j['lrc']['lyric'])
      f=open("d://downmusic/歌词/"+name+".lrc","w")
      f.write(j['lrc']['lyric'])
      f.close()
    elif rboxtext=="qq":
      headers={'user-agent':'mozilla/5.0 (windows nt 10.0; wow64) applewebkit/537.36 (khtml, like gecko) chrome/79.0.3945.130 safari/537.36'}
  
      import requests
      url=arr[3]
      name=str(arr[1])+"-"+str(arr[2])
      res=requests.get(url,headers=headers)
      print(res)
      m=res.content
      qqmusicpath="d://downmusic/歌曲/"+name+".mp3"
      mv=open(qqmusicpath,"wb")
      mv.write(m)
      mv.close()
        
        
        
      import os
      s="e://python/ffmpeg-20200403-52523b6-win64-static/bin/ffmpeg -i d://downmusic/歌曲/a.mp3 "+qqmusicpath
      os.system(s)
      os.remove("d://downmusic/歌曲/a.mp3")
      #  下载歌词
      pass
    else:
      pass
  def onclick4(self, event): 
      
    self.setdata()
    
  
    
#刷新页面赋值内容
  def setdata(self):
    y=self.pagenum.getstringselection()
#    self.button4.setlabel("clicked") 
    print("跳转")
    print(y)
    id=""
    name=""
    singer=""
    
    for i in self.music_arr[(int(y[1:-1])-1)*10:(int(y[1:-1])-1)*10+10]:
      id=id+str(i[0])+"\n"+"----\n"
      name=name+str(i[1])+"-"+str(i[2])+"\n"+"------------------------------------------------------------------------------------------\n"
#      singer=singer+str(i[2])+"\n"+"----------\n"
      
    self.music_id.setlabel(id)
    self.music_name.setlabel(name)
#    self.music_singer.setlabel(singer)

  def getfile(self):
    self.music_arr.clear()
    import os
    
    local_url=self.path+"/"
  
    if os.path.isdir(local_url):
      pass
    else:
      os.mkdir(local_url)
      print("路径已创建")
    file = os.listdir(self.path)
    
    j=0
    for i in file:
      
      k=[]
      music_name=i[:-4].split("-", 1)[0]
      try:
        
        music_singer=i[:-4].split("-", 1)[1]
      except:
        music_singer="sen"
      music_id="0"
      music_edition="本地歌曲"
      music_url=self.path+"/"+i
      k.append(j)
      k.append(music_name)
      k.append(music_singer)
      k.append(music_url)
      k.append(music_edition)
      print(j)
      print(music_name)
      print(music_singer)
      print(music_url)
      print(music_edition)
      print("----------")
      self.music_arr.append(k)
      j=j+1
    self.key1="本地音乐"
#    选择本地文件
  def onbutton(self, event):
    """"""
    dlg = wx.dirdialog(self,u"选择文件夹",style=wx.dd_default_style)
    if dlg.showmodal() == wx.id_ok:
      print(dlg.getpath()) #文件夹路径
      self.path=dlg.getpath()
    dlg.destroy()
    
    self.getfile()
    self.setdata()
#    下一页
  def nextpage(self,event):
    y=self.pagenum.getstringselection()
    self.pagenum.setstringselection("第"+str(int(y[1:-1])+1)+"页")
    self.setdata()
#    上一页
  def prepage(self,event):
    y=self.pagenum.getstringselection()
    self.pagenum.setstringselection("第"+str(int(y[1:-1])-1)+"页")
    self.setdata()
    
    
  def dmylove(self,event):
#    同步网易云音乐
    
    import requests
    from bs4 import beautifulsoup
    import urllib.request
    from lxml import html
    etree = html.etree
    #这里是设置请求头
    headers = {
      'referer': 'http://music.163.com/',
      'host': 'music.163.com',
      'user-agent': 'mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/62.0.3202.75 safari/537.36',
      'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    }
     
    # 歌单的url地址这里改id
    play_url = 'http://music.163.com/playlist?id=587771511'
    s = requests.session()
    response = s.get(play_url, headers=headers).content
    # 使用bs4匹配出对应的歌曲名称和地址
    s = beautifulsoup(response, 'lxml')
    main = s.find('ul', {'class': 'f-hide'})
    #print(main.find_all('a'))
    lists = []
    for music in main.find_all('a'):
      list = []
      # print('{} : {}'.format(music.text, music['href']))
      musicurl = 'http://music.163.com/song/media/outer/url' + music['href'][5:] + '.mp3'
      musicname = music.text
      # 单首歌曲的名字和地址放在list列表中
      list.append(musicname)
      list.append(musicurl)
      
      list.append(music['href'][9:])
      # 全部歌曲信息放在lists列表中
      lists.append(list)
    # 下载列表中的全部歌曲,并以歌曲名命名下载后的文件,文件位置为当前文件夹
    for i in lists:
      url = i[1]
      name = i[0]
      try:
        import os
        if os.path.exists("d:/downmusic/歌曲/"+name+".mp3"):
          pass
        else:
          
        
          print('正在下载', name)
          #这里修改路径,随便指定盘符,但是得存在
          urllib.request.urlretrieve(url, 'd:/downmusic/歌曲/%s.mp3' % name)
          import requests
          import json
          
          
          url = 'http://music.163.com/api/song/lyric?'+ 'id=' + str(i[2])+ '&lv=1&kv=1&tv=-1'
          r = requests.get(url,headers=headers)
          json_obj = r.text
          print(r.text)
          print(url)
          j = json.loads(json_obj)
          print(j['lrc']['lyric'])
          f=open("d://downmusic/歌词/"+name+".lrc","w")
          f.write(j['lrc']['lyric'])
          f.close()
          print('下载成功')
      except:
        print('下载失败')
if __name__ == '__main__': 
  app = wx.pysimpleapp() 
  frame = myframe() 
  
  frame.show() 
  
  
  app.mainloop() 
  
  
  
  
  

  

  
  

#
#pyinstaller -d -w wx音乐播放器.py -p d:/anaconda/lib/site-packages
#  pyinstaller -d wx音乐播放器.py -p d:/anaconda/lib/site-packages
#  
#pyinstaller -d wx音乐播放器.py

以上就是python 基于wx实现音乐播放的详细内容,更多关于python 音乐播放的资料请关注其它相关文章!