Python youtube_dl.YoutubeDL() Examples
The following are 30
code examples of youtube_dl.YoutubeDL().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
youtube_dl
, or try the search function
.
Example #1
Source File: sound_board_utility.py From JJMumbleBot with GNU General Public License v3.0 | 14 votes |
def download_clip(url, name): ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': dir_utils.get_perm_med_dir() + f'/sound_board/{name}.wav', 'noplaylist': True, 'continue_dl': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'wav', 'preferredquality': '192', }] } try: with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.cache.remove() info_dict = ydl.extract_info(url, download=False) ydl.prepare_filename(info_dict) ydl.download([url]) return True except Exception: return False
Example #2
Source File: musictools.py From MusicTools with MIT License | 8 votes |
def download_song(song_url, song_title): """ Download a song using youtube url and song title """ outtmpl = song_title + '.%(ext)s' ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': outtmpl, 'postprocessors': [ {'key': 'FFmpegExtractAudio','preferredcodec': 'mp3', 'preferredquality': '192', }, {'key': 'FFmpegMetadata'}, ], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(song_url, download=True)
Example #3
Source File: app.py From VK-Scraper with GNU General Public License v3.0 | 8 votes |
def determine_max_video_res(item, save_dir): if 'files' in item: if 'mp4_1080' in item['files']: return item['files']['mp4_1080'] if 'mp4_720' in item['files']: return item['files']['mp4_720'] if 'mp4_480' in item['files']: return item['files']['mp4_480'] if 'mp4_360' in item['files']: return item['files']['mp4_360'] if 'mp4_240' in item['files']: return item['files']['mp4_240'] elif 'player' in item: # TODO: parse VK videos here to download user-owned private files ydl_opts = { 'outtmpl': save_dir + '/%(title)s.%(ext)s', 'noplaylist': True, 'logger': VkScraper.VideoLogger(), } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([item['player']])
Example #4
Source File: youtube.py From bulk-downloader-for-reddit with GNU General Public License v3.0 | 6 votes |
def download(self,filename,directory,url): ydl_opts = { "format": "best", "outtmpl": str(directory / (filename + ".%(ext)s")), "progress_hooks": [self._hook], "playlistend": 1, "nooverwrites": True, "quiet": True } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) location = directory/(filename+".mp4") if GLOBAL.arguments.no_dupes: try: fileHash = createHash(location) except FileNotFoundError: return None if fileHash in GLOBAL.downloadedPosts(): os.remove(location) raise FileAlreadyExistsError GLOBAL.downloadedPosts.add(fileHash)
Example #5
Source File: Download.py From youtube-dl-GUI with MIT License | 6 votes |
def download(self): ydl_options = self._prepare_ytd_options() with youtube_dl.YoutubeDL(ydl_options) as ydl: ydl.add_default_info_extractors() ydl.add_progress_hook(self.hook) try: ydl.download([self.url]) except ( youtube_dl.utils.DownloadError, youtube_dl.utils.ContentTooShortError, youtube_dl.utils.ExtractorError, youtube_dl.utils.UnavailableVideoError ) as e: self.error_occurred = True self.remove_row_signal.emit() self.remove_url_signal.emit(self.url) self.status_bar_signal.emit(str(e))
Example #6
Source File: ytdl.py From gallery-dl with GNU General Public License v2.0 | 6 votes |
def __init__(self, job): DownloaderBase.__init__(self, job) extractor = job.extractor retries = self.config("retries", extractor._retries) options = { "format": self.config("format") or None, "ratelimit": text.parse_bytes(self.config("rate"), None), "retries": retries+1 if retries >= 0 else float("inf"), "socket_timeout": self.config("timeout", extractor._timeout), "nocheckcertificate": not self.config("verify", extractor._verify), "nopart": not self.part, "updatetime": self.config("mtime", True), "proxy": extractor.session.proxies.get("http"), } options.update(self.config("raw-options") or {}) if self.config("logging", True): options["logger"] = self.log self.forward_cookies = self.config("forward-cookies", False) outtmpl = self.config("outtmpl") self.outtmpl = DEFAULT_OUTTMPL if outtmpl == "default" else outtmpl self.ytdl = YoutubeDL(options)
Example #7
Source File: download.py From lynda-video-downloader with GNU General Public License v3.0 | 6 votes |
def get_videos(url): """Download all videos in the course. """ # Lynda.com login and video filename options options = { 'username': USERNAME, 'password': PASSWORD, 'outtmpl': u'%(playlist_index)s-%(title)s.%(ext)s', 'writesubtitles': SUBTITLES, 'allsubtitles': SUBTITLES, 'download_archive': ARCHIVE, 'external_downloader': EXTERNAL_DL } try: with youtube_dl.YoutubeDL(options) as ydl: ydl.download([url]) except: print('Could not download the video in course: {}'.format(url))
Example #8
Source File: playlistfromsong.py From playlistfromsong with MIT License | 6 votes |
def downloadURL(url, preferredCodec=None, preferredQuality=None): """ Downloads song using youtube_dl and the song's youtube url. """ codec, quality = getCodecAndQuality() ydl_opts = { 'format': 'bestaudio/best', 'quiet': True, 'no_warnings': True, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': codec, 'preferredquality': quality, }, {'key': 'FFmpegMetadata'}, ], } try: with YoutubeDL(ydl_opts) as ydl: return ydl.extract_info(url, download=True) except: print("Problem downloading " + url) return None
Example #9
Source File: yt_dl.py From BotHub with Apache License 2.0 | 6 votes |
def list_formats(info_dict: dict) -> str: """YoutubeDL's list_formats method but without format notes. Args: info_dict (``dict``): Dictionary which is returned by YoutubeDL's extract_info method. Returns: ``str``: All available formats in order as a string instead of stdout. """ formats = info_dict.get('formats', [info_dict]) table = [[ f['format_id'], f['ext'], youtube_dl.YoutubeDL.format_resolution(f) ] for f in formats if f.get('preference') is None or f['preference'] >= -1000] if len(formats) > 1: table[-1][-1] += (' ' if table[-1][-1] else '') + '(best)' header_line = ['format code', 'extension', 'resolution'] fmtStr = ( '`Available formats for %s:`\n`%s`' % (info_dict['title'], youtube_dl.render_table(header_line, table))) return fmtStr
Example #10
Source File: command_line.py From MusicNow with MIT License | 6 votes |
def download_song(song_url, song_title): ''' Downloads song from youtube-dl ''' outtmpl = song_title + '.%(ext)s' ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': outtmpl, 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }, {'key': 'FFmpegMetadata'}, ], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(song_url, download=True)
Example #11
Source File: worker.py From youtube-dl-webui with GNU General Public License v2.0 | 6 votes |
def run(self): self.intercept_ydl_opts() with YoutubeDL(self.ydl_opts) as ydl: try: if self.first_run: info_dict = ydl.extract_info(self.url, download=False) # self.logger.debug(json.dumps(info_dict, indent=4)) info_dict['description'] = info_dict['description'].replace('\n', '<br />'); payload = {'tid': self.tid, 'data': info_dict} self.msg_cli.put('info_dict', payload) self.logger.info('start downloading, url - %s' %(self.url)) ydl.download([self.url]) except DownloadError as e: # url error event_handler = FatalEvent(self.tid, self.msg_cli) event_handler.invalid_url(self.url); self.msg_cli.put('worker_done', {'tid': self.tid, 'data': {}})
Example #12
Source File: extractors.py From facebook-scraper with MIT License | 6 votes |
def extract_video_highres(self): if not YoutubeDL: raise ModuleNotFoundError( "youtube-dl must be installed to download videos in high resolution." ) ydl_opts = { 'format': 'best', 'quiet': True, } try: post_id = self.post.get('post_id') video_page = 'https://www.facebook.com/' + post_id with YoutubeDL(ydl_opts) as ydl: url = ydl.extract_info(video_page, download=False)['url'] return {'video': url} except ExtractorError as ex: logger.error("Error extracting video with youtube-dl: %r", ex) return None
Example #13
Source File: bot.py From music-bot with GNU General Public License v3.0 | 6 votes |
def download(title, video_url): ydl_opts = { 'outtmpl': '{}.%(ext)s'.format(title), 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192', }], } with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([video_url]) return { 'audio': open(title + '.mp3', 'rb'), 'title': title, }
Example #14
Source File: yt_dl.py From TG-UserBot with GNU General Public License v3.0 | 6 votes |
def list_formats(info_dict: dict) -> str: """YoutubeDL's list_formats method but without format notes. Args: info_dict (``dict``): Dictionary which is returned by YoutubeDL's extract_info method. Returns: ``str``: All available formats in order as a string instead of stdout. """ formats = info_dict.get('formats', [info_dict]) table = [ [f['format_id'], f['ext'], youtube_dl.YoutubeDL.format_resolution(f)] for f in formats if f.get('preference') is None or f['preference'] >= -1000] if len(formats) > 1: table[-1][-1] += (' ' if table[-1][-1] else '') + '(best)' header_line = ['format code', 'extension', 'resolution'] fmtStr = ( '`Available formats for %s:`\n`%s`' % (info_dict['title'], youtube_dl.render_table(header_line, table)) ) return fmtStr
Example #15
Source File: test_camgear.py From vidgear with Apache License 2.0 | 6 votes |
def return_youtubevideo_params(url): """ returns Youtube Video parameters(FPS, dimensions) directly using Youtube-dl """ ydl = youtube_dl.YoutubeDL( { "outtmpl": "%(id)s%(ext)s", "noplaylist": True, "quiet": True, "format": "bestvideo", } ) with ydl: result = ydl.extract_info( url, download=False ) # We just want to extract the info return (int(result["width"]), int(result["height"]), float(result["fps"]))
Example #16
Source File: test_tubeup.py From tubeup with GNU General Public License v3.0 | 6 votes |
def test_create_basenames_from_ydl_info_dict_playlist(self): ydl = YoutubeDL() result = self.tu.create_basenames_from_ydl_info_dict( ydl, info_dict_playlist) expected_result = set([ 'Live Streaming Rafid Aslam-7gjgkH5iPaE', 'Live Streaming Rafid Aslam-q92kxPm-pqM', 'Cara Membuat Laptop Menjadi Hotspot WiFi Dengan CMD-YjFwMSDNphM', '[CSO] Defeat Boss in Dead End With Thanatos 7-EEm6MwXLse0', 'Cara Bermain Minecraft Multiplayer Dengan LAN-g2vTZ2ka-tM', 'Live Streaming Rafid Aslam-AXhuSS5_9YU', 'Cara Membuat Disk Baru di Komputer-KDOygJnK7Sw', 'Cara Mendownload Lewat Torrent-cC-9RghkvXs'] ) self.assertEqual(result, expected_result)
Example #17
Source File: theaterTrailers.py From TheaterTrailers with GNU General Public License v3.0 | 6 votes |
def videoDownloader(string, passedTitle, yearVar): # Options for the video downloader ydl1_opts = { 'outtmpl': os.path.join(TheaterTrailersHome, 'Trailers', '{0} ({1})'.format(passedTitle, yearVar), '{0} ({1}).mp4'.format(passedTitle, yearVar)), 'ignoreerrors': True, 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', } with youtube_dl.YoutubeDL(ydl1_opts) as ydl: logger.info("downloading {0} from {1}".format(passedTitle, string)) ydl.download([string]) shutil.copy2( os.path.join(trailerLocation, '{0} ({1})'.format(passedTitle, yearVar), '{0} ({1}).mp4'.format(passedTitle, yearVar)), os.path.join(trailerLocation, '{0} ({1})'.format(passedTitle, yearVar), '{0} ({1})-trailer.mp4'.format(passedTitle, yearVar)) ) shutil.copy2( os.path.join(TheaterTrailersHome, 'res', 'poster.jpg'), os.path.join(trailerLocation, '{0} ({1})'.format(passedTitle, yearVar)) ) updatePlex() # Downloads info for the videos from the playlist
Example #18
Source File: videoDownloader.py From Liked-Saved-Image-Downloader with MIT License | 6 votes |
def shouldUseYoutubeDl(url): for siteMask in youtubeDlSitesSupported: if siteMask in url: isBlacklisted = False for blacklistSite in youtubeDlBlacklistSites: if blacklistSite in url: isBlacklisted = True break # Use the gfycat api for these, if available # Ever since the creation of RedGifs, use YoutubeDL for all gfycat links... # if settings.settings['Gfycat_Client_id'] and 'gfycat.com' in url: # isBlacklisted = True if isBlacklisted: # We probably have another downloader return False # YoutubeDL should support this site return True return False # Returns (success or failure, output file or failure message)
Example #19
Source File: url.py From botamusique with MIT License | 6 votes |
def _get_info_from_url(self): self.log.info("url: fetching metadata of url %s " % self.url) ydl_opts = { 'noplaylist': True } succeed = False with youtube_dl.YoutubeDL(ydl_opts) as ydl: attempts = var.config.getint('bot', 'download_attempts', fallback=2) for i in range(attempts): try: info = ydl.extract_info(self.url, download=False) self.duration = info['duration'] self.title = info['title'] self.keywords = info['title'] succeed = True return True except youtube_dl.utils.DownloadError: pass except KeyError: # info has no 'duration' break if not succeed: self.ready = 'failed' self.log.error("url: error while fetching info from the URL") raise ValidationFailedError(constants.strings('unable_download', item=self.format_title()))
Example #20
Source File: music.py From apex-sigma-core with GNU General Public License v3.0 | 6 votes |
def __init__(self, requester, item_info): """ :param requester: The user that queued the item. :type requester: discord.Member :param item_info: Document containing the queued item's details. :type item_info: dict """ self.requester = requester self.item_info = item_info self.url = self.item_info.get('webpage_url') self.video_id = self.item_info.get('id', self.url) self.uploader = self.item_info.get('uploader', 'Unknown') self.title = self.item_info.get('title') self.thumbnail = self.item_info.get('thumbnail', 'https://i.imgur.com/CGPNJDT.png') self.duration = int(self.item_info.get('duration', 0)) self.downloaded = False self.loop = asyncio.get_event_loop() self.threads = ThreadPoolExecutor() self.ytdl_params = ytdl_params self.ytdl = youtube_dl.YoutubeDL(self.ytdl_params) self.token = self.tokenize() self.location = None
Example #21
Source File: youtube.py From raveberry with GNU Lesser General Public License v3.0 | 6 votes |
def check_available(self) -> bool: try: with youtube_dl.YoutubeDL(self.ydl_opts) as ydl: self.info_dict = ydl.extract_info(self.query, download=False) except (youtube_dl.utils.ExtractorError, youtube_dl.utils.DownloadError) as e: self.error = e return False # this value is not an exact match, but it's a good approximation if "entries" in self.info_dict: self.info_dict = self.info_dict["entries"][0] self.id = self.info_dict["id"] size = self.info_dict["filesize"] max_size = self.musiq.base.settings.basic.max_download_size * 1024 * 1024 if ( max_size != 0 and self.check_cached() is None and (size is not None and size > max_size) ): self.error = "Song too long" return False return True
Example #22
Source File: test_youtube.py From raveberry with GNU Lesser General Public License v3.0 | 6 votes |
def setUp(self): super().setUp() try: # try to find out whether youtube is happy with us this time # send a request and skip the test if there is an error ydl_opts = Youtube.get_ydl_opts() ydl_opts["logger"] = YoutubeDLLogger(self) with youtube_dl.YoutubeDL(ydl_opts) as ydl: self.info_dict = ydl.download( ["https://www.youtube.com/watch?v=wobbf3lb2nk"] ) except (youtube_dl.utils.ExtractorError, youtube_dl.utils.DownloadError) as e: self.skipTest(f"Error when interacting with youtube, skipping test: {e}") # reduce the number for youtube playlists self.client.post(reverse("set_max_playlist_items"), {"value": "3"}) # clear test cache; ensure that it's the test directory if os.path.split(os.path.dirname(settings.SONGS_CACHE_DIR))[1] == "test_cache": for member in os.listdir(settings.SONGS_CACHE_DIR): member_path = os.path.join(settings.SONGS_CACHE_DIR, member) if os.path.isfile(member_path): os.remove(member_path)
Example #23
Source File: youtube_helper.py From JJMumbleBot with GNU General Public License v3.0 | 6 votes |
def download_next(): queue_list = list(YoutubeHelper.queue_instance.queue_storage) if len(queue_list) > 0: youtube_url = queue_list[-1]['std_url'] else: return if os.path.exists(f"{dir_utils.get_temp_med_dir()}/youtube/{queue_list[-1]['img_id']}.jpg"): dprint(f"Thumbnail exists for {queue_list[-1]['img_id']}.jpg...skipping") return try: with youtube_dl.YoutubeDL(YoutubeHelper.ydl_opts) as ydl: ydl.cache.remove() ydl.extract_info(youtube_url, download=True) except youtube_dl.utils.DownloadError as e: dprint(e) return return
Example #24
Source File: youtube_helper.py From JJMumbleBot with GNU General Public License v3.0 | 6 votes |
def download_song_name(url): try: with youtube_dl.YoutubeDL(YoutubeHelper.ydl_opts) as ydl: ydl.cache.remove() info_dict = ydl.extract_info(url, download=True) if info_dict['duration'] >= YoutubeHelper.max_track_duration: return None if info_dict['duration'] <= 0.1: return None prep_struct = { 'std_url': url, 'main_url': info_dict['url'], 'main_title': info_dict['title'], 'img_id': info_dict['id'], 'duration': info_dict['duration'] } return prep_struct except youtube_dl.utils.DownloadError: return None
Example #25
Source File: youtube-dl-server.py From youtube-dl-server with MIT License | 5 votes |
def download(url, request_options): with youtube_dl.YoutubeDL(get_ydl_options(request_options)) as ydl: ydl.download([url])
Example #26
Source File: theaterTrailers.py From TheaterTrailers with GNU General Public License v3.0 | 5 votes |
def infoDownloader(playlist): # Options for the info downloader ydl_opts = { 'skip_download': True, 'ignoreerrors': True, 'playlistreverse': True, 'playliststart': 1, 'playlistend': playlistEndVar, 'quiet': False, 'matchtitle': '.*\\btrailer\\b.*', 'extract_flat': True, } with youtube_dl.YoutubeDL(ydl_opts) as ydl: info = ydl.extract_info(playlist) for x in info['entries']: MovieVar = x['title'].encode('ascii',errors='ignore') MovieVar = MovieVar.replace(':', '') if 'Official' in MovieVar: regexedTitle = re.search('^.*(?=(Official))', MovieVar) elif 'Trailer' in MovieVar: regexedTitle = re.search('.*?(?=Trailer)', MovieVar) elif redBand == True: if 'Red Band' in MovieVar: regexedTitle = re.search('.*?(?=Red)', MovieVar) else: # Throws out edge cases continue trailerYear = re.search('(?<=\().*(?=\))', MovieVar) TempDict = { 'url' : info['entries'][info['entries'].index(x)]['url']} movieTitleUntrimmed = regexedTitle.group(0).strip() movieTitle = fixTitle(movieTitleUntrimmed) MovieDict[movieTitle] = TempDict try: MovieDict[movieTitle]['Trailer Release'] = trailerYear.group(0) except AttributeError: pass MovieDict[movieTitle]['Movie Title'] = movieTitle MovieList.append(movieTitle)
Example #27
Source File: voice.py From jishaku with MIT License | 5 votes |
def __init__(self, url, download: bool = False): ytdl = youtube_dl.YoutubeDL(BASIC_OPTS) info = ytdl.extract_info(url, download=download) super().__init__(info['url'])
Example #28
Source File: dlyt.py From wuy with GNU General Public License v2.0 | 5 votes |
def get(self,q): if "http" in q.lower(): u=q.strip() else: u='https://www.youtube.com/watch?v='+q y = yt.YoutubeDL() return y.extract_info(u, download=False)
Example #29
Source File: downloader.py From rhinobot_heroku with MIT License | 5 votes |
def __init__(self, download_folder=None): self.thread_pool = ThreadPoolExecutor(max_workers=2) self.unsafe_ytdl = youtube_dl.YoutubeDL(ytdl_format_options) self.safe_ytdl = youtube_dl.YoutubeDL(ytdl_format_options) self.safe_ytdl.params['ignoreerrors'] = True self.download_folder = download_folder if download_folder: otmpl = self.unsafe_ytdl.params['outtmpl'] self.unsafe_ytdl.params['outtmpl'] = os.path.join(download_folder, otmpl) # print("setting template to " + os.path.join(download_folder, otmpl)) otmpl = self.safe_ytdl.params['outtmpl'] self.safe_ytdl.params['outtmpl'] = os.path.join(download_folder, otmpl)
Example #30
Source File: sum.py From vidsum with GNU General Public License v3.0 | 5 votes |
def download_video_srt(subs): """ Downloads specified Youtube video's subtitles as a vtt/srt file. Args: subs(str): Full url of Youtube video Returns: True The video will be downloaded as 1.mp4 and its subtitles as 1.(lang).srt Both, the video and its subtitles, will be downloaded to the same location as that of this script (sum.py) """ ydl_opts = { 'format': 'best', 'outtmpl': '1.%(ext)s', 'subtitlesformat': 'srt', 'writeautomaticsub': True, # 'allsubtitles': True # Get all subtitles } movie_filename = "" subtitle_filename = "" with youtube_dl.YoutubeDL(ydl_opts) as ydl: # ydl.download([subs]) result = ydl.extract_info("{}".format(url), download=True) movie_filename = ydl.prepare_filename(result) subtitle_info = result.get("requested_subtitles") subtitle_language = subtitle_info.keys()[0] subtitle_ext = subtitle_info.get(subtitle_language).get("ext") subtitle_filename = movie_filename.replace(".mp4", ".%s.%s" % (subtitle_language, subtitle_ext)) return movie_filename, subtitle_filename