Skip to content

Instantly share code, notes, and snippets.

@kstohr
Last active October 16, 2017 23:47
Show Gist options
  • Save kstohr/d122d7bfbd53798b3ce5a34a24d62ab4 to your computer and use it in GitHub Desktop.
Save kstohr/d122d7bfbd53798b3ce5a34a24d62ab4 to your computer and use it in GitHub Desktop.
Download video and extract audio using Youtube-DL
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
#job = get_current_job()
#job.meta['progress'] = msg
#job.save()
print(msg)
class DownloadConvert(object):
"""
Uses Youtube.dl to download video file and convert and extract audio file.
"""
def __init__(self, file_input, file_output, audio_codec, logger=MyLogger()):
self.file_input = file_input
self.file_output = file_output
self.audio_codec = audio_codec
self.ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': self.file_output,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': self.audio_codec,
'preferredquality': '5',
}],
'logger': logger,
'progress_hooks': [self.done_hook],
}
#for debugging
def progress_hook(self, download):
if download['status'] == 'downloading':
sys.stdout.write(str(download['eta']))
sys.stdout.flush()
def done_hook(self, download):
if download['status'] == 'finished':
print('Done downloading, now converting ...')
def download_convert_file(self):
with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
ydl.download([self.file_input])
print('File conversion done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment