From de6fc16044d59c0a665492c05b8f81999ed9a06d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Fri, 2 Feb 2018 16:03:25 +0100 Subject: [PATCH] audio extraction capabilities --- robot.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/robot.py b/robot.py index 966ba39..08120b4 100755 --- a/robot.py +++ b/robot.py @@ -46,13 +46,19 @@ def ytdl_has(url): return False -def download_ydl(urls, subdir, date): +def download_ydl(urls, subdir, date, extract=False): ydl_opts = { 'noplaylist': True, 'restrictfilenames': True, 'download_archive': DIR + '/downloaded.lst', 'outtmpl': f'{TMP_DIR}/' + datestr(date) + '__%(title)s__%(id)s.%(ext)s' } + if extract: + ydl_opts['format'] = 'bestaudio' + ydl_opts['postprocessors'] = [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'wav' + }] with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download(urls) out_dir = f'{OUT_DIR}/{subdir}/' @@ -98,10 +104,17 @@ def handle_url(bot, update): try: logger.info("Downloading %s" % ytdl_urls) hashtag = get_first_hashtag(update.message) + + reply = 'Downloading' if hashtag: mkdir_p(f'{OUT_DIR}/{hashtag}') - update.message.reply_text('Downloading%s...' % f' to "{hashtag}"' if hashtag else '') - download_ydl(ytdl_urls, hashtag or '.', update.message.date) + reply += f' to "{hashtag}"' + reply += '...' + + if hashtag == 'AUDIO': + reply += ' (And also guessing you want to extract the audio)' + update.message.reply_text(reply) + download_ydl(ytdl_urls, hashtag or '.', update.message.date, extract=hashtag == 'AUDIO') except: type, value, _ = sys.exc_info() update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))