Skip to content

Instantly share code, notes, and snippets.

@edgartaor
Created January 22, 2016 02:14
Show Gist options
  • Save edgartaor/5c4217f3bc086bf60697 to your computer and use it in GitHub Desktop.
Save edgartaor/5c4217f3bc086bf60697 to your computer and use it in GitHub Desktop.
Download Youtube audio as mp3 in a specific directory within artist subfolder (if possible)
__author__ = 'EdgarT'
from __future__ import unicode_literals
import youtube_dl
import sys
class MyLogger(object):
def debug(self, msg):
print(msg)
def warning(self, msg):
print(msg)
def error(self, msg):
print(msg)
def my_hook(d):
if d['status'] == 'finished':
print('Descarga termindada, convirtiendo a mp3...')
def main():
url = ''
BASE_DIR = 'A:\\Music\\'
if len(sys.argv)> 1:
url= sys.argv[1]
else:
print('Especifica una URL de un video de Youtube')
sys.exit(1)
ydl_opts = {
'outtmpl' : BASE_DIR + '%(title)s.%(ext)s',
'writethumbnail': True,
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
},
{
'key': 'EmbedThumbnail',
},
{
'key': 'MetadataFromTitle',
'titleformat' : '%(artist)s - %(title)s'
},
{
'key': 'FFmpegMetadata',
},
],
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
info = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False)
artist = None
artist = info.get('title').split('-',1)[0].strip()
if artist is not None:
ydl_opts['outtmpl'] = BASE_DIR + artist + '\\%(title)s.%(ext)s'
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print(info.get('title'))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment