Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1.  
  2. import pymumble.pymumble_py3 as pymumble
  3. import subprocess as sp
  4. import time
  5. import audioop
  6. import pafy.pafy as pafy
  7. import os
  8. #import http.client
  9. #import urllib.request, urllib.error, urllib.parse
  10.  
  11.  
  12. bot_host = 'mumble.kafnasetpo.re'
  13. bot_user = "Jukebox"
  14. bot_port = 64738
  15. bot_password = 'masters'
  16. bot_reconnect = False
  17. bot_debug = False
  18.  
  19. class bot:
  20.  
  21. def __init__(self):
  22. self.bot = pymumble.Mumble(host = bot_host, user = bot_user, port = bot_port, password = bot_password,
  23. reconnect = bot_reconnect, debug = bot_debug)
  24. self.bot.start()
  25. self.bot.is_ready()
  26. self.bot.set_codec_profile("audio")
  27. self.bot.callbacks.set_callback("text_received", self.message_recu)
  28. self.exit = False
  29. self.playing = False
  30. self.volume = 0.15
  31. self.channel = 'Dev'
  32. self.loop()
  33.  
  34. if self.channel:
  35. self.bot.channels.find_by_name(self.channel).move_in()
  36.  
  37. def message_recu(self, objet_text):
  38. message = objet_text.message.split(' ')
  39. if message[0] == "!play":
  40. self.player()
  41. if message[0] == "!stop":
  42. self.playing = False
  43. if message[0] == "!stream":
  44. url = message[1]
  45. stream = pafy.new(url)
  46. audio = stream.getbestaudio()
  47. try :
  48. audio.download()
  49. #fichier =
  50. finally :
  51. self.stream()
  52. else :
  53. return
  54.  
  55. def player(self):
  56. path = '/home/hmg/PycharmProjects/Mumble_bot/'
  57. fichier = 'Wiz Khalifa - "Paperbond".opus'
  58. url = 'https://www.youtube.com/watch?v=jOMXMmKg74Y'
  59. stream = pafy.new(url)
  60. audio = stream.getbestaudio()
  61. #musique = path + fichier
  62. ffmpeg_debug = "warning"
  63. command = ["ffmpeg", '-v', ffmpeg_debug, '-nostdin', '-i', audio, '-ac', '1', '-f', 's16le', '-ar', '48000', '-']
  64. self.thread = sp.Popen(command, stdout=sp.PIPE, bufsize=480)
  65. self.playing = True
  66. #self.url = 'https://www.youtube.com/watch?v=TNjE4m7Xlvk'
  67.  
  68. def stream(self):
  69. url = 'https://www.youtube.com/watch?v=TNjE4m7Xlvk'
  70. ffmpeg_debug = "warning"
  71. command = ["ffmpeg", '-v', ffmpeg_debug, '-nostdin', '-i', url, '-ac', '1', '-f', 's16le', '-ar', '48000', '-']
  72. self.thread = sp.Popen(command, stdout=sp.PIPE, bufsize=480)
  73. time.sleep(3)
  74. self.playing = True
  75.  
  76. def loop(self):
  77. while not self.exit:
  78. if self.playing:
  79. while self.bot.sound_output.get_buffer_size() > 0.5 and self.playing:
  80. time.sleep(0.01)
  81. music = self.thread.stdout.read(480)
  82. if music:
  83. self.bot.sound_output.add_sound(audioop.mul(music, 2, self.volume))
  84. else:
  85. time.sleep(0.01)
  86. else:
  87. time.sleep(1)
  88. while self.bot.sound_output.get_buffer_size() > 0:
  89. time.sleep(0.01)
  90. time.sleep(0.5)
  91.  
  92.  
  93. mumble_music = bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement