Last active
November 27, 2024 11:29
-
-
Save smellslikeml/3ab14dcbd4c7961ca9f0e02f6464c6e3 to your computer and use it in GitHub Desktop.
download youtube videos to .mp4 with command line argument search string
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
#!/usr/bin/env python | |
import os | |
import sys | |
import requests | |
from bs4 import BeautifulSoup as bs | |
from urllib.parse import urlencode | |
from pytube import YouTube | |
qstring = sys.argv[1] | |
out_dir = sys.argv[2] | |
base = "https://www.youtube.com/results?" | |
s = {"search_query": qstring} | |
s = urlencode(s) | |
r = requests.get(base + s) | |
page = r.text | |
soup=bs(page,'html.parser') | |
vids = soup.findAll('a',attrs={'class':'yt-uix-tile-link'}) | |
videolist=[] | |
for v in vids: | |
tmp = 'https://www.youtube.com' + v['href'] | |
videolist.append(tmp) | |
count=0 | |
os.chdir(out_dir) | |
for item in videolist: | |
# increment counter: | |
count+=1 | |
try: | |
yt = YouTube(item) | |
# grab the video: | |
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download() | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment