def get_song_info(given_url="", local_dir=cur_dir, quality=1): """Returns song info for given YouTube url""" url = is_valid_youtube_url(given_url) yt = YouTube(url) raw_info = yt.get_video_data() if 'args' in raw_info: song_info = copy.copy(empty_record) song_info['url'] = url song_info['title'] = raw_info['args']['title'] song_info['author'] = raw_info['args']['author'] try: song_info['video_id'] = raw_info['args']['vid'] except KeyError: song_info['video_id'] = url.replace(youtube_base_url, '') song_info['duration'] = int(raw_info['args']['length_seconds']) song_info['view_count'] = int(raw_info['args']['view_count']) song_info['thumbnail_url'] = raw_info['args']['thumbnail_url'] filter_index = get_filter_index(quality, len(yt.filter())) video = yt.filter()[filter_index] local_file_name = "{0}.{1}".format(yt.filename, video.extension) local_file_path = os.path.join(local_dir, local_file_name) if os.path.exists(local_file_path): song_info['local_file_path'] = local_file_path return song_info else: return None
def get_video(video_info): save_path = os.path.join(settings["store_path"], video_info["snippet"]["channelTitle"]) mkdir(save_path) upload = YouTube("https://www.youtube.com/watch?v={0}".format( video_info["id"]["videoId"])) video = None res = "" i = 0 while not video: if not res: resolution = video_resolutions[str(settings["max_quality"] - i)] if upload.filter(resolution=resolution): res = resolution else: i += 1 if i > settings["max_quality"]: log("No upload candidate matches quality criteria.", "warn") return else: if len(upload.filter(resolution=res)) > 1: if upload.filter("mp4", resolution=res): video = upload.get('mp4', res) else: video = upload.get(None, res) video.download(save_path) get_audio( video_info, os.path.join(save_path, (video.filename + "." + video.extension)))
def downloadFile(url): streams = YouTube(url).streams streams.filter(progressive=True).desc().first() name = streams.first().download() newname = name.replace(' ','_') os.rename(name,newname) return newname
def down(url, directory=None, skippable=False, ftype=None): if not directory: directory = os.getcwd() try: yt = YouTube(url) except: print('Could not get URL "{}"'.format(url)) return yt.get_videos() print('Found "{}"'.format(yt.filename)) if skippable and input("Download? [y]/n: ") not in ['','y']: return if len(yt.filter(resolution='480p')) == 0: if len(yt.filter(resolution='360p')) == 0: print("Can't find 480p or 360p: {}".format(yt.filename)) return video = yt.get('mp4', '360p') else: video = yt.get('mp4', '480p') try: video.download(os.getcwd()) print('...download finished') except OSError: print("Could not write file") if ftype == 'mp3': fname = video.filename mp4_to_mp3(fname, directory)
def download_video(video_id='', folder='.', progress=None, finish=None): url = 'http://www.youtube.com/watch?v={}'.format(video_id) try: yt = YouTube(url) except Exception as e: print('Failed to get video with url', url) return yt.set_filename(yt.filename + ' ' + str(video_id)) if len(yt.filter('mp4')) > 0: if len(yt.filter('mp4', '720p')) > 0: video = yt.get('mp4', '720p') else: video = yt.filter('mp4')[-1] else: print('No mp4 formatted videos') return try: video.download(folder, on_progress=progress, on_finish=finish) except Exception as e: print('Failed to download file with exception:', e) return
def _download_file(self, video_id): file_path = self._build_file_path(video_id) if not os.path.isfile(file_path): yt = YouTube() yt.from_url("http://youtube.com/watch?v=" + video_id) yt.filter('mp4')[0].download(file_path) # downloads the mp4 with lowest quality return file_path
def post(self, request): videoURL = request.POST.get('url') start = request.POST.get('start') end = request.POST.get('end') if videoURL is not None and start is not None and end is not None: try: if (int(end) - int(start)) > 10: return HttpResponse(status=400) except: return HttpResponse(status=400) try: yt = YouTube(videoURL) print('ConverGIF - get video') yt.set_filename('video') print('ConverGIF - set filename') yt.filter('mp4')[-1].download('{}/youtube_video/'.format( settings.MEDIA_ROOT)) print('ConverGIF - download') except: return HttpResponse(status=404) try: video = VideoFileClip('{}/youtube_video/video.mp4'.format( settings.MEDIA_ROOT)).subclip(int(start), int(end)) video.write_videofile('{}/youtube_gif/gif.gif'.format( settings.MEDIA_ROOT), fps=25, codec='gif') os.remove('{}/youtube_video/video.mp4'.format( settings.MEDIA_ROOT)) except: return HttpResponse(status=416) else: return HttpResponse(status=400) gifURL = '{}/youtube_gif/gif.gif'.format(settings.MEDIA_ROOT) return JsonResponse({'url': gifURL})
def download(url, resolution, path, addOrder, numOfVideos, video_urls, completed): try: n = url[1] url = url[0] downloaded = listdir(path) resolutions = ['720p', '480p', '360p', '240p', '144p'] yt = YouTube(url) if (yt.filename + '.mp4') in downloaded: return False if len(yt.filter('mp4', resolution=resolution)) == 0: resolutions.remove(resolution) for r in resolutions: if len(yt.filter('mp4', resolution=r)) != 0: resolution = r break if addOrder: length = int(math.log(numOfVideos,10)) + 1 prefix = str(n) prefix = "0"*(length - len(str(n))) + prefix +'. ' if (prefix + yt.filename + '.mp4') in downloaded: return False yt.set_filename(prefix + yt.filename) video = yt.get('mp4', resolution) print("downloading ", yt.filename, resolution) video.download(path) completed.put(1) except: print("downloading ", yt.filename, "failed") print("retry later") video_urls.put((url, n))
def yt_download(row, csv_w, idx): video_name = 'v_fight_' + '{:05d}'.format(idx) res = ['480p', '720p', '1080p'] try: yt = YouTube(row.video_link).streams.filter( progressive=True, subtype='mp4') except Exception as e: print(idx, row.video_link, e) return try: for r in res: if yt.filter(res=r).count() != 0: yt = yt.filter(res=r).first() break except Exception as e: print(idx, e) return try: if random() < 0.8: yt.download('/home/uscc/train', video_name) csv_w.writerow([idx, video_name, yt.filesize, 'train']) print([video_name, yt.filesize, idx, 'train']) else: yt.download('/home/uscc/test', video_name) csv_w.writerow([idx, video_name, yt.filesize, 'test']) print([video_name, yt.filesize, idx, 'test']) except AttributeError as e: print(idx, e) return
def _download_file(self, video_id): file_path = self._build_file_path(video_id) if not os.path.isfile(file_path): yt = YouTube() yt.from_url("http://youtube.com/watch?v=" + video_id) yt.filter('mp4')[0].download( file_path) # downloads the mp4 with lowest quality return file_path
def Download(songURI, bestQuality = False): filetype = "mp4" #Check if NAS is mounted nasDir = '/home/pi/music/yt/' bckDir = '/home/pi/musicBAK/' dirUse = nasDir if os.path.isdir(nasDir): #put bck files into nasDirs bakFiles = os.listdir(bckDir) for x in range(0,len(bakFiles)): shutil.copy(bckDir+bakFiles[x], nasDir+bakFiles[x]) os.remove(bckDir+bakFiles[x]) else: dirUse = bckDir #if input is null then return if songURI == "": return songURL = "https://www.youtube.com/watch?v="+songURI #use full url if that is given if "youtube.com" in songURI: songURL = songURI try: yt = YouTube(songURL) except: print "ERROR: URL is not a video!" if bestQuality: video = yt.filter(filetype)[-1] else : video = yt.filter(filetype)[0] print "starting download to '" + dirUse + "'"; try: video.download(dirUse); print "down succeeded" except: print "download failed:", sys.exc_info() if dirUse == nasDir: client = MPDClient() # create client object client.connect("localhost", 6600) # connect to localhost:6600 #client.update("yt/"+yt.filename + "."+filetype) #Hack so that it will wait for the f*****g update. call(["mpc", "update",("yt/"+yt.filename+"."+filetype),"-w"]) client.disconnect() time.sleep(0.1); return yt.filename + "."+filetype
def download(video_id): yt = YouTube() yt.from_url(construct_url(video_id)) yt.set_filename(video_id) first_video = yt.filter(resolution='480p') + yt.filter(resolution='360p') if not len(first_video): return False first_video[0].download(MEDIA_URL) return True
def download(video_url_or_id, folder_path = "./"): if "youtube.com" in video_url_or_id: video_id = video_url_or_id.split("v=")[-1] elif "youtu.be" in video_url_or_id: video_id = video_url_or_id.split(".be/")[-1] else: video_id = video_url_or_id video_id = re.search("[\w-]+", video_id).group(0) yt = YouTube() yt.from_url("http://www.youtube.com/watch?v={}".format(video_id)) yt.filter("mp4")[-1].download(path = folder_path, force_overwrite = True)
def Download(songURI, bestQuality=False): filetype = "mp4" #Check if NAS is mounted nasDir = '/home/pi/music/yt/' bckDir = '/home/pi/musicBAK/' dirUse = nasDir if os.path.isdir(nasDir): #put bck files into nasDirs bakFiles = os.listdir(bckDir) for x in range(0, len(bakFiles)): shutil.copy(bckDir + bakFiles[x], nasDir + bakFiles[x]) os.remove(bckDir + bakFiles[x]) else: dirUse = bckDir #if input is null then return if songURI == "": return songURL = "https://www.youtube.com/watch?v=" + songURI #use full url if that is given if "youtube.com" in songURI: songURL = songURI try: yt = YouTube(songURL) except: print "ERROR: URL is not a video!" if bestQuality: video = yt.filter(filetype)[-1] else: video = yt.filter(filetype)[0] print "starting download to '" + dirUse + "'" try: video.download(dirUse) print "down succeeded" except: print "download failed:", sys.exc_info() if dirUse == nasDir: client = MPDClient() # create client object client.connect("localhost", 6600) # connect to localhost:6600 #client.update("yt/"+yt.filename + "."+filetype) #Hack so that it will wait for the f*****g update. call(["mpc", "update", ("yt/" + yt.filename + "." + filetype), "-w"]) client.disconnect() time.sleep(0.1) return yt.filename + "." + filetype
def dl_video(link): print("Downloading [" + link + "]") try: yt = YouTube(link) yt.filter('mp4')[-1].download('./') print("Done [" + link + "] (" + yt.filename + ")") return yt.filename except: print("Could not download: " + link) print(sys.exc_info()[0]) return False
def download(video_url_or_id, folder_path="./"): if "youtube.com" in video_url_or_id: video_id = video_url_or_id.split("v=")[-1] elif "youtu.be" in video_url_or_id: video_id = video_url_or_id.split(".be/")[-1] else: video_id = video_url_or_id video_id = re.search("[\w-]+", video_id).group(0) yt = YouTube() yt.from_url("http://www.youtube.com/watch?v={}".format(video_id)) yt.filter("mp4")[-1].download(path=folder_path, force_overwrite=True)
def download_song(given_url="", local_dir=cur_dir, quality=1): """ Downloads the video song for given YouTube URL to the given local_dir (default os.getcwd()) of given quality """ url = is_valid_youtube_url(given_url) yt = YouTube(url) filter_index = get_filter_index(quality, len(yt.filter())) video = yt.filter()[filter_index] local_file_name = "{0}.{1}".format(yt.filename, video.extension) local_file_path = os.path.join(local_dir, local_file_name) if not os.path.exists(local_file_path): video.download(local_dir) return local_file_path
def downloadFisicalFile(youtubeId, idName): yt = YouTube() yt.url = ("http://www.youtube.com/watch?v=" + youtubeId) yt.filename = idName video = 0 if len(yt.filter('mp4')) > 0: if len(yt.filter(resolution='720p')) > 0: video = yt.get('mp4','720p') else: video = yt.get('mp4') video.download("tempvideo/") else: cocomo.printJson("no se puede descargar el archivo", "error")
def download_Video_Audio(path, vid_url, file_no): try: yt = YouTube(vid_url) except Exception as e: print("Error:", str(e), "- Skipping Video with url '" + vid_url + "'.") return new_filename, author = produceNewName(yt.filename) pathslash = path + "/" try: if os.path.isfile(pathslash + new_filename + ".mp3"): raise FileNotFoundError() try: # Tries to find the video in 720p video = yt.get('mp4', '720p') except Exception: # Sorts videos by resolution and picks the highest quality video if a 720p video doesn't exist video = sorted(yt.filter("mp4"), key=lambda video: int(video.resolution[:-1]), reverse=True)[0] print("downloading", yt.filename + " Video and Audio...") bar = progressBar() video.download(path, on_progress=bar.print_progress, on_finish=bar.print_end, force_overwrite=True) print("successfully downloaded", new_filename, "!") try: os.system("sacad '{}' '{}' 480 '{}'".format( author, new_filename, pathslash + str(file_no) + ".png")) aud = 'ffmpeg -i \"{}.mp4\" \"{}.wav\"'.format( pathslash + str(yt.filename), pathslash + str(file_no)) final_audio = 'lame --tt \"{}\" --ta \"{}\" --ty {} \"{}.wav\" \"{}.mp3\"'.format( new_filename, author, getPublishingYear(vid_url), pathslash + str(file_no), pathslash + str(new_filename)) if os.path.isfile(pathslash + str(file_no) + ".png"): final_audio += "--ti \"{}.png\"".format(pathslash + str(file_no)) os.system(aud) os.system(final_audio) os.remove(pathslash + str(yt.filename) + '.mp4') os.remove(pathslash + str(file_no) + '.wav') if os.path.isfile(pathslash + str(file_no) + ".png"): os.remove(pathslash + str(file_no) + '.png') print("sucessfully converted", new_filename, "into audio!") except OSError as e: print(yt.filename, "Looks like we got an error:", str(e)) except FileNotFoundError: print(new_filename, "already exists in this directory! Skipping video...")
def fetch_video_by_url(self, url): '''Fetch video by url :param [str] url: The url linked to a YouTube video ''' yt = YouTube(url) if(self.resolution == 'highest' or self.resolution == 'lowest'): video_list = yt.filter(self.extension) if(len(video_list) == 0): raise DoesNotExist("No videos met this criteria.") if(self.resolution == 'highest'): video = video_list[-1] else: video = video_list[0] else: result = [] for v in yt.get_videos(): if self.extension and v.extension != self.extension: continue elif self.resolution and v.resolution != self.resolution: continue else: result.append(v) matches = len(result) if matches <= 0: raise DoesNotExist("No videos met this criteria.") elif matches == 1: video = result[0] else: raise MultipleObjectsReturned("Multiple videos met this criteria.") return video
def download_single_video(save_dir, video_url): yt = YouTube(video_url) try: video = min(yt.filter('mp4')) except: video = min(yt.get_videos()) # save_dir = "video/" + label + "/" if not (os.path.isdir(save_dir)): os.mkdir(save_dir) valid_chars = "-_ %s%s" % (string.ascii_letters, string.digits) filename = ''.join(c for c in yt.filename if c in valid_chars) filename = filename.replace('-','_') filename = filename.replace(' ','_') while(filename[-1] == '_'): filename = filename[:-1] if(os.path.isfile(save_dir + filename + '.' + video.extension)): return yt.set_filename(filename) video.download(save_dir)
def getVideoFname(videoId): try: fname = DIR+"/"+videoId+".mp4" # Caching if os.path.isfile(fname): print("Used cached video file %s " % fname) return fname url = _videos[videoId]['url'] print("Fetching info from %s " % url) yt = YouTube(url) v = yt.filter('mp4')[0] # For Non mp4, NOT SUPPORTED for now # if v is None: # v = yt.videos()[0] dfname = DIR+"/"+v.filename+".mp4" if v: print("Video Downloading %s " % videoId) v.download(DIR) print("Moving %s to %s " % (dfname,fname)) os.rename(dfname,fname) print("Video Downloaded") return fname else: print("Video not Found for %s " % videoId) return None except Exception as e: print(str(e)) return None
def download(self, url): try: yt = YouTube(url) except Exception as e: print(e) raise SystemExit filename = "{}\\{}.mp4".format(os.getcwd(), yt.filename) # Remove non-ascii characters so that the filename works!!! # Also removes some self-defined characters forbidden = ["[", "]", "(", ")", " ", "&", "%", "#"] filename = u"{}".format(filename) filename = "".join(i for i in filename if i in string.printable) for char in forbidden: filename = filename.replace(char, "") # Finds best quality for the video resolutions = ["1080p", "720p", "480p", "360p", "240p"] video_info = yt.filter("mp4") for resolution in resolutions: if resolution in str(video_info): res = resolution break # Downloads the video try: video = yt.get("mp4", res) video.download(filename) except Exception as e: print(e) raise SystemExit return filename
def fetch_video(url): global FILENAME yt = YouTube(url) FILENAME = yt.filename (yt.filter('mp4')[-1]).download(pA_path)
def download_Video_Audio(path, vid_url, file_no): try: yt = YouTube(vid_url) except Exception as e: print("Error:", str(e), "- Skipping Video with url '"+vid_url+"'.") return try: # Tries to find the video in 720p video = yt.get('mp4', '720p') except Exception: # Sorts videos by resolution and picks the highest quality video if a 720p video doesn't exist video = sorted(yt.filter("mp4"), key=lambda video: int(video.resolution[:-1]), reverse=True)[0] print("downloading", yt.filename+" Video and Audio...") try: bar = progressBar() video.download(path, on_progress=bar.print_progress, on_finish=bar.print_end) print("successfully downloaded", yt.filename, "!") except OSError: print(yt.filename, "already exists in this directory! Skipping video...") try: os.rename(yt.filename+'.mp4',str(file_no)+'.mp4') aud= 'ffmpeg -i '+str(file_no)+'.mp4'+' '+str(file_no)+'.wav' final_audio='lame '+str(file_no)+'.wav'+' '+str(file_no)+'.mp3' os.system(aud) os.system(final_audio) os.remove(str(file_no)+'.wav') print("sucessfully converted",yt.filename, "into audio!") except OSError: print(yt.filename, "There is some problem with the file names...")
def downloadSong(url): video = pafy.new(url) best = video.getbest(preftype="mp4") yt = YouTube() yt.url = url os.system('cls') # os.system('clear') for linux. print "Download URL fetched successfully!\n" print "1. Get the list of available streams" print "2. Get the highest resolution available" print "3. Start download the video\n" while True: print "Select an action: " action = raw_input("> ") if action == str(1): print "Availabile streams for the URL: \n" pprint(yt.videos) print "\n" elif action == str(2): print "Highest resolution is:" pprint(yt.filter('mp4')[-1]) print "\n" elif action == str(3): print "Starting download: \n" best.download(quiet=False) print "Download finished!, the file has been downloaded to the same folder where the script is located." sys.exit(0) else: print "Not a valid option!\n"
def download_video(url, save_in=None, filename_out=None, verbose=False): ''' Descarga un vídeo de youtube, se necesita instalar pytube ''' from pytube import YouTube y = YouTube(url) original_name = YoutubeApi().search_video(url)['title'] if verbose == True: print('Downloading: ' + str(original_name)) print('Please, wait...') if filename_out: y.set_filename(filename_out) else: filename_out = original_name y.set_filename(filename_out) video = y.filter('mp4')[-1] if save_in == None: video.download(config.PATH) else: video.download(save_in) if verbose == True: print('Video downloaded and saved as ' + filename_out) return filename_out + '.mp4'
def download_videos(links, loc, quality): """ Download the video in the lowest mp4 quality available and store it in a folder. Age-restricted videos are ignored. The Downloading resumes if there was any interruption last time. links : list of the links in the Youtube Playlist loc : file location for the downloading of videos quality : 1 for best quality & 2 for minimum quality """ print "\nPress Ctrl+Z for stopping downloads at any moment.\n" print "DOWNLOADING " + str(len(links)) + " VIDEOS \n" video_count=0 for link in links: try: video_count=video_count+1 yt = YouTube(link) yt.set_filename(yt.filename) print "Video #" + str(video_count) + ": \t"+ str(yt.filename.encode('ascii','ignore')) if downloaded(loc,yt.filename): print "\t\tAlready Downloaded \n" continue quality_idx=0 if quality==1: quality_idx=-1 video = yt.filter('mp4')[quality_idx] print "\t\tDownloading ........" video.download(loc) print "\t\tDownload Done \n" except: print "Video #" + str(video_count) + ": \t" + "Could Not Download" + "\n"
def downloadytVideo(self, song, dldir = "."): # If video file exists, remove it. Only the case if program crashed, # therefore integrety of file cannot be guaranteed or reset option # specified. if os.path.isfile(dldir + "/" + song.name + ".mp4") and not song.mp4exists: os.remove(dldir + "/" + song.name + ".mp4") try: # Get the video yt = YouTube(song.url) yt.set_filename(song.name) # Select the mp4 video in the highest resolution data = yt.filter('mp4')[-1] video = yt.get('mp4', data.resolution) # Download the video video.download(dldir) except Exception as e: print("Something went wrong:") print(e) print("Ignoring...") return None song.mp4exists = True videoFilePath_nofe = dldir + "/" + song.name return videoFilePath_nofe
def download_by_id(vid_id): """ Download a YouTube video given the video's ID, and return the path to the video. If the video has already been downloaded, then return the path to that file without redownloading. """ if not os.path.isdir(MP4_DIR): os.mkdir(MP4_DIR) if not os.path.isdir(MP3_DIR): os.mkdir(MP3_DIR) url = "https://www.youtube.com/watch?v=%s" % vid_id yt = YouTube(url) yt.filename = vid_id # Check if the mp3 already exists mp3_output = os.path.join(MP3_DIR, yt.filename + ".mp3") if os.path.exists(mp3_output): return mp3_output # Download full video mp4_output = os.path.join(MP4_DIR, yt.filename + ".mp4") if not os.path.exists(mp4_output): vid = yt.filter('mp4')[-1] vid.download(MP4_DIR) # Extract audio from video mp3_output = os.path.join(MP3_DIR, vid_id + ".mp3") if not os.path.exists(mp3_output): command = "ffmpeg -i \"%s\" -ab 160k -ac 2 -ar 44100 -vn \"%s\"" % ( mp4_output, mp3_output) subprocess.call(command, shell=True) return mp3_output
def Playlist(playlist): playlist_url = urlopen(playlist) page = playlist_url.read() page_soup = soup(page, 'lxml') count = 0 for i in page_soup.find_all('td', {'class': 'pl-video-title'}): count += 1 print('\n There are total', count, 'videos in Playlist to be downloaded \n') video_quality = quality() print('Enter Complete Path - ', end=' ') path = input() os.system('cls') Downloading = 0 for i in page_soup.find_all('td', {'class': 'pl-video-title'}): Downloading += 1 print('Downloaded - ', end=' ') print(int(((Downloading - 1) * 100) / count), '%') video = i.a["href"] URL = "http://www.youtube.com%s" % video yt = YouTube(URL) video = yt.filter('mp4')[video_quality] print('Downloading', Downloading, '/', count, '- ', end=' ') print(i.a.text) video.download(path) print('\nCongrats :-) You have Downloaded the Playlist Effortlessly! \n')
def downloadVideo(url, codec): try: yt = YouTube(url) vidName = str(yt.filename) start_time = time.time() if codec == 0: print "(+) Codec: MP4" allVidFormat = yt.get_videos() higMp4Res = str(yt.filter('mp4')[-1]).split()[-3] print "\n(+) Name: %s" %vidName print "(+) URL: %s" %url print "(+) Resolution: %s" %higMp4Res video = yt.get('mp4', higMp4Res) print "(+) Downloading video" video.download('.') print "(+) Download complete" if codec == 1: print "[youtube] Codec: MP3" ydl = youtube_dl.YoutubeDL() r = ydl.extract_info(url, download=False) options = {'format': 'bestaudio/best', 'extractaudio' : True, 'audioformat' : "best", 'outtmpl': r['title'], 'noplaylist' : True,} print "[youtube] Name: %s" % (vidName) print "[youtube] Uploaded by: %s" % (r['uploader']) print "[youtube] Likes: %s | Dislikes: %s" % (r['like_count'], r['dislike_count']) print "[youtube] Views: %s" % (r['view_count']) with youtube_dl.YoutubeDL(options) as ydl: ydl.download([url]) print("[youtube] Download Time: %s sec" % round((time.time() - start_time), 2)) print "" except Exception as e: print "(-) Error: %s" %e
def get_slit_scans(video_id): yt = YouTube("https://www.youtube.com/watch?v=" + video_id) youtube_video = sorted(yt.filter('mp4'), key=lambda x: int(x.resolution[:-1]))[-1] video_filename = youtube_video.filename + ".mp4" youtube_video.download(".") def get_row(gf, frame): row = frame % clip.h return gf(frame / clip.fps)[row] def slitscan(gf, t): frame = int(clip.fps * t) min_frame = int(frame - frame % clip.h) max_frame = int(frame + (clip.h - frame % clip.h)) return np.array([get_row(gf, x) for x in range(min_frame, max_frame)]) clip = mpy.VideoFileClip(video_filename) clip = clip.fl(slitscan, apply_to="mask") scans = [ clip.get_frame(t / clip.fps) for t in range(0, int(clip.duration * clip.fps), clip.h) ] os.remove(video_filename) return scans
def youtube_download(url, save_in='', max_quality=True, extension='mp4', name='default'): from pytube import YouTube y = YouTube(url) if name != 'default': y.set_filename(name) if max_quality == True: video = y.filter(extension)[-1] try: video.download(save_in) except OSError: num = 0 error = True while error == True: y.set_filename(name + '_' + str(num)) try: video.download(save_in) error == False except OSError: num += 1 error == True return 'Descarga finalizada'
def youtube_links_info(linklist): with open('links.txt', 'r') as f: old_links = f.readlines() print(" ") for link in linklist: if link not in old_links: try: link_info = YouTube(link) #check if the video is still available for downloading ex. blocked videos for copyright issues codec, resolution, quality = str( link_info.filter("mp4")[-1]).split("-") video = link_info.get(codec[-5:-2], resolution.strip()) link_info.set_filename( str(randint(0, 10000)) + "_" + str(link_info.filename)) print("Downloading:", link_info.title) if not os.path.exists(os.getcwd() + '/videos/'): os.makedirs('videos') video.download(os.path.join(os.getcwd()) + '/videos/') used_links(link) except exceptions.PytubeError: print("Wrong link") continue except exceptions.AgeRestricted: print("Wrong link - need to be logged in to download!") continue
def download_youtube_video(video_url): ''' Signed woodworksio Aug 13, 2017 Takes in a video URL to a youtube video, outputs the relative path to the downloaded mp3 Usage: download_youtube_video(video_url) --> returns mp3 filepath ''' vid = Youtube_Video(video_url) vid.set_filename(re.sub('[^a-zA-Z]','',vid.filename).lower()) saved_to = vid.filename if os.path.exists('static/'+saved_to+'.mp4'): print("[=] Video exists...") return 'static/'+saved_to+'.mp4' mp4_vids = vid.filter('mp4') if not len(mp4_vids)>0: return -1 vid = mp4_vids[0] #who gives a f**k about resolution ### UNCOMMENT BELOW FOR PROGRESS # vid.download('static/',on_progress=lambda x,y: print(int(x/y*100))) vid.download('static/') mp4 = 'static/'+saved_to+'.mp4' mp3 = 'static/'+saved_to+'.mp3' # TODO open pipe on init to mass convert videos while keeping ffmpeg open cmd = "ffmpeg -i \"" + mp4 + "\" -vn -ar 44100 -ac 2 -ab 192k -f mp3 \"" + mp3 + "\"" os.system(cmd) try: os.remove(mp4) except Exception as e: print(e) print("-"*60) print("\nFailed to remove "+mp4) return 'static/'+saved_to+'.mp3'
def _download_file(self, video_id): file_path = self._build_file_path(video_id) if not os.path.isfile(file_path): yt = YouTube() yt.from_url("http://youtube.com/watch?v=" + video_id) video = yt.filter('mp4')[0] video.download(file_path) return file_path
def down(url,qual,loc): if "youtube" in url and "http" in url: url=url else: url="http://www.youtube.com%s"%url obj=YouTube(url) video=obj.filter('mp4')[qual] video.download(loc)
def setup_audio(config): while True: link = input("Link to YouTube video: ") try: streams = YouTube(link).streams except RegexMatchError: print("Invalid YouTube link") else: break ffmpeg_path = input("path to ffmpeg.exe: ") AudioSegment.converter = ffmpeg_path file = streams.filter(only_audio=True)[0].download() audio = AudioSegment.from_file(file) adjust_dB = 0 while True: # play sound to see if volume needs to be turned down play(audio[:2000]) adjust = input("Adjust dB of volume? (y,n): ") if adjust == "y": db_str = input("How much? (can be negative to lower the volume): ") try: adjust_dB = float(db_str) except ValueError: print( f"{db_str} cannot be converted to floating point number.") else: audio += adjust_dB else: break while True: how_long_s = input( "How long to play is sec? (number or blank to play the whole duration): " ) if not how_long_s: how_long_s = None break try: how_long_ms = int(how_long_s * 1000) except ValueError: print( f"{how_long_s} cannnot be converted to a floating point number." ) else: break # save config config["audio_file"] = file config["adjust_dB"] = adjust_dB config["play_dur_sec"] = how_long_s config["ffmpeg_path"] = ffmpeg_path with open(CONFIG_FILE, 'w') as f: json.dump(config, f) print("You can modify the audio in " + CONFIG_FILE + '.')
def download_now(self, url, codec='mp4', location='/tmp/'): try: yt = YouTube(self.url) yt.get_videos() video = yt.filter(codec)[0] video.download(location) except Exception, e: print e print "Error: Invalid URL or network problem"
def download_now(self, url, codec='mp4', location='/tmp/'): try: yt = YouTube(url) yt.get_videos() video = yt.filter(codec)[0] print 'file name:', video.filename video.download(location, on_progress=utils.print_status) except Exception, e: print "Error: Invalid URL or network problem or file exist"
def download_video(self, url): yt = YouTube(url) #~ video = yt.get('mp4') video = yt.filter('mp4')[-1] try: video.download(self.project_dir) except OSError: pass return self
def downloadVideo(self, videoList): ytVideo = videoList[0] yt = Ytdwn("http://www.youtube.com/watch?v=%s"%ytVideo._ident) filename = 'YT_%s' % ytVideo._ident yt.set_filename(filename) video = yt.filter(resolution='360p')[0] filepath = "%s/%s.%s" % (YOUTUBE_VIDEOS_LOCATION, filename, video.extension) if os.path.exists(filepath): os.remove(filepath) video.download(YOUTUBE_VIDEOS_LOCATION) ytVideo._file_path = filepath ytVideo.save()
def download(self): """ Downloads the YouTube video and sets the video's title. """ yt = YouTube('https://www.youtube.com/watch?v={0}'.format(self.youtube_id)) video = yt.filter('mp4')[-1] self.title = video.filename self.file_path = '{0}.mp4'.format(self.youtube_id) yt.set_filename(self.youtube_id) video.download(settings.YOUTUBE_DOWNLOAD_ROOT) self.downloaded = True self.save()
def download(link, path="video_index/"): yt = YouTube(link) file_name = re.sub(r'\W+', '', yt.filename.replace(" ", "_")) # Ugly ass solution to fix encoding errors later on... yt.set_filename(file_name) video_path = os.path.join(path, yt.filename) print "Downloading video: " + yt.filename # Below downloads the video in the lowest resolution available. # Change the '0' to '-1' to get the highest instead video = yt.filter('mp4')[VIDEO_RESOLUTION] video.download(path) return video_path
def download(self, url): storage = self.config.get('youtube', 'storage') yt = YouTube(url) video = yt.filter('mp4')[-1] # This will get us the highest res version. lockFile = '%s/%s.mp4.lck' % (storage, video.filename) try: open(lockFile, 'a').close() video.download(storage) os.remove(lockFile) return True except: return False
class TestYouTube(unittest.TestCase): '''Test all methods of Youtube class''' def setUp(self): '''Set up the all attributes required for a particular video''' self.url="https://www.youtube.com/watch?v=Ik-RsDGPI5Y" self.video_id = 'Ik-RsDGPI5Y' self.filename = 'Pulp Fiction - Dancing Scene' self.yt = YouTube(self.url) #: don't hard code, make is universal self.videos = ['<Video: MPEG-4 Visual (.3gp) - 144p - Simple>', '<Video: MPEG-4 Visual (.3gp) - 240p - Simple>', '<Video: Sorenson H.263 (.flv) - 240p - N/A>', '<Video: H.264 (.mp4) - 360p - Baseline>', '<Video: H.264 (.mp4) - 720p - High>', '<Video: VP8 (.webm) - 360p - N/A>'] # using flv since it has only once video self.flv = '<Video: Sorenson H.263 (.flv) - 240p - N/A>' def test_url(self): self.assertEqual(self.yt.url ,self.url) def test_video_id(self): self.assertEqual(self.yt.video_id, self.video_id ) def test_filename(self): self.assertEqual(self.yt.filename, self.filename) def test_get_videos(self): self.assertEqual ( map( str, self.yt.get_videos() ), self.videos ) def test_get_video_data(self): self.assertEqual((self.yt.get_video_data()['args']['loaderUrl']), self.url) def test_get_false(self): with self.assertRaises(MultipleObjectsReturned): self.yt.get() def test_get_true(self): self.assertEqual( str( self.yt.get('flv') ), self.flv ) def test_filter(self): self.assertEqual( str( self.yt.filter('flv')[0] ), self.flv )
def default(self, *args, **kwargs): url = cherrypy.url(qs=cherrypy.request.query_string) print url if '/watch?' in url: error = False url = url.replace('youtubell','youtube') try: yt = YouTube(url) except: error = True if not error: video = yt.filter('mp4')[-1] raise cherrypy.HTTPRedirect(video.url) else: return 'Invalid URL or the video is private or requires login.' else: return 'Incorrect input.'
def main(input, output, limit): random.seed(42) baseurl = "https://www.youtube.com/oembed?format=json&url=" validurls = [] # get links to validate with open(input) as f: links = f.readlines() # shuffle to have videos from various categories random.shuffle(links) for link in links: try: # make request response = requests.get(baseurl + link) # url is valid and video exists if we got 200 in response if response.status_code == 200: data = response.json() title = data['title'] isEnglish = detect(title) == 'en' yt = YouTube(link) canDownload = len(yt.filter('mp4')) > 0 if isEnglish and canDownload: print title print link validurls.append(link.strip()) # break if we reached given limit if len(validurls) >= limit: break except Exception as e: print e # write resulting list to the output file with open(output, mode="w+") as f: for vl in validurls: f.write("%s\n" % vl)
def download_video(self,url): # Settings yt = YouTube(url) print("Found video: " + yt.filename) filename = yt.filename.replace("!", "").replace("&","").replace(" ","-") # Finds the correct resolution res_info = yt.filter("mp4")[-1] res_settings = ["1080p", "720p", "480p", "360p", "240p", "144p"] for i in res_settings: if i in str(res_info): res = i print("Found video resolution: " + res) video = yt.get("mp4", res) # Downloads the video in the correct resolution video.download("Video/"+filename+".mp4") # Gets the audio self.audio_convert(filename)
def download_yt_video(yt_url, filename, path, sim=False): global needs global gots yt = YouTube() yt.url = yt_url yt.filename = u'{}'.format(filename.replace('/', '-').replace(':', ',')) if os.path.isfile(os.path.join(path, u'{}.mp4'.format(yt.filename))): print(' Got it!') gots += 1 else: if sim: print(' Need It!') needs += 1 else: print(' Downloading... ', end='') max_res = yt.filter('mp4')[-1].resolution video = yt.get('mp4', max_res) video.download(path, verbose=False) print('Done!') gots += 1
def download_video(url, folder): try: yt = YouTube(url) except Exception as e: print ("Error:", e, "- skipping video with url:",url) return #video should be downloaded in 720p try: vid = yt.get("mp4","720p") #else tries to get the highest resolution available except Exception: vid = yt.filter("mp4")[-1] #download video try: bar = progress_bar()#init progress_bar vid.download(folder,on_progress = bar.print_progress, on_finish = bar.print_end) print("Successfully downloaded", yt.filename, " !") except OSError: print(yt.filename, "already exists in the directory. Skipping video...") return #converts video to audio try: aud = "ffmpeg -i " + folder + "/" + "\"" + str(yt.filename) + "\"" + ".mp4 " + folder + "/" + "\"" + str(yt.filename) + "\"" + ".mp3" print (aud) os.system(aud) if os.path.exists(folder +"\\" + yt.filename + ".mp4"): os.remove(folder +"\\" + yt.filename + ".mp4") print("Succesfully converted",yt.filename, "to mp3!") except OSError: print("There are some problems with the file name(s), skipping video..") return
def lambda_handler(event, context): youtube_url = event.get('url', None) if youtube_url is None: # TODO: Raise exception return # Get youtube video yt = YouTube(youtube_url) video = yt.filter('mp4')[-1] if not video: # TODO: Raise exception return file_name = event.get('filename', video.filename + '.mp4') or 'youtube_video.mp4' # store to S3 bucket_name = 'dsotr_youtube_downloader' s3 = boto3.resource('s3') s3.create_bucket(Bucket=bucket_name) obj = s3.Object(bucket_name, file_name) obj.put(Body=urllib2.urlopen(video.url).read(), ACL='public-read') client = boto3.client('s3') presigned_url = client.generate_presigned_url('get_object', Params={'Bucket': bucket_name, 'Key': obj.key}) result = presigned_url return result
def download_videos(test=True): manually_download = [] positions = Week.objects.all()[0].position_set.all().order_by('position') songs = [p.song for p in positions][50:] for song in songs: if song.youtube_link: link = 'http://www.youtube.com/watch?v=' + song.youtube_link try: yt = YouTube(link) yt.set_filename("{} - {}".format(song.name, song.artist.name)) except AgeRestricted: manually_download.append(link) print 'Song is age restricted, adding to manually_download list.' continue try: video_type = yt.filter('mp4')[-1] video = yt.get(video_type.extension, video_type.resolution) except Exception: traceback.print_exc() continue if not os.path.exists(os.path.abspath(os.path.join(settings.RAW_VIDEOS, video.filename + ".mp4"))): print 'Downloading video: {} - {}'.format(song, video_type.resolution) video.download(settings.RAW_VIDEOS) else: print 'Video: {} already downlaoded. Skipping.'.format(song) print 'Manually download these songs: %s' % manually_download