Skip to content

Instantly share code, notes, and snippets.

@AdamSpannbauer
Created January 19, 2018 16:48
Show Gist options
  • Save AdamSpannbauer/3f4cb44417d8865cbb33dcd69243e5fd to your computer and use it in GitHub Desktop.
Save AdamSpannbauer/3f4cb44417d8865cbb33dcd69243e5fd to your computer and use it in GitHub Desktop.
example pytube function for downloading youtube video
import os
import re
from pytube import YouTube
def downloadYtMp4(ytURL, dlDir=os.getcwd()):
#find youtube video
yt = YouTube(ytURL)
#
#filter to only mp4 files and take last in list (sorted lowest to highest quality)
hqMp4 = yt.filter('mp4')[-1]
#strip quality from video info.. example video info:
m = re.search("- (\d\d\dp) -", str(hqMp4))
#save quality capturing group
quality = m.group(1)
#
#get mp4 video with highest quality found
video = yt.get('mp4', quality)
#download and save video to specified dir
video.download(dlDir)
downloadYtMp4("https://www.youtube.com/watch?v=MvFcY9rTPx8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment