Advertisement
FocusedWolf

Python: Youtube video downloader

Jul 14th, 2023 (edited)
1,254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # This calls yt-dlp with my preferred options.
  3.  
  4. # Prevent creation of the __pycache__ directory.
  5. # SOURCE: https://github.com/pytest-dev/pytest/issues/200
  6. import sys
  7. sys.dont_write_bytecode = True
  8.  
  9. # -----
  10.  
  11. yt_dlp_path = r'C:\ProgramData\chocolatey\lib\yt-dlp\tools\x64\yt-dlp.exe'
  12. download_directory = 'E:\\'
  13.  
  14. # -----
  15.  
  16. def get_video_url():
  17.     url = None
  18.     while True:
  19.         try:
  20.             url = input(' Youtube URL: ')
  21.             if not str_is_none_or_whitespace(url):
  22.                break
  23.             clear_console()
  24.         except ValueError as e:
  25.            print(e)
  26.     return url
  27.  
  28. def str_is_none_or_whitespace(input):
  29.     return not input or input.isspace()
  30.  
  31. import os
  32. def clear_console():
  33.     os.system('cls' if os.name in ('nt', 'dos') else 'clear')
  34.  
  35. import subprocess
  36. def main():
  37.     url = get_video_url()
  38.     #  url = r'https://www.youtube.com/watch?v=ROS3OGABTWg'
  39.    
  40.     print()
  41.  
  42.     #  subprocess.run([yt_dlp_path, '--list-formats', url])
  43.  
  44.     print(' Download Directory', download_directory)
  45.     print()
  46.  
  47.     subprocess.run([yt_dlp_path, '-f', 'bestvideo+bestaudio', '--merge-output-format', 'mp4', '--output', '"%(title)s-%(id)s.%(ext)s"', url], cwd=download_directory)
  48.    
  49.     print()
  50.     wait_for_any_keypress()
  51.  
  52. # ----- Press any key to continue -----
  53.  
  54. import sys
  55. def wait_for_any_keypress():
  56.     if sys.platform == 'win32':
  57.         import os
  58.         os.system('pause')
  59.     elif sys.platform in ('linux2', 'darwin'):
  60.         print('Press any key to continue . . .')
  61.         import termios
  62.         import tty
  63.         stdin_file_desc = sys.stdin.fileno()
  64.         old_stdin_tty_attr = termios.tcgetattr(stdin_file_desc)
  65.         try:
  66.             tty.setraw(stdin_file_desc)
  67.             sys.stdin.read(1)
  68.         finally:
  69.             termios.tcsetattr(stdin_file_desc, termios.TCSADRAIN, old_stdin_tty_attr)
  70.  
  71. if __name__ == '__main__':
  72.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement