Advertisement
Fadly31337

YouTube MP3 Downloader From Playlist

Jan 31st, 2021
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import os
  2. import subprocess
  3.  
  4. from pytube import Playlist, YouTube
  5.  
  6. def run(pl):
  7.     # get parent directory; VERY IMPORTANT!!
  8.     # INCLUDE LAST SLASH AFTER FOLDER NAME
  9.     # e.g. /home/username/Folder/ or C:\Users\Username\Folder\
  10.     filepath = input("Please enter the filepath of the directory where this script is located:\n")
  11.     # get linked list of links in the playlist
  12.     links = pl.parse_links()
  13.     # download each item in the list
  14.     for l in links:
  15.         # converts the link to a YouTube object
  16.         yt = YouTube(l)
  17.         # takes first stream; since ffmpeg will convert to mp3 anyway
  18.         music = yt.streams.first()
  19.         # gets the filename of the first audio stream
  20.         default_filename = music.default_filename
  21.         print("Downloading " + default_filename + "...")
  22.         # downloads first audio stream
  23.         music.download()
  24.         # creates mp3 filename for downloaded file
  25.         new_filename = default_filename[0:-3] + "mp3"
  26.         print("Converting to mp3....")
  27.         # converts mp4 audio to mp3 audio
  28.         subprocess.run(['ffmpeg', '-i',
  29.             os.path.join(filepath, default_filename),
  30.             os.path.join(filepath, new_filename)
  31.         ])
  32.    
  33.     print("Download finished.")
  34.  
  35. if __name__ == "__main__":
  36.     url = input("Please enter the url of the playlist you wish to download: ")
  37.     pl = Playlist(url)
  38.     run(pl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement