diff --git a/mumbleBot.py b/mumbleBot.py index 71f68a0..7abec0f 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -20,6 +20,7 @@ import util import base64 from PIL import Image from io import BytesIO +from mutagen.easyid3 import EasyID3 class MumbleBot: @@ -321,29 +322,34 @@ class MumbleBot: def download_music(self, url): url_hash = hashlib.md5(url.encode()).hexdigest() path = var.config.get('bot', 'tmp_folder') + url_hash + ".%(ext)s" - ydl_opts = { - 'format': 'bestaudio/best', - 'outtmpl': path, - 'noplaylist': True, - 'writethumbnail': True, - 'updatetime': False, - 'postprocessors': [{ - 'key': 'FFmpegExtractAudio', - 'preferredcodec': 'mp3', - 'preferredquality': '192'}, - {'key': 'FFmpegMetadata'}] - } - video_title = "" - with youtube_dl.YoutubeDL(ydl_opts) as ydl: - for i in range(2): - try: - info_dict = ydl.extract_info(url) - video_title = info_dict['title'] - except youtube_dl.utils.DownloadError: - pass - else: - break - return path.replace(".%(ext)s", ".mp3"), video_title + mp3 = path.replace(".%(ext)s", ".mp3") + if os.path.isfile(mp3): + audio = EasyID3(mp3) + video_title = audio["title"] + else: + ydl_opts = { + 'format': 'bestaudio/best', + 'outtmpl': path, + 'noplaylist': True, + 'writethumbnail': True, + 'updatetime': False, + 'postprocessors': [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192'}, + {'key': 'FFmpegMetadata'}] + } + video_title = "" + with youtube_dl.YoutubeDL(ydl_opts) as ydl: + for i in range(2): + try: + info_dict = ydl.extract_info(url) + video_title = info_dict['title'] + except youtube_dl.utils.DownloadError: + pass + else: + break + return mp3, video_title def loop(self): raw_music = "" diff --git a/requirements.txt b/requirements.txt index 155681c..0315e18 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ flask youtube-dl python-magic Pillow +mutagen