本文整理汇总了Python中youtube_dl.YoutubeDL.download方法的典型用法代码示例。如果您正苦于以下问题:Python YoutubeDL.download方法的具体用法?Python YoutubeDL.download怎么用?Python YoutubeDL.download使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类youtube_dl.YoutubeDL
的用法示例。
在下文中一共展示了YoutubeDL.download方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ScramDLThread
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
class ScramDLThread(threading.Thread):
def __init__(self, urls, addendum_params=None):
threading.Thread.__init__(self)
override_params = dict()
override_params['writeinfojson'] = False
override_params['test'] = False
override_params['format'] = 'mp4'
if addendum_params is not None:
override_params.update(addendum_params)
params = get_params(override_params)
self.scram_dl = YoutubeDL(params)
self.scram_dl.add_progress_hook(self.hook_progress)
self.scram_dl.add_default_info_extractors()
self.is_finished = False
self.urls = urls
def hook_progress(self, status):
if status['status'] == 'finished':
self.is_finished = True
else:
self.is_finished = False
def run(self):
self.scram_dl.download(self.urls)
示例2: test_info_json
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def test_info_json(self):
expected = list(EXPECTED_ANNOTATIONS) #Two annotations could have the same text.
ie = youtube_dl.extractor.YoutubeIE()
ydl = YoutubeDL(params)
ydl.add_info_extractor(ie)
ydl.download([TEST_ID])
self.assertTrue(os.path.exists(ANNOTATIONS_FILE))
annoxml = None
with io.open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as annof:
annoxml = xml.etree.ElementTree.parse(annof)
self.assertTrue(annoxml is not None, 'Failed to parse annotations XML')
root = annoxml.getroot()
self.assertEqual(root.tag, 'document')
annotationsTag = root.find('annotations')
self.assertEqual(annotationsTag.tag, 'annotations')
annotations = annotationsTag.findall('annotation')
#Not all the annotations have TEXT children and the annotations are returned unsorted.
for a in annotations:
self.assertEqual(a.tag, 'annotation')
if a.get('type') == 'text':
textTag = a.find('TEXT')
text = textTag.text
self.assertTrue(text in expected) #assertIn only added in python 2.7
#remove the first occurance, there could be more than one annotation with the same text
expected.remove(text)
#We should have seen (and removed) all the expected annotation texts.
self.assertEqual(len(expected), 0, 'Not all expected annotations were found.')
示例3: process_url
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def process_url(self, source, global_opts):
"""Main method for processing urls
This method basically hands over the configuration to YoutubeDL and repeats
the step until every source in the configuration was read
"""
ydl = YoutubeDL()
ydl.add_default_info_extractors()
sourceUrl = source['url']
sourceDescription = source.get("description", "")
self._logsource(
"Processing source: '" + sourceDescription +
"' Url: '" + sourceUrl + "'", source)
# Merge local parameters with global ones
ydl.params = copy.copy(global_opts)
ydl.params.update(source)
prefix = ""
ydl.params['match_filter'] = (
None if 'match_filter' not in ydl.params or ydl.params['match_filter'] is None
else match_filter_func(ydl.params['match_filter']))
# Settings by commute tube over the standard settings, respect if the config sets them differently
if 'format' not in ydl.params and 'format_limit' not in ydl.params:
ydl.params['format'] = "bestvideo+bestaudio/best" if 'format' not in self.config else self.config["format"]
if 'nooverwrites' not in ydl.params:
ydl.params['nooverwrites'] = True
if 'ignoreerrors' not in ydl.params:
ydl.params['ignoreerrors'] = True
if 'download_archive' not in ydl.params:
ydl.params['download_archive'] = self.download_archive
if 'prefix' in ydl.params:
prefix = ydl.params['prefix']
ydl.params['restrictfilenames'] = True
ydl.params['logger'] = self.ydlLog
outtmpl = self.pathToDownloadFolder + "/" + prefix + \
'%(uploader)s-%(title)s.%(ext)s'
if 'outtmpl' not in ydl.params:
ydl.params['outtmpl'] = outtmpl
elif not (ydl.params['outtmpl'].startswith(self.pathToDownloadFolder)):
self._logsource("Prefixing custom set outtmpl with '" + self.pathToDownloadFolder + "/" + prefix + "'", source)
ydl.params['outtmpl'] = self.pathToDownloadFolder + "/" + prefix + \
ydl.params['outtmpl']
if self.debug:
self._logsource(
"All downloads will be simulated since this is debug mode", source)
ydl.params['simulate'] = True
ydl.download([source['url']])
示例4: download
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def download(self, url=None, output=None):
opts = {
'outtmpl':
os.path.join(
output, '%(title)s-%(id)s.%(ext)s'
),
'progress_hooks': [self.progress_hook],
}
y = YoutubeDL(params=opts)
y.download([url])
示例5: download
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def download(link, files_present, avg):
'''gets the playlist and Downloads the videos that i dont have'''
#url = 'https://www.youtube.com/watch?v=MCs5OvhV9S4'
url = 'https://www.youtube.com/playlist?list=PLwyG5wA5gIzjhW36BxGBoQwUZHnPDFux3'
ydl=YoutubeDL()
ydl.add_default_info_extractors()
playlist = ydl.extract_info(url, download=False)
for video in playlist['entries']:
import ipdb; ipdb.set_trace()
if video['title'] in files_present:
print ("Video #{} {} is present and will be ignored").format(video['playlist_index'], video['title'])
else:
print ("currently downloading: {}").format(video['title'])
ydl.download(video['webpage_url'])
示例6: start_youtube_dl
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def start_youtube_dl(self):
# Start downloading the specified url
if self._output_path.get():
output_path = self._output_path.get()
else:
try:
output_path = os.path.dirname(os.path.abspath(__file__))
except NameError:
import sys
output_path = os.path.dirname(os.path.abspath(sys.argv[0]))
output_tmpl = output_path + '/%(title)s-%(id)s.%(ext)s'
options = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio/best',
'merge_output_format': 'mp4',
'socket_timeout': '15',
'progress_hooks': [self._logger.log],
'ignoreerrors': True,
'outtmpl': output_tmpl,
}
if self._extract_audio.get():
options['format'] = 'bestaudio/best',
options['postprocessors'] = [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '3',
}]
dl = YoutubeDL(options)
status = dl.download([self._video_url.get()])
if status != 0:
mbox.showerror("youtube-dl error", "An error happened whilst processing your video(s)")
else:
mbox.showinfo("youtube-dl finished", "Your video(s) have been successfully processed")
示例7: run
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def run(self, release_task_pk: int, custom_options: Optional[dict]=None) -> None:
try:
with transaction.atomic():
self.download_task = DownloadTask.objects.get(pk=release_task_pk)
self.download_task.downloader = DownloadTask.YOUTUBE_DL
self.download_task.celery_id = self.request.id
self.download_task.state = DownloadTask.DOWNLOADING
self.download_task.started_at = timezone.now()
self.download_task.save()
options = copy.deepcopy(self.YOUTUBE_DL_OPTIONS)
options['outtmpl'] = options['outtmpl'].format(self.request.id.replace('-', ''))
options['progress_hooks'] = [self._progress_hook]
if custom_options:
custom_options = self._clean_options(custom_options)
options.update(custom_options)
downloader = YoutubeDL(options)
downloader.add_post_processor(self._get_postprocessor())
downloader.download([self.download_task.url])
except SystemExit:
try:
os.remove(self._last_status['tmpfilename'])
except AttributeError:
return
except:
log.exception('Exception while removing temporary file. id={0}, tempfilename={1}'.format(
release_task_pk, self._last_status['tmpfilename'])
)
raise
except (BaseDatabaseError, ObjectDoesNotExist, MultipleObjectsReturned):
# Hope we've caught everything
log.exception('Exception while updating DownloadTask. id={0}'.format(release_task_pk))
raise
except:
try:
with transaction.atomic():
self.download_task.state = DownloadTask.ERROR
self.download_task.finished_at = timezone.now()
self.download_task.save()
self.download_task.tracebacks.create(text=traceback.format_exc())
raise
except (BaseDatabaseError, ObjectDoesNotExist, MultipleObjectsReturned):
log.exception('Exception while changing DownloadTask.state to ERROR. id={0}'.format(release_task_pk))
raise
示例8: on_task_output
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def on_task_output(self, task, config):
import youtube_dl.YoutubeDL
from youtube_dl.utils import ExtractorError
class YoutubeDL(youtube_dl.YoutubeDL):
def __init__(self, *args, **kwargs):
self.to_stderr = self.to_screen
self.processed_info_dicts = []
super(YoutubeDL, self).__init__(*args, **kwargs)
def report_warning(self, message):
raise ExtractorError(message)
def process_info(self, info_dict):
self.processed_info_dicts.append(info_dict)
return super(YoutubeDL, self).process_info(info_dict)
for entry in task.accepted:
if task.options.test:
log.info('Would download %s' % entry['title'])
else:
try:
outtmpl = entry.render(config['path']) + '/' + pathscrub(entry.render(config['template']) + '.%(ext)s', filename=True)
log.info("Output file: %s" % outtmpl)
except RenderError as e:
log.error('Error setting output file: %s' % e)
entry.fail('Error setting output file: %s' % e)
params = {'quiet': True, 'outtmpl': outtmpl}
if 'username' in config and 'password' in config:
params.update({'username': config['username'], 'password': config['password']})
elif 'username' in config or 'password' in config:
log.error('Both username and password is required')
if 'videopassword' in config:
params.update({'videopassword': config['videopassword']})
if 'title' in config:
params.update({'title': config['title']})
ydl = YoutubeDL(params)
ydl.add_default_info_extractors()
log.info('Downloading %s' % entry['title'])
try:
ydl.download([entry['url']])
except ExtractorError as e:
log.error('Youtube-DL was unable to download the video. Error message %s' % e.message)
entry.fail('Youtube-DL was unable to download the video. Error message %s' % e.message)
except Exception as e:
log.error('Youtube-DL failed. Error message %s' % e.message)
entry.fail('Youtube-DL failed. Error message %s' % e.message)
示例9: _download_restricted
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def _download_restricted(url, filename, age):
""" Returns true iff the file has been downloaded """
params = {
'age_limit': age,
'skip_download': True,
'writeinfojson': True,
"outtmpl": "%(id)s.%(ext)s",
}
ydl = YoutubeDL(params)
ydl.add_default_info_extractors()
json_filename = filename + '.info.json'
try_rm(json_filename)
ydl.download([url])
res = os.path.exists(json_filename)
try_rm(json_filename)
return res
示例10: processUrl
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def processUrl(self, source):
ydl = YoutubeDL()
ydl.add_default_info_extractors()
sourceUrl = source['url'].decode()
sourceDescription = ""
if 'description' in source:
sourceDescription = source['description'].decode()
self.log.info(
"Processing source: '" + sourceDescription
+ "' Url: '" + sourceUrl + "'")
ydl.params = source
prefix = ""
if 'format' not in ydl.params and 'format_limit' not in ydl.params:
ydl.params['format'] = "bestvideo+bestaudio"
if 'nooverwrites' not in ydl.params:
ydl.params['nooverwrites'] = True
if 'ignoreerrors' not in ydl.params:
ydl.params['ignoreerrors'] = True
if 'download_archive' not in ydl.params:
ydl.params['download_archive'] = "already_downloaded.txt"
if 'prefix' in ydl.params:
prefix = ydl.params['prefix']
ydl.params['restrictfilenames'] = True
ydl.params['logger'] = self.ydlLog
outtmpl = self.pathToDownloadFolder + "/" + prefix + \
u'%(uploader)s-%(title)s-%(id)s.%(ext)s'
if 'outtmpl' not in ydl.params:
ydl.params['outtmpl'] = outtmpl
if self.debug is True:
self.log.debug(
"All downloads will be simulated since this is debug mode")
ydl.params['simulate'] = True
ydl.download([source['url']])
示例11: test_info_json
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def test_info_json(self):
ie = youtube_dl.extractor.YoutubeIE()
ydl = YoutubeDL(params)
ydl.add_info_extractor(ie)
ydl.download([TEST_ID])
self.assertTrue(os.path.exists(INFO_JSON_FILE))
with io.open(INFO_JSON_FILE, 'r', encoding='utf-8') as jsonf:
jd = json.load(jsonf)
self.assertEqual(jd['upload_date'], u'20121002')
self.assertEqual(jd['description'], EXPECTED_DESCRIPTION)
self.assertEqual(jd['id'], TEST_ID)
self.assertEqual(jd['extractor'], 'youtube')
self.assertEqual(jd['title'], u'''youtube-dl test video "'/\ä↭𝕐''')
self.assertEqual(jd['uploader'], 'Philipp Hagemeister')
self.assertTrue(os.path.exists(DESCRIPTION_FILE))
with io.open(DESCRIPTION_FILE, 'r', encoding='utf-8') as descf:
descr = descf.read()
self.assertEqual(descr, EXPECTED_DESCRIPTION)
示例12: download_video
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def download_video(url,google_user_dict):
if google_user_dict["google_username"] != None and google_user_dict["google_passwd"]:
params = {"username":google_user_dict["google_username"],"password":google_user_dict["google_passwd"]}
else:
params = {}
downloader = YoutubeDL(params)
res = downloader.download([url])
filename = downloader.prepare_filename(res[1])
ret_code = res[0]
video_info = res[1]
return video_info
示例13: download_youtube
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def download_youtube(url,filepath,params=None):
tmp_filepath = compat_str(filepath)
print "download to",tmp_filepath
params = params or settings.youtube_params
params.update({"outtmpl":tmp_filepath,"daterange":DateRange(None,None)})
y = YoutubeDL(params)
#y = YoutubeDL({"format":"18/34/35/5/17","outtmpl":filepath})
#y.print_debug_header()
y.add_default_info_extractors()
y.add_post_processor(FFmpegExtractAudioPP(preferredcodec="m4a",preferredquality=5, nopostoverwrites=False))
value = y.download([url])
#cmd = 'youtube-dl {url} --extract-audio --audio-format wav -o {filepath}'.format(url=url,filepath=filepath)
#print cmd
#result = subprocess.call(cmd,shell=True)
#print result
return True
示例14: test_template
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def test_template(self):
ie = youtube_dl.extractor.get_info_extractor(test_case['name'])
other_ies = [get_info_extractor(ie_key) for ie_key in test_case.get('add_ie', [])]
def print_skipping(reason):
print('Skipping %s: %s' % (test_case['name'], reason))
if not ie.working():
print_skipping('IE marked as not _WORKING')
return
if 'playlist' not in test_case:
info_dict = test_case.get('info_dict', {})
if not test_case.get('file') and not (info_dict.get('id') and info_dict.get('ext')):
print_skipping('The output file cannot be know, the "file" '
'key is missing or the info_dict is incomplete')
return
if 'skip' in test_case:
print_skipping(test_case['skip'])
return
for other_ie in other_ies:
if not other_ie.working():
print_skipping(u'test depends on %sIE, marked as not WORKING' % other_ie.ie_key())
return
params = get_params(test_case.get('params', {}))
ydl = YoutubeDL(params)
ydl.add_default_info_extractors()
finished_hook_called = set()
def _hook(status):
if status['status'] == 'finished':
finished_hook_called.add(status['filename'])
ydl.add_progress_hook(_hook)
def get_tc_filename(tc):
return tc.get('file') or ydl.prepare_filename(tc.get('info_dict', {}))
test_cases = test_case.get('playlist', [test_case])
def try_rm_tcs_files():
for tc in test_cases:
tc_filename = get_tc_filename(tc)
try_rm(tc_filename)
try_rm(tc_filename + '.part')
try_rm(os.path.splitext(tc_filename)[0] + '.info.json')
try_rm_tcs_files()
try:
try_num = 1
while True:
try:
ydl.download([test_case['url']])
except (DownloadError, ExtractorError) as err:
# Check if the exception is not a network related one
if not err.exc_info[0] in (compat_urllib_error.URLError, socket.timeout, UnavailableVideoError, compat_http_client.BadStatusLine) or (err.exc_info[0] == compat_HTTPError and err.exc_info[1].code == 503):
raise
if try_num == RETRIES:
report_warning(u'Failed due to network errors, skipping...')
return
print('Retrying: {0} failed tries\n\n##########\n\n'.format(try_num))
try_num += 1
else:
break
for tc in test_cases:
tc_filename = get_tc_filename(tc)
if not test_case.get('params', {}).get('skip_download', False):
self.assertTrue(os.path.exists(tc_filename), msg='Missing file ' + tc_filename)
self.assertTrue(tc_filename in finished_hook_called)
info_json_fn = os.path.splitext(tc_filename)[0] + '.info.json'
self.assertTrue(os.path.exists(info_json_fn))
if 'md5' in tc:
md5_for_file = _file_md5(tc_filename)
self.assertEqual(md5_for_file, tc['md5'])
with io.open(info_json_fn, encoding='utf-8') as infof:
info_dict = json.load(infof)
for (info_field, expected) in tc.get('info_dict', {}).items():
if isinstance(expected, compat_str) and expected.startswith('md5:'):
got = 'md5:' + md5(info_dict.get(info_field))
else:
got = info_dict.get(info_field)
self.assertEqual(expected, got,
u'invalid value for field %s, expected %r, got %r' % (info_field, expected, got))
# If checkable fields are missing from the test case, print the info_dict
test_info_dict = dict((key, value if not isinstance(value, compat_str) or len(value) < 250 else 'md5:' + md5(value))
for key, value in info_dict.items()
if value and key in ('title', 'description', 'uploader', 'upload_date', 'uploader_id', 'location'))
if not all(key in tc.get('info_dict', {}).keys() for key in test_info_dict.keys()):
sys.stderr.write(u'\n"info_dict": ' + json.dumps(test_info_dict, ensure_ascii=False, indent=4) + u'\n')
# Check for the presence of mandatory fields
for key in ('id', 'url', 'title', 'ext'):
self.assertTrue(key in info_dict.keys() and info_dict[key])
# Check for mandatory fields that are automatically set by YoutubeDL
for key in ['webpage_url', 'extractor', 'extractor_key']:
self.assertTrue(info_dict.get(key), u'Missing field: %s' % key)
finally:
try_rm_tcs_files()
示例15: test_template
# 需要导入模块: from youtube_dl import YoutubeDL [as 别名]
# 或者: from youtube_dl.YoutubeDL import download [as 别名]
def test_template(self):
ie = youtube_dl.extractor.get_info_extractor(test_case['name'])
other_ies = [get_info_extractor(ie_key) for ie_key in test_case.get('add_ie', [])]
def print_skipping(reason):
print('Skipping %s: %s' % (test_case['name'], reason))
if not ie.working():
print_skipping('IE marked as not _WORKING')
return
if 'playlist' not in test_case:
info_dict = test_case.get('info_dict', {})
if not test_case.get('file') and not (info_dict.get('id') and info_dict.get('ext')):
raise Exception('Test definition incorrect. The output file cannot be known. Are both \'id\' and \'ext\' keys present?')
if 'skip' in test_case:
print_skipping(test_case['skip'])
return
for other_ie in other_ies:
if not other_ie.working():
print_skipping(u'test depends on %sIE, marked as not WORKING' % other_ie.ie_key())
return
params = get_params(test_case.get('params', {}))
ydl = YoutubeDL(params)
ydl.add_default_info_extractors()
finished_hook_called = set()
def _hook(status):
if status['status'] == 'finished':
finished_hook_called.add(status['filename'])
ydl.add_progress_hook(_hook)
def get_tc_filename(tc):
return tc.get('file') or ydl.prepare_filename(tc.get('info_dict', {}))
test_cases = test_case.get('playlist', [test_case])
def try_rm_tcs_files():
for tc in test_cases:
tc_filename = get_tc_filename(tc)
try_rm(tc_filename)
try_rm(tc_filename + '.part')
try_rm(os.path.splitext(tc_filename)[0] + '.info.json')
try_rm_tcs_files()
try:
try_num = 1
while True:
try:
ydl.download([test_case['url']])
except (DownloadError, ExtractorError) as err:
# Check if the exception is not a network related one
if not err.exc_info[0] in (compat_urllib_error.URLError, socket.timeout, UnavailableVideoError, compat_http_client.BadStatusLine) or (err.exc_info[0] == compat_HTTPError and err.exc_info[1].code == 503):
raise
if try_num == RETRIES:
report_warning(u'Failed due to network errors, skipping...')
return
print('Retrying: {0} failed tries\n\n##########\n\n'.format(try_num))
try_num += 1
else:
break
for tc in test_cases:
tc_filename = get_tc_filename(tc)
if not test_case.get('params', {}).get('skip_download', False):
self.assertTrue(os.path.exists(tc_filename), msg='Missing file ' + tc_filename)
self.assertTrue(tc_filename in finished_hook_called)
info_json_fn = os.path.splitext(tc_filename)[0] + '.info.json'
self.assertTrue(os.path.exists(info_json_fn))
if 'md5' in tc:
md5_for_file = _file_md5(tc_filename)
self.assertEqual(md5_for_file, tc['md5'])
with io.open(info_json_fn, encoding='utf-8') as infof:
info_dict = json.load(infof)
expect_info_dict(self, tc.get('info_dict', {}), info_dict)
finally:
try_rm_tcs_files()