from pytube import Playlist import youtube_dl, os, time from moviepy.editor import * ### FOR A PLAYLIST ### ## FUNTIONS # downloading def download(): try: for link in linkset: print("downloading...", link) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([link]) except: print("un probleme est survenu... on réessaye!") retry(2) # converting and renaming fonction def convert(video): videoO = VideoFileClip(video) # split the .mp4 filename, file_extension = os.path.splitext(str(video)) # renvoie le .mp3 dans le repertoire de destination return videoO.audio.write_audiofile(os.path.join("./" + destination + "/" + filename + ".mp3")) # retry fonction en cas d'erreurs def retry(download, max_tries=10): for i in range(max_tries): try: time.sleep(0.5) download() break except Exception: continue ## MAIN PROGRAM # recup des liens de la playlist via lien playlist input playlist = Playlist(str(input("Entrer les liens de la playlist youtube: "))) linkset = list(playlist.video_urls) print(linkset) # creation du repertoire destination destination = str(input("Entrer le repertoire de destination:")) os.mkdir("./" + destination + "") # download options ydl_opts = {'format': 'best',} # download download() # convert and rename try: videos = os.listdir("./") except: print("vidéos à convertir introuvables dans le dossier courant") for video in videos: if video.endswith('.mp4'): print("convertion en mp3...") convert(video) # move to folder # cleaning for video in videos: try: if video.endswith('.mp4'): print(video) os.remove(video) except: print("rien à nettoyer!") break # done! print('Done!')