Created
September 1, 2015 09:13
-
-
Save mertsalik/447e5f63468b12727064 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pafy | |
import re | |
_slugify_strip_re = re.compile(r'[^\w\s-]') | |
_slugify_hyphenate_re = re.compile(r'[-\s]+') | |
def _slugify(value): | |
""" | |
Normalizes string, converts to lowercase, removes non-alpha characters, | |
and converts spaces to hyphens. | |
From Django's "django/template/defaultfilters.py". | |
""" | |
import unicodedata | |
if not isinstance(value, unicode): | |
value = unicode(value) | |
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') | |
value = unicode(_slugify_strip_re.sub('', value).strip().lower()) | |
return _slugify_hyphenate_re.sub('-', value) | |
url = "https://www.youtube.com/watch?v=_FE194VN6c4" | |
video = pafy.new(url) | |
print video.title | |
streams = video.streams | |
print "Available video streams :" | |
for s in streams: | |
print(s.resolution, s.extension) | |
print "Available audio streams :" | |
audiostreams = video.audiostreams | |
for a in audiostreams: | |
print(a.bitrate, a.extension, a.get_filesize()) | |
bestaudio = video.getbestaudio() | |
filename = _slugify(video.title) +"."+ bestaudio.extension | |
print filename | |
bestaudio.download(quiet=False,filepath=filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment