Skip to content

Instantly share code, notes, and snippets.

@sdushantha
Created May 22, 2021 10:53
Show Gist options
  • Save sdushantha/b935778c444c069e2f487059178b2fba to your computer and use it in GitHub Desktop.
Save sdushantha/b935778c444c069e2f487059178b2fba to your computer and use it in GitHub Desktop.
Very short YouTube downloader. Extraction of video is done through code, no external modules.
#!/usr/bin/env python3
#
# by Siddharth Dushantha (sdushantha)
#
# My very simple version of youtube-dl.
#
# Credits: https://git.io/JTPr9
#
import re
import json
import argparse
import requests
def download(url, filename):
r = requests.get(url)
html = r.text
data = re.findall(r";ytplayer\.config = (.*);ytplayer\.web_player_context_config", html)[0]
player_response = json.loads(data).get("args").get("player_response")
videoFormats = json.loads(player_response).get("streamingData").get("formats")
bitrates = [videoFormat.get("bitrate") for videoFormat in videoFormats]
bestQualityIndex = bitrates.index(max(bitrates))
videoStreamURL = videoFormats[bestQualityIndex].get("url")
r = requests.get(videoStreamURL)
rawData = r.content
with open(filename, "wb") as f:
f.write(rawData)
print(f"Finished downloading {filename}")
def main():
parser = argparse.ArgumentParser(description="YouTube Downloader")
parser.add_argument("url", help="URL to the YouTube video")
parser.add_argument("filename", help="Name you want the file to be saved as")
args = parser.parse_args()
download(args.url, args.filename)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment