def video(self, url, download_folder, logs): logs_file = open(logs, 'a') x = re.search('list=', url) if x is not None: playList = Playlist(url) if playList is None: playList._video_regex = re.compile( r"\"url\":\"(/watch\?v=[\w-]*)") for playlists_url in playList: try: print('Downloading...' + playlists_url) playList.download_all(download_folder) except Exception as ex: logs_file.write('Unable to download: ' + playlists_url + '\t' + str(ex) + '\n') pass else: try: print('Downloading...' + url) self.__download_file(url, download_folder, False) except Exception as ex: logs_file.write('Unable to download: ' + url + '\t' + str(ex)) logs_file.close()
def option1(): w = 0 while (w == 0): pl = input("Ingrese Url playlist completa:") try: playlist = Playlist(pl) playlist._video_regex = compile(r"\"url\":\"(/watch\?v=[\w-]*)") w = 1 except: print( "Url ingresada de manera incorrecta, por favor vuelva a intentarlo" ) w = 0 print("Numero de videos añadidos:", len(playlist.video_urls)) urls = [] for url in playlist.video_urls: urls.append((url + "\n")) lista = open("video-list.txt", "w+") lista.writelines(urls) lista.close()
def doDownload(self): playlistUrl = self.getPlaylistUrlFromClipboard() if playlistUrl == None: self.displayError('Playlist URL not in clipboard. Program closed.') return playlist = None try: playlist = Playlist(playlistUrl) playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") except KeyError as e: self.displayError('Playlist URL not in clipboard. Program closed.') return except http.client.InvalidURL as e: self.displayError(str(e)) return playlistTitle = playlist.title() if 'Oops' in playlistTitle: self.displayError( 'The URL obtained from clipboard is not pointing to a playlist. Program closed.' ) return playlistName, timeInfo = self.splitPlayListTitle(playlistTitle) targetAudioDir = AUDIO_DIR + DIR_SEP + playlistName if not os.path.isdir(targetAudioDir): targetAudioDirList = targetAudioDir.split(DIR_SEP) targetAudioDirShort = DIR_SEP.join(targetAudioDirList[-2:]) if self.getConfirmation( "Directory\n{}\nwill be created.\n\nContinue with download ?" .format(targetAudioDirShort)) != 'yes': return os.makedirs(targetAudioDir) for video in playlist.videos: audioStream = video.streams.get_by_itag(YOUTUBE_STREAM_AUDIO) videoTitle = video.title self.msgText = self.msgText + 'downloading ' + videoTitle + '\n' self.msg.configure(text=self.msgText) self.root.update() audioStream.download(output_path=targetAudioDir) for file in [ n for n in os.listdir(targetAudioDir) if re.search('mp4', n) ]: mp4FilePathName = os.path.join(targetAudioDir, file) mp3FilePathName = os.path.join(targetAudioDir, os.path.splitext(file)[0] + '.mp3') if timeInfo: timeStartSec, timeEndSec = self.splitTimeInfo(timeInfo) import moviepy.editor as mp # not working on Android clip = mp.AudioFileClip(mp4FilePathName).subclip( timeStartSec, timeEndSec) # disable if do not want any clipping clip.write_audiofile(mp3FilePathName) clip.close() os.remove(mp4FilePathName) else: if os.path.isfile(mp3FilePathName): os.remove(mp3FilePathName) os.rename(mp4FilePathName, mp3FilePathName)
def __init__(self, playlist_url='', skip=False): if not skip: playlist = Playlist(playlist_url) playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") self.urls = playlist.video_urls logging.info(f"[+] Number of tracks: {len(self.urls)}")
if dlType == "1": videoFile = VideoFileClip(path) videoFile.audio.write_audiofile(FOLDER + "/" + video.title + ".mp3") videoFile.close() os.remove(path) print("Downloaded " + video.title) print("\nSuccessfully downloaded YouTube video: " + video.title) elif choice == "2": playlist = Playlist( input("\nPlease input the YouTube playlist link\n")) playlist._video_regex = re.compile( r"\"url\":\"(/watch\?v=[\w-]*)") # Fixes empty playlist dlType = "" while dlType != "1" and dlType != "2": dlType = input( "\nInsert\n- '1' to download only audio\n- '2' to download the full video\n" ) print("\nDownloading...") for video in playlist.videos: try: path = video.streams.first().download(output_path=FOLDER) if dlType == "1": videoFile = VideoFileClip(path) videoFile.audio.write_audiofile(FOLDER + "/" +
from pytube import Playlist, YouTube from termcolor import colored from tqdm import tqdm import re print(colored("Please give us the link of the playlist:", 'yellow'), end=" ") urlOfPlayList = input() videosOfPlaylist = Playlist(urlOfPlayList) videosOfPlaylist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") videosUrls = videosOfPlaylist.video_urls print(colored( "If you want the hight resolution or the lowerst resolution:(l/h)[l]", 'yellow'), end=' ') chose = input() k = 0 loop = tqdm(total=len(videosUrls), position=0, leave=True) for url in videosUrls: while True: try: if chose == "h": YouTube(url).streams.filter( type="video").get_highest_resolution().download() else: YouTube(url).streams.filter( type="video").get_lowest_resolution().download() loop.set_description("Downloading".format(k))
def __download__playlist(self, playlist_url): playlist = Playlist(playlist_url) playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") for url in playlist.video_urls: self.__download__(url)
while True: print("\n" + "Output Format" + "\n" + "===================" + "\n" + "1) mp4" + "\n" + "2) mp3" + "\n" + "\n") a = input("Choice --> ") if a == "1": url = input("Url --> ") if "&list=" not in url: p1 = Process(target=mp4) p1.start() p2 = Process(target=vbar(0.0000001)) p2.start() p1.join() p2.join() if "&list=" in url: name1 = Playlist(url) name1._video_regex = re.compile( r"\"url\":\"(/watch\?v=[\w-]*)") try: print((name1.title())) except: pass print("Total videos " + str(len(name1.video_urls)) + "\n") print("Start Progressing...") for url in name1.video_urls: name2 = YouTube(url) print(name2.title) p1 = Process(target=mp4) p1.start() p2 = Process(target=vbar(1)) p2.start() p1.join() p2.join()
def create_youtube_playlist(self): play_list = Playlist(self.url) play_list._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") return play_list
def downloadplaylist(url, path_to_save, what, ask, wait, START): """Downlaods the sound of the videos in a playlist Args: url (string): the URL with the word list in it """ try: playlist = Playlist(url) except: print("Not a valid list URL and/or no list/videos found") sys.exit() # this fixes the empty playlist.videos list playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") STOP = 9990 #test purposes for n, video in enumerate(playlist.videos): if n >= START and n < STOP: try: mp4 = cleanup(video.title) + ".mp4" if ask: dl_yes = input( f"Download {n+1}/{len(playlist.video_urls)} - {video.title} ? (y/n/q) " ) else: dl_yes = "y" if dl_yes == "y" or dl_yes == "Y": print( f"Downloading {n+1}/{len(playlist.video_urls)} - {video.title}" ) if what == "audio": #video.streams.first().download(filename=mp4) # for music video.streams.get_by_itag(140).download(filename=mp4) new_filename = path_to_save + left( mp4, (len(mp4) - 4) ) + "mp4.mp4" # for some reason files are saved as namemp4.mp4 #convert_to_mp3(new_filename) elif what == "video": video.streams.get_by_itag(18).download( filename=mp4) # for video 360p if wait: wait_time = randint(10, 30) countdown(wait_time) elif dl_yes == "q" or dl_yes == "Q": sys.exit() except SystemExit: print() print("Thank you for using this script.") sys.exit() except: print( f"ERROR Downloading or finding the name of {n+1}/{len(playlist.video_urls)} " ) # some videos give 403 error # too lazy to implement this (yet) # https://github.com/pytube/pytube/issues/399 main_convert(path_to_save)
def main(): # list of podcast video ids already present in the raw data archive processed_podcasts = get_processed_podcasts() # playlist to download the podcasts from playlist_link = "https://www.youtube.com/watch?v=c9AbECvRt20&list=PLrAXtmErZgOdP_8GztsuKi9nrraNbKKp4" playlist = Playlist(playlist_link) # this fixes the empty playlist.videos list playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)") print('Total number of podcasts in the playlist: ', str(len(playlist.video_urls))) limit = 0 # counter used to limit number of files to be downloaded for video in playlist.videos: # download no more than these many podcasts if (limit == 80): break # ignore podcasts that are already loaded to the raw data archive if (video.video_id in processed_podcasts): limit = limit + 1 continue # ignore podcasts without segment information by timestamps if (video.description == ""): limit = limit + 1 continue # extract metadata for this podcast metadata = {} metadata['video_id'] = video.video_id metadata['title'] = re.sub('[^a-zA-Z0-9 \n\.]', '_', video.title) metadata['description'] = video.description metadata['rating'] = video.rating metadata['length'] = video.length metadata['views'] = video.views metadata['author'] = video.author aware_local_now = datetime.now(timezone.utc).astimezone() metadata['downloaded_at'] = str(aware_local_now) # create directory for this podcast download_dir = 'podcast_' + metadata['video_id'] os.makedirs(download_dir, exist_ok=True) # open a new text file to write metadata for this podcast with open(os.path.join(download_dir, metadata['title'] + '.txt'), 'w+') as f: # append metadata to text file f.write(str(metadata) + '\n') # start audio mp4 download stream for this podcast ys = video.streams.filter(only_audio=True, file_extension='mp4') print('Downloading : ', metadata['title']) ys[0].download(filename=metadata['title']) # to ensure mp4 is fully downloaded before further processing time.sleep(2) ''' convert downloaded mp4 to wav file RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz and save in the directory created for this podcast ''' subprocess.call(['ffmpeg', \ '-i', metadata['title']+'.mp4', '-acodec', 'pcm_s16le', '-ac', '1', '-ar', '16000', \ os.path.join(download_dir, metadata['title']+'.wav')]) # delete raw mp4 file delete_file_from_path(metadata['title'] + '.mp4') limit = limit + 1