From f7e9e806e09ddf64c1c4cbdeabf2fc72582c8382 Mon Sep 17 00:00:00 2001 From: Lartza Date: Tue, 19 Jun 2018 14:02:41 +0300 Subject: [PATCH 1/4] Fix youtube-dl processing --- mumbleBot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mumbleBot.py b/mumbleBot.py index f716a90..e4d05ef 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -320,7 +320,7 @@ class MumbleBot: def download_music(self, url): url_hash = hashlib.md5(url.encode()).hexdigest() - path = var.config.get('bot', 'tmp_folder') + url_hash + ".mp3" + path = var.config.get('bot', 'tmp_folder') + url_hash + ".%(ext)s" ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': path, @@ -343,7 +343,7 @@ class MumbleBot: pass else: break - return path, video_title + return path.replace(".%(ext)s", ".mp3"), video_title def loop(self): raw_music = "" From e538c3c9fb7e47d69795c5dcec6071053f6d23a2 Mon Sep 17 00:00:00 2001 From: Lartza Date: Tue, 19 Jun 2018 14:10:31 +0300 Subject: [PATCH 2/4] Make youtube-dl write metadata --- mumbleBot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mumbleBot.py b/mumbleBot.py index e4d05ef..71f68a0 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -330,8 +330,8 @@ class MumbleBot: 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', - 'preferredquality': '192', - }] + 'preferredquality': '192'}, + {'key': 'FFmpegMetadata'}] } video_title = "" with youtube_dl.YoutubeDL(ydl_opts) as ydl: From f7ab210b8a8a73d8e6f5f879cb87f0e9023f3d9f Mon Sep 17 00:00:00 2001 From: Lartza Date: Tue, 19 Jun 2018 14:27:28 +0300 Subject: [PATCH 3/4] Check if a file exists, read metadata. Closes #15 --- mumbleBot.py | 52 +++++++++++++++++++++++++++--------------------- requirements.txt | 1 + 2 files changed, 30 insertions(+), 23 deletions(-) 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 From 6edec5e0ec70b3085b4ae089357fc2dbbf62080a Mon Sep 17 00:00:00 2001 From: Lartza Date: Tue, 19 Jun 2018 14:33:48 +0300 Subject: [PATCH 4/4] Most tags are lists --- mumbleBot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mumbleBot.py b/mumbleBot.py index 7abec0f..2ac90f2 100644 --- a/mumbleBot.py +++ b/mumbleBot.py @@ -325,7 +325,7 @@ class MumbleBot: mp3 = path.replace(".%(ext)s", ".mp3") if os.path.isfile(mp3): audio = EasyID3(mp3) - video_title = audio["title"] + video_title = audio["title"][0] else: ydl_opts = { 'format': 'bestaudio/best',