audio extraction capabilities

This commit is contained in:
Tomáš Mládek 2018-02-02 16:03:25 +01:00 committed by Tomáš Mládek
parent 2cfacc76b5
commit de6fc16044

View file

@ -46,13 +46,19 @@ def ytdl_has(url):
return False return False
def download_ydl(urls, subdir, date): def download_ydl(urls, subdir, date, extract=False):
ydl_opts = { ydl_opts = {
'noplaylist': True, 'noplaylist': True,
'restrictfilenames': True, 'restrictfilenames': True,
'download_archive': DIR + '/downloaded.lst', 'download_archive': DIR + '/downloaded.lst',
'outtmpl': f'{TMP_DIR}/' + datestr(date) + '__%(title)s__%(id)s.%(ext)s' '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: with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(urls) ydl.download(urls)
out_dir = f'{OUT_DIR}/{subdir}/' out_dir = f'{OUT_DIR}/{subdir}/'
@ -98,10 +104,17 @@ def handle_url(bot, update):
try: try:
logger.info("Downloading %s" % ytdl_urls) logger.info("Downloading %s" % ytdl_urls)
hashtag = get_first_hashtag(update.message) hashtag = get_first_hashtag(update.message)
reply = 'Downloading'
if hashtag: if hashtag:
mkdir_p(f'{OUT_DIR}/{hashtag}') mkdir_p(f'{OUT_DIR}/{hashtag}')
update.message.reply_text('Downloading%s...' % f' to "{hashtag}"' if hashtag else '') reply += f' to "{hashtag}"'
download_ydl(ytdl_urls, hashtag or '.', update.message.date) 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: except:
type, value, _ = sys.exc_info() type, value, _ = sys.exc_info()
update.message.reply_text("Something is FUCKED: %s, %s" % (type, value)) update.message.reply_text("Something is FUCKED: %s, %s" % (type, value))