Skip to content

Instantly share code, notes, and snippets.

@andygock
Last active June 2, 2017 15:52
Show Gist options
  • Save andygock/d785e940c8da27d82fcbf9c7d692988b to your computer and use it in GitHub Desktop.
Save andygock/d785e940c8da27d82fcbf9c7d692988b to your computer and use it in GitHub Desktop.
Rip album of AAC audio from Youtube playlist with youtube-dl
#/usr/bin/env python
"""
Prerequisites:
* python
* youtube-dl https://rg3.github.io/youtube-dl/
* (not required yet) mutagen (pip install mutagen)
Usage examples:
Save in format '%(uploader)s/%(playlist_title)s/%(title)s.%(ext)s':
yt-rip.py "URL"
Save in format '%(uploader)s/%(playlist_title)s/(%(autonumber)s) %(title)s.%(ext)s':
yt-rip.py --track "URL"
Save in format '%(uploader)s/%(playlist_title)s/(%(autonumber)s) %(title)s.%(ext)s':
yt-rip.py --autonumber --select "1,2,5-10,12,14" "URL"
"""
import subprocess
import argparse
parser = argparse.ArgumentParser(description='youtube-dl invoker')
parser.add_argument('urls', metavar='URL', type=str, nargs='+',
help='URL of video or playlist')
parser.add_argument('--debug', action='store_true', help='debugging mode')
parser.add_argument('--tracks', action='store_true', help='place track number in filename (uses playlist index number)')
parser.add_argument('--autonumber', action='store_true', help='place track number in filename (auto increment number starting from 1)')
parser.add_argument('--select', nargs=1, help='select playlist items')
args = parser.parse_args()
# print args
url = args.urls
options = []
command = []
command.append('youtube-dl.exe')
command.append('-i')
command.append('-f 22')
command.append('-x')
command.append('--no-overwrites')
# command.append('-q')
for o in options:
command.append(o)
if args.select:
command.append('--playlist-items')
command.append(args.select[0])
if args.tracks:
command.append('-o')
command.append('%(uploader)s/%(playlist_title)s/(%(playlist_index)s) %(title)s.%(ext)s')
elif args.autonumber:
command.append('-o')
command.append('%(uploader)s/%(playlist_title)s/(%(autonumber)s) %(title)s.%(ext)s')
else:
command.append('-o')
command.append('%(uploader)s/%(playlist_title)s/%(title)s.%(ext)s')
for address in url:
command.append(address)
if args.debug:
print(command)
else:
subprocess.call(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment