music.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from __future__ import unicode_literals
  2. import youtube_dl
  3. from wbb.utils import cust_filter
  4. from pyrogram import filters
  5. from wbb import app, Command
  6. import os
  7. import glob
  8. ydl_opts = {
  9. 'format': 'bestaudio/best',
  10. 'postprocessors': [{
  11. 'key': 'FFmpegExtractAudio',
  12. 'preferredcodec': 'mp3',
  13. 'preferredquality': '320'
  14. }],
  15. 'postprocessor_args': [
  16. '-ar', '16000'
  17. ],
  18. 'prefer_ffmpeg': True,
  19. 'keepvideo': False
  20. }
  21. @app.on_message(cust_filter.command(commands=(["music"])))
  22. async def commit(client, message):
  23. app.set_parse_mode("markdown")
  24. await message.reply_chat_action("upload_audio")
  25. link = (message.text.split(None, 1)[1])
  26. with youtube_dl.YoutubeDL(ydl_opts) as ydl:
  27. ydl.download([link])
  28. for filename in glob.glob("*.mp3"):
  29. filename_final = filename.split("-")[0]+" - "+filename.split("-")[-2]+".mp3"
  30. os.rename(filename, filename_final)
  31. m = await message.reply_audio(filename_final)
  32. await m.edit_caption(f"[{filename_final}]({link})")
  33. os.remove(filename_final)