Check if a file exists, read metadata. Closes #15

This commit is contained in:
Lartza 2018-06-19 14:27:28 +03:00
parent e538c3c9fb
commit f7ab210b8a
2 changed files with 30 additions and 23 deletions

View File

@ -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 = ""

View File

@ -4,3 +4,4 @@ flask
youtube-dl
python-magic
Pillow
mutagen