Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import youtube_dl
- import threading
- import sys
- # help command
- if sys.argv[1] == "help":
- print("Usage: python multithreaded.py YT-Playlist-Link")
- print("Usage: python multithreaded.py YT-Playlist-Link StartAtInt")
- print("Usage: python multithreaded.py YT-Playlist-Link StartAtInt EndAtInt")
- sys.exit(0)
- # Optional StartAt and EndAt parameter
- hasstart = False
- hasend = False
- try:
- if sys.argv[2] != None:
- hasstart = True
- startat = int(sys.argv[2])
- if sys.argv[3] != None:
- hasend = True
- endat = int(sys.argv[3])
- except:
- pass
- # Method executed by individual threads
- def download(url: str, title: str):
- ydl_opts = {
- 'format': 'best',
- 'outtmpl': title + '.%(ext)s',
- 'noplaylist': True,
- 'ignoreerrors': True,
- 'abort_on_unavailable_fragments': True,
- 'quiet': True,
- }
- try:
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
- ydl.download([url])
- print("Download comlete: " + title)
- except Exception as e:
- print("Download exception: " + str(e))
- # Select options based on input parameters
- if not hasstart and not hasend:
- ydl_opts = {
- 'ignoreerrors': True,
- 'abort_on_unavailable_fragments': True,
- }
- elif hasstart and not hasend:
- ydl_opts = {
- 'ignoreerrors': True,
- 'abort_on_unavailable_fragments': True,
- 'playliststart': startat,
- }
- elif hasend:
- ydl_opts = {
- 'ignoreerrors': True,
- 'abort_on_unavailable_fragments': True,
- 'playliststart': startat,
- 'playlistend': endat,
- }
- # Get all videos in the playlist and run download in individual thread
- try:
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
- result = ydl.extract_info(sys.argv[1], download=False)
- for video in result['entries']:
- try:
- threading.Thread(target=download, args=(video['webpage_url'], video['title'])).start()
- except:
- print("Skipped one due to error")
- continue
- except KeyboardInterrupt:
- # Doesnt work for some reason
- for thread in threading.enumerate():
- thread.join()
- except Exception as e:
- print("General exception: " + str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement