TheHamkerCat hace 5 años
padre
commit
cbd451415f
Se han modificado 2 ficheros con 28 adiciones y 6 borrados
  1. 17 4
      wbb/modules/music.py
  2. 11 2
      wbb/utils/inlinefuncs.py

+ 17 - 4
wbb/modules/music.py

@@ -46,9 +46,15 @@ __HELP__ = """/ytmusic [link] To Download Music From Various Websites Including
 is_downloading = False
 
 
+def get_file_extension_from_url(url):
+    url_path = urlparse(url).path
+    basename = os.path.basename(url_path)
+    return basename.split(".")[-1]
+
+
 async def download_youtube_audio(url: str, m = 0):
     global is_downloading
-    with youtube_dl.YoutubeDL({'format': 'bestaudio'}) as ydl:
+    with youtube_dl.YoutubeDL({'format': 'bestaudio', 'writethumbnail': True,}) as ydl:
         info_dict = ydl.extract_info(url, download=False)
         if int(float(info_dict['duration'])) > 600:
             if m != 0:
@@ -64,10 +70,13 @@ async def download_youtube_audio(url: str, m = 0):
                                             codec="copy", loglevel='error').overwrite_output().run()
             os.remove(audio_file)
             audio_file = audio_file_opus
+        thumbnail_url = info_dict['thumbnail']
+        thumbnail_file = basename + "." + \
+            get_file_extension_from_url(thumbnail_url)
         title = info_dict['title']
         performer = info_dict['uploader']
         duration = int(float(info_dict['duration']))
-    return [title, performer, duration, audio_file]
+    return [title, performer, duration, audio_file, thumbnail_file]
 
 
 @app.on_message(filters.command("ytmusic") & ~filters.edited & filters.user(SUDOERS))
@@ -85,14 +94,18 @@ async def music(_, message):
     m = await message.reply_text(f"Downloading {url}",
                                  disable_web_page_preview=True)
     try:
-        title, performer, duration, audio_file = await download_youtube_audio(url, message)
+        title, performer, duration, audio_file, thumbnail_file = await download_youtube_audio(url, message)
     except Exception as e:
         is_downloading = False
         await m.edit(str(e))
         return
-    await message.reply_audio(audio_file, duration=duration, performer=performer, title=title)
+    await message.reply_audio(
+            audio_file, duration=duration,
+            performer=performer, title=title, thumb=thumbnail_file
+            )
     await m.delete()
     os.remove(audio_file)
+    os.remove(thumbnail_file)
     is_downloading = False
 
 

+ 11 - 2
wbb/utils/inlinefuncs.py

@@ -839,8 +839,17 @@ async def yt_music_func(answers, url, user_id):
                 input_message_content=InputTextMessageContent(msg)
             ))
         return answers
-    title, performer, duration, audio_file = await download_youtube_audio(url)
-    m = await app.send_audio(MESSAGE_DUMP_CHAT, audio_file)
+    title, performer, duration, audio, thumbnail = await download_youtube_audio(url)
+    m = await app.send_audio(
+            MESSAGE_DUMP_CHAT,
+            audio_file,
+            title=title,
+            duration=duration,
+            performer=performer,
+            thumb=thumbnail
+            )
+    os.remove(audio)
+    os.remove(thumbnail)
     answers.append(
         InlineQueryResultCachedDocument(
             title=title,