bw_mutley

SDYTDL

Oct 28th, 2020
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. # by Siddharth Dushantha (sdushantha)
  4. #
  5. # My very simple version of youtube-dl.
  6. #
  7. # Credits: https://git.io/JTPr9
  8. #
  9.  
  10. import re
  11. import json
  12. import argparse
  13. import requests
  14.  
  15. def download(url, filename):
  16. r = requests.get(url)
  17. html = r.text
  18.  
  19. data = re.findall(r";ytplayer\.config = (.*);ytplayer\.web_player_context_config", html)[0]
  20.  
  21. player_response = json.loads(data).get("args").get("player_response")
  22. videoFormats = json.loads(player_response).get("streamingData").get("formats")
  23.  
  24. bitrates = [videoFormat.get("bitrate") for videoFormat in videoFormats]
  25. bestQualityIndex = bitrates.index(max(bitrates))
  26.  
  27. videoStreamURL = videoFormats[bestQualityIndex].get("url")
  28.  
  29. r = requests.get(videoStreamURL)
  30. rawData = r.content
  31.  
  32. with open(filename, "wb") as f:
  33. f.write(rawData)
  34.  
  35. print(f"Finished downloading {filename}")
  36.  
  37. def main():
  38. parser = argparse.ArgumentParser(description="YouTube Downloader")
  39. parser.add_argument("url", help="URL to the YouTube video")
  40. parser.add_argument("filename", help="Name you want the file to be saved as")
  41. args = parser.parse_args()
  42.  
  43. download(args.url, args.filename)
  44.  
  45.  
  46. if __name__ == "__main__":
  47. main()
Add Comment
Please, Sign In to add comment