Skip to content

Instantly share code, notes, and snippets.

@ivanleoncz
Forked from benzap/youtube2mp3.py
Last active November 25, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanleoncz/a6ba98f0bc5183bfdfbef0791e43bc4d to your computer and use it in GitHub Desktop.
Save ivanleoncz/a6ba98f0bc5183bfdfbef0791e43bc4d to your computer and use it in GitHub Desktop.
Youtube to MP3 Downloader Script
#!/usr/bin/python3
#
# Requires: youtube_dl module
# Requires: ffmpeg
# Usage:
#
# python youtube2mp3.py <URL>, ...
#
# Example:
#
# python youtube2mp3.py https://www.youtube.com/watch?v=dQw4w9WgXcQ
import glob
import subprocess as sp
import sys
import youtube_dl
default_dir="~/Music"
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
if __name__ == "__main__":
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
filenames = sys.argv[1:]
ydl.download(filenames)
while True:
move = input("\nMove all .mp3 files to {0} (y/n) ? ".format(default_dir))
if move == "y":
files = glob.glob("*.mp3")
for f in files:
sp.call(["mv", f, default_dir])
break
elif move == "n":
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment