Advertisement
mdgaziur001

playlist downloader

Jan 13th, 2023
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. # First argument: path to playlists links list file(links should be separated by newline and the file should have a trailing newline)
  2. # Second argument: the number of processes to spawn concurrently
  3. # Third argument: maximum resolution
  4. import shlex
  5. import subprocess
  6. from multiprocessing import Process
  7. import sys
  8.  
  9. TEXT_FILE = None
  10. with open(sys.argv[1]) as fp:
  11.     TEXT_FILE = fp.read()
  12. N_THREADS = int(sys.argv[2])
  13. RES = sys.argv[3]
  14. LINKS = TEXT_FILE.split('\n')
  15. LINKS.pop()
  16.  
  17.  
  18. def download_playlist(link: str) -> bool:
  19.     command = shlex.split(f'yt-dlp -i \"{link}\" --no-mtime -o \'%(playlist)s/%(title)s.%(ext)s\' -f \'bestvideo[height<={RES}]bestvideo[ext=mp4]\'')
  20.     try:
  21.         process = subprocess.Popen(command)
  22.         process.wait()
  23.     except Exception as e:
  24.         print("Unexpected exception: ", e)
  25.         return False
  26.     return True
  27.  
  28.  
  29. def main():
  30.     idx = 0
  31.     while idx < len(LINKS):
  32.         processes = []
  33.         for _ in range(0, N_THREADS):
  34.             print(f"Spawning thread for playlist: {LINKS[idx]}")
  35.             process = Process(target=download_playlist, args=(LINKS[idx], ))
  36.             processes.append(process)
  37.             process.start()
  38.             idx += 1
  39.  
  40.         while len(processes) != 0:
  41.             p_idx = 0
  42.             while p_idx < len(processes):
  43.                 if not processes[p_idx].is_alive():
  44.                     processes.pop(p_idx)
  45.                     break
  46.                 p_idx += 1
  47.  
  48.  
  49. if __name__ == "__main__":
  50.     main()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement