Advertisement
metalni

[self_coded py script] yt_download

Jun 18th, 2021 (edited)
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.14 KB | None | 0 0
  1. """
  2.  
  3.  
  4. With this python script you can download youtube videos in m4a format.
  5.  
  6. You must have python installed to be able to use this script.
  7.  
  8. Usage: python3 yt_download.py
  9.  
  10. Features:
  11.  
  12.    Download a single song
  13.    Download a whole playlist
  14.    Download a whole album
  15.  
  16. You can either download in:
  17.  
  18.    Default location: "C:\Music" for Windows, "/home/Music" for Linux
  19.    Custom location(You can input custom absolute path)
  20.  
  21. To be able to start using this script, you have to first edit line 7 with your youtube data api key, you can read more about it here: https://developers.google.com/youtube/v3/getting-started
  22.  
  23. Note for Linux users: Run the script in root mode, for example sudo python3 yt_download.py
  24.  
  25. Note: After you use the script for the first time, feel free to comment out lines 13 & 14 for faster execution. This is not a necessary action, you can completely ignore this note if you feel lazy to do it.
  26.  
  27. Note: If you get HTTP Error 404 or 403 error code, it's nothing to worry about, just re-run the script.
  28.  
  29. """
  30.  
  31. import os
  32. import sys
  33. import subprocess
  34. import platform
  35.  
  36.  
  37. # Install dependency packages function
  38. def install(package):
  39.     subprocess.check_call([sys.executable, "-m", "pip", "install", package])
  40.  
  41.  
  42. # Install pafy package
  43. install('pafy')
  44. install('youtube-dl')
  45. import pafy
  46.  
  47. # Set yt_api_key
  48. api_key = "Add your api key here"
  49. pafy.set_api_key(api_key)
  50.  
  51.  
  52. # Handle download location
  53. def down_location(type, playlist_name=""):
  54.     def_path = None
  55.     current_os = platform.system()
  56.     if type == 1:
  57.         if current_os == "Windows":
  58.             def_path = "C:\Music\Single songs"
  59.         else:
  60.             def_path = "/home/Music/Single songs"
  61.     elif type == 2:
  62.         if current_os == "Windows":
  63.             def_path = "C:\Music\Playlists"
  64.         else:
  65.             def_path = "/home/Music/Playlists"
  66.     elif type == 3:
  67.         if current_os == "Windows":
  68.             def_path = "C:\Music\Albums"
  69.         else:
  70.             def_path = "/home/Music/Albums"
  71.  
  72.     print("Use default download location or a custom one? (Just type 1 or 2)")
  73.     print("1. Default location")
  74.     print("2. Custom location")
  75.     location = int(input())
  76.     if location == 1:
  77.         if not os.path.exists(def_path):
  78.             os.makedirs(def_path)
  79.         os.chdir(def_path)
  80.  
  81.         if playlist_name != "":
  82.             if not os.path.exists(playlist_name):
  83.                 os.mkdir(playlist_name)
  84.             os.chdir(playlist_name)
  85.     elif location == 2:
  86.         custom_dir = input("Enter absolute path of custom directory \n")
  87.         os.chdir(custom_dir)
  88.     else:
  89.         print("Invalid input")
  90.  
  91.  
  92. def down_playlist(down_type):
  93.     url = input("Enter playlist url: \n")
  94.     playlist = pafy.get_playlist2(url)
  95.     down_location(down_type, playlist.title)
  96.  
  97.     curr_song = 1
  98.     for song in playlist:
  99.         try:
  100.             print(
  101.                 f"Currently downloading song {curr_song}/{len(playlist) + 1} - [{int((curr_song / len(playlist)) * 100)} %]")
  102.             song_video = pafy.new(song.watchv_url)
  103.             audiostreams = song_video.audiostreams
  104.             audiostreams[2].download(song_video.title + ".m4a")
  105.             curr_song += 1
  106.         except:
  107.             pass
  108.  
  109.  
  110. if __name__ == '__main__':
  111.  
  112.     # Main
  113.     while True:
  114.         print("Choose what type of download you're going to use: (Just type 1, 2 or 3)")
  115.         print("1. A single song")
  116.         print("2. A playlist")
  117.         print("3. An album")
  118.         down_type = int(input())
  119.  
  120.         if down_type == 1:
  121.             url = input("Enter video url: \n")
  122.             video = pafy.new(url)
  123.             down_location(down_type)
  124.             audiostreams = video.audiostreams
  125.             audiostreams[2].download(video.title + ".m4a")
  126.  
  127.         elif down_type == 2:
  128.             down_playlist(down_type)
  129.  
  130.         elif down_type == 3:
  131.             down_playlist(down_type)
  132.  
  133.         else:
  134.             print("Invalid input")
  135.  
  136.         print("All songs downloaded successfully")
  137.         con = input("Do you wish to continue downloading?[y\\n]\n")
  138.         if con == 'y':
  139.             continue
  140.         else:
  141.             break
  142.  
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement