-#!/usr/bin/env python
+#!/usr/bin/env python3
# coding: utf-8
from __future__ import unicode_literals
compat_getpass,
workaround_optparse_bug9161,
)
+from .cookies import SUPPORTED_BROWSERS
from .utils import (
DateRange,
decodeOption,
DownloadError,
+ error_to_compat_str,
ExistingVideoReached,
expand_path,
match_filter_func,
preferredencoding,
read_batch_urls,
RejectedVideoReached,
- REMUX_EXTENSIONS,
render_table,
SameFileError,
setproctitle,
std_headers,
write_string,
)
-from .update import update_self
+from .update import run_update
from .downloader import (
FileDownloader,
)
from .extractor import gen_extractors, list_extractors
from .extractor.common import InfoExtractor
from .extractor.adobepass import MSO_INFO
+from .postprocessor.ffmpeg import (
+ FFmpegExtractAudioPP,
+ FFmpegSubtitlesConvertorPP,
+ FFmpegThumbnailsConvertorPP,
+ FFmpegVideoConvertorPP,
+ FFmpegVideoRemuxerPP,
+)
from .postprocessor.metadatafromfield import MetadataFromFieldPP
from .YoutubeDL import YoutubeDL
setproctitle('yt-dlp')
parser, opts, args = parseOpts(argv)
+ warnings = []
# Set user agent
if opts.user_agent is not None:
parser.error('account username missing\n')
if opts.ap_password is not None and opts.ap_username is None:
parser.error('TV Provider account username missing\n')
- if opts.outtmpl is not None and (opts.usetitle or opts.autonumber or opts.useid):
- parser.error('using output template conflicts with using title, video ID or auto number')
if opts.autonumber_size is not None:
if opts.autonumber_size <= 0:
parser.error('auto number size must be positive')
if opts.autonumber_start is not None:
if opts.autonumber_start < 0:
parser.error('auto number start must be positive or 0')
- if opts.usetitle and opts.useid:
- parser.error('using title conflicts with using video ID')
if opts.username is not None and opts.password is None:
opts.password = compat_getpass('Type account password and press [Return]: ')
if opts.ap_username is not None and opts.ap_password is None:
if numeric_limit is None:
parser.error('invalid rate limit specified')
opts.ratelimit = numeric_limit
+ if opts.throttledratelimit is not None:
+ numeric_limit = FileDownloader.parse_bytes(opts.throttledratelimit)
+ if numeric_limit is None:
+ parser.error('invalid rate limit specified')
+ opts.throttledratelimit = numeric_limit
if opts.min_filesize is not None:
numeric_limit = FileDownloader.parse_bytes(opts.min_filesize)
if numeric_limit is None:
parser.error('requests sleep interval must be positive or 0')
if opts.ap_mso and opts.ap_mso not in MSO_INFO:
parser.error('Unsupported TV Provider, use --ap-list-mso to get a list of supported TV Providers')
- if opts.overwrites:
- # --yes-overwrites implies --no-continue
+ if opts.overwrites: # --yes-overwrites implies --no-continue
opts.continue_dl = False
if opts.concurrent_fragment_downloads <= 0:
raise ValueError('Concurrent fragments must be positive')
if opts.playlistend not in (-1, None) and opts.playlistend < opts.playliststart:
raise ValueError('Playlist end must be greater than playlist start')
if opts.extractaudio:
- if opts.audioformat not in ['best', 'aac', 'flac', 'mp3', 'm4a', 'opus', 'vorbis', 'wav']:
+ if opts.audioformat not in ['best'] + list(FFmpegExtractAudioPP.SUPPORTED_EXTS):
parser.error('invalid audio format specified')
if opts.audioquality:
opts.audioquality = opts.audioquality.strip('k').strip('K')
if not opts.audioquality.isdigit():
parser.error('invalid audio quality specified')
if opts.recodevideo is not None:
- if opts.recodevideo not in REMUX_EXTENSIONS:
- parser.error('invalid video recode format specified')
+ opts.recodevideo = opts.recodevideo.replace(' ', '')
+ if not re.match(FFmpegVideoConvertorPP.FORMAT_RE, opts.recodevideo):
+ parser.error('invalid video remux format specified')
if opts.remuxvideo is not None:
opts.remuxvideo = opts.remuxvideo.replace(' ', '')
- remux_regex = r'{0}(?:/{0})*$'.format(r'(?:\w+>)?(?:%s)' % '|'.join(REMUX_EXTENSIONS))
- if not re.match(remux_regex, opts.remuxvideo):
+ if not re.match(FFmpegVideoRemuxerPP.FORMAT_RE, opts.remuxvideo):
parser.error('invalid video remux format specified')
if opts.convertsubtitles is not None:
- if opts.convertsubtitles not in ['srt', 'vtt', 'ass', 'lrc']:
+ if opts.convertsubtitles not in FFmpegSubtitlesConvertorPP.SUPPORTED_EXTS:
parser.error('invalid subtitle format specified')
+ if opts.convertthumbnails is not None:
+ if opts.convertthumbnails not in FFmpegThumbnailsConvertorPP.SUPPORTED_EXTS:
+ parser.error('invalid thumbnail format specified')
+
+ if opts.cookiesfrombrowser is not None:
+ opts.cookiesfrombrowser = [
+ part.strip() or None for part in opts.cookiesfrombrowser.split(':', 1)]
+ if opts.cookiesfrombrowser[0] not in SUPPORTED_BROWSERS:
+ parser.error('unsupported browser specified for cookies')
if opts.date is not None:
date = DateRange.day(opts.date)
else:
date = DateRange(opts.dateafter, opts.datebefore)
- # Do not download videos when there are audio-only formats
+ def parse_compat_opts():
+ parsed_compat_opts, compat_opts = set(), opts.compat_opts[::-1]
+ while compat_opts:
+ actual_opt = opt = compat_opts.pop().lower()
+ if opt == 'youtube-dl':
+ compat_opts.extend(['-multistreams', 'all'])
+ elif opt == 'youtube-dlc':
+ compat_opts.extend(['-no-youtube-channel-redirect', '-no-live-chat', 'all'])
+ elif opt == 'all':
+ parsed_compat_opts.update(all_compat_opts)
+ elif opt == '-all':
+ parsed_compat_opts = set()
+ else:
+ if opt[0] == '-':
+ opt = opt[1:]
+ parsed_compat_opts.discard(opt)
+ else:
+ parsed_compat_opts.update([opt])
+ if opt not in all_compat_opts:
+ parser.error('Invalid compatibility option %s' % actual_opt)
+ return parsed_compat_opts
+
+ all_compat_opts = [
+ 'filename', 'format-sort', 'abort-on-error', 'format-spec', 'no-playlist-metafiles',
+ 'multistreams', 'no-live-chat', 'playlist-index', 'list-formats', 'no-direct-merge',
+ 'no-youtube-channel-redirect', 'no-youtube-unavailable-videos', 'no-attach-info-json',
+ 'embed-thumbnail-atomicparsley', 'seperate-video-versions',
+ ]
+ compat_opts = parse_compat_opts()
+
+ def _unused_compat_opt(name):
+ if name not in compat_opts:
+ return False
+ compat_opts.discard(name)
+ compat_opts.update(['*%s' % name])
+ return True
+
+ def set_default_compat(compat_name, opt_name, default=True, remove_compat=False):
+ attr = getattr(opts, opt_name)
+ if compat_name in compat_opts:
+ if attr is None:
+ setattr(opts, opt_name, not default)
+ return True
+ else:
+ if remove_compat:
+ _unused_compat_opt(compat_name)
+ return False
+ elif attr is None:
+ setattr(opts, opt_name, default)
+ return None
+
+ set_default_compat('abort-on-error', 'ignoreerrors')
+ set_default_compat('no-playlist-metafiles', 'allow_playlist_files')
+ if 'format-sort' in compat_opts:
+ opts.format_sort.extend(InfoExtractor.FormatSort.ytdl_default)
+ _video_multistreams_set = set_default_compat('multistreams', 'allow_multiple_video_streams', False, remove_compat=False)
+ _audio_multistreams_set = set_default_compat('multistreams', 'allow_multiple_audio_streams', False, remove_compat=False)
+ if _video_multistreams_set is False and _audio_multistreams_set is False:
+ _unused_compat_opt('multistreams')
+ outtmpl_default = opts.outtmpl.get('default')
+ if 'filename' in compat_opts:
+ if outtmpl_default is None:
+ outtmpl_default = '%(title)s.%(id)s.%(ext)s'
+ opts.outtmpl.update({'default': outtmpl_default})
+ else:
+ _unused_compat_opt('filename')
+
+ def validate_outtmpl(tmpl, msg):
+ err = YoutubeDL.validate_outtmpl(tmpl)
+ if err:
+ parser.error('invalid %s %r: %s' % (msg, tmpl, error_to_compat_str(err)))
+
+ for k, tmpl in opts.outtmpl.items():
+ validate_outtmpl(tmpl, '%s output template' % k)
+ for tmpl in opts.forceprint:
+ validate_outtmpl(tmpl, 'print template')
+
if opts.extractaudio and not opts.keepvideo and opts.format is None:
opts.format = 'bestaudio/best'
- outtmpl = opts.outtmpl
- if not outtmpl:
- outtmpl = {'default': (
- '%(title)s-%(id)s-%(format)s.%(ext)s' if opts.format == '-1' and opts.usetitle
- else '%(id)s-%(format)s.%(ext)s' if opts.format == '-1'
- else '%(autonumber)s-%(title)s-%(id)s.%(ext)s' if opts.usetitle and opts.autonumber
- else '%(title)s-%(id)s.%(ext)s' if opts.usetitle
- else '%(id)s.%(ext)s' if opts.useid
- else '%(autonumber)s-%(id)s.%(ext)s' if opts.autonumber
- else None)}
- outtmpl_default = outtmpl.get('default')
if outtmpl_default is not None and not os.path.splitext(outtmpl_default)[1] and opts.extractaudio:
parser.error('Cannot download a video and extract audio into the same'
' file! Use "{0}.%(ext)s" instead of "{0}" as the output'
if re.match(MetadataFromFieldPP.regex, f) is None:
parser.error('invalid format string "%s" specified for --parse-metadata' % f)
- any_getting = opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson or opts.dump_single_json
+ any_getting = opts.forceprint or opts.geturl or opts.gettitle or opts.getid or opts.getthumbnail or opts.getdescription or opts.getfilename or opts.getformat or opts.getduration or opts.dumpjson or opts.dump_single_json
any_printing = opts.print_json
download_archive_fn = expand_path(opts.download_archive) if opts.download_archive is not None else opts.download_archive
opts.writeinfojson = True
def report_conflict(arg1, arg2):
- write_string('WARNING: %s is ignored since %s was given\n' % (arg2, arg1), out=sys.stderr)
+ warnings.append('%s is ignored since %s was given' % (arg2, arg1))
+
if opts.remuxvideo and opts.recodevideo:
report_conflict('--recode-video', '--remux-video')
opts.remuxvideo = False
+ if opts.sponskrub_cut and opts.split_chapters and opts.sponskrub is not False:
+ report_conflict('--split-chapter', '--sponskrub-cut')
+ opts.sponskrub_cut = False
+
if opts.allow_unplayable_formats:
if opts.extractaudio:
report_conflict('--allow-unplayable-formats', '--extract-audio')
postprocessors.append({
'key': 'MetadataFromField',
'formats': opts.metafromfield,
- 'when': 'beforedl'
+ # Run this immediately after extraction is complete
+ 'when': 'pre_process'
+ })
+ if opts.convertsubtitles:
+ postprocessors.append({
+ 'key': 'FFmpegSubtitlesConvertor',
+ 'format': opts.convertsubtitles,
+ # Run this before the actual video download
+ 'when': 'before_dl'
+ })
+ if opts.convertthumbnails:
+ postprocessors.append({
+ 'key': 'FFmpegThumbnailsConvertor',
+ 'format': opts.convertthumbnails,
+ # Run this before the actual video download
+ 'when': 'before_dl'
+ })
+ # Must be after all other before_dl
+ if opts.exec_before_dl_cmd:
+ postprocessors.append({
+ 'key': 'ExecAfterDownload',
+ 'exec_cmd': opts.exec_before_dl_cmd,
+ 'when': 'before_dl'
})
if opts.extractaudio:
postprocessors.append({
# so metadata can be added here.
if opts.addmetadata:
postprocessors.append({'key': 'FFmpegMetadata'})
- if opts.convertsubtitles:
- postprocessors.append({
- 'key': 'FFmpegSubtitlesConvertor',
- 'format': opts.convertsubtitles,
- })
if opts.embedsubtitles:
already_have_subtitle = opts.writesubtitles
postprocessors.append({
'key': 'FFmpegEmbedSubtitle',
+ # already_have_subtitle = True prevents the file from being deleted after embedding
'already_have_subtitle': already_have_subtitle
})
if not already_have_subtitle:
# this was the old behaviour if only --all-sub was given.
if opts.allsubtitles and not opts.writeautomaticsub:
opts.writesubtitles = True
- if opts.embedthumbnail:
- already_have_thumbnail = opts.writethumbnail or opts.write_all_thumbnails
- postprocessors.append({
- 'key': 'EmbedThumbnail',
- 'already_have_thumbnail': already_have_thumbnail
- })
- if not already_have_thumbnail:
- opts.writethumbnail = True
- # XAttrMetadataPP should be run after post-processors that may change file
- # contents
- if opts.xattrs:
- postprocessors.append({'key': 'XAttrMetadata'})
- # This should be below all ffmpeg PP because it may cut parts out from the video
+ # This should be above EmbedThumbnail since sponskrub removes the thumbnail attachment
+ # but must be below EmbedSubtitle and FFmpegMetadata
+ # See https://github.com/yt-dlp/yt-dlp/issues/204 , https://github.com/faissaloo/SponSkrub/issues/29
# If opts.sponskrub is None, sponskrub is used, but it silently fails if the executable can't be found
if opts.sponskrub is not False:
postprocessors.append({
'force': opts.sponskrub_force,
'ignoreerror': opts.sponskrub is None,
})
+ if opts.embedthumbnail:
+ already_have_thumbnail = opts.writethumbnail or opts.write_all_thumbnails
+ postprocessors.append({
+ 'key': 'EmbedThumbnail',
+ # already_have_thumbnail = True prevents the file from being deleted after embedding
+ 'already_have_thumbnail': already_have_thumbnail
+ })
+ if not already_have_thumbnail:
+ opts.writethumbnail = True
+ if opts.split_chapters:
+ postprocessors.append({'key': 'FFmpegSplitChapters'})
+ # XAttrMetadataPP should be run after post-processors that may change file contents
+ if opts.xattrs:
+ postprocessors.append({'key': 'XAttrMetadata'})
# ExecAfterDownload must be the last PP
if opts.exec_cmd:
postprocessors.append({
'key': 'ExecAfterDownload',
'exec_cmd': opts.exec_cmd,
- 'when': 'aftermove'
+ # Run this only after the files have been moved to their final locations
+ 'when': 'after_move'
})
def report_args_compat(arg, name):
- write_string(
- 'WARNING: %s given without specifying name. The arguments will be given to all %s\n' % (arg, name),
- out=sys.stderr)
+ warnings.append('%s given without specifying name. The arguments will be given to all %s' % (arg, name))
+
if 'default' in opts.external_downloader_args:
- report_args_compat('--external-downloader-args', 'external downloaders')
+ report_args_compat('--downloader-args', 'external downloaders')
if 'default-compat' in opts.postprocessor_args and 'default' not in opts.postprocessor_args:
report_args_compat('--post-processor-args', 'post-processors')
opts.postprocessor_args['default'] = opts.postprocessor_args['default-compat']
final_ext = (
- opts.recodevideo
- or (opts.remuxvideo in REMUX_EXTENSIONS) and opts.remuxvideo
- or (opts.extractaudio and opts.audioformat != 'best') and opts.audioformat
- or None)
+ opts.recodevideo if opts.recodevideo in FFmpegVideoConvertorPP.SUPPORTED_EXTS
+ else opts.remuxvideo if opts.remuxvideo in FFmpegVideoRemuxerPP.SUPPORTED_EXTS
+ else opts.audioformat if (opts.extractaudio and opts.audioformat != 'best')
+ else None)
match_filter = (
None if opts.match_filter is None
else match_filter_func(opts.match_filter))
ydl_opts = {
- 'convertsubtitles': opts.convertsubtitles,
'usenetrc': opts.usenetrc,
'username': opts.username,
'password': opts.password,
'forceduration': opts.getduration,
'forcefilename': opts.getfilename,
'forceformat': opts.getformat,
+ 'forceprint': opts.forceprint,
'forcejson': opts.dumpjson or opts.print_json,
'dump_single_json': opts.dump_single_json,
'force_write_download_archive': opts.force_write_download_archive,
'skip_download': opts.skip_download,
'format': opts.format,
'allow_unplayable_formats': opts.allow_unplayable_formats,
+ 'ignore_no_formats_error': opts.ignore_no_formats_error,
'format_sort': opts.format_sort,
'format_sort_force': opts.format_sort_force,
'allow_multiple_video_streams': opts.allow_multiple_video_streams,
'allow_multiple_audio_streams': opts.allow_multiple_audio_streams,
+ 'check_formats': opts.check_formats,
'listformats': opts.listformats,
'listformats_table': opts.listformats_table,
- 'outtmpl': outtmpl,
+ 'outtmpl': opts.outtmpl,
'outtmpl_na_placeholder': opts.outtmpl_na_placeholder,
'paths': opts.paths,
'autonumber_size': opts.autonumber_size,
'ignoreerrors': opts.ignoreerrors,
'force_generic_extractor': opts.force_generic_extractor,
'ratelimit': opts.ratelimit,
+ 'throttledratelimit': opts.throttledratelimit,
'overwrites': opts.overwrites,
'retries': opts.retries,
'fragment_retries': opts.fragment_retries,
'writeannotations': opts.writeannotations,
'writeinfojson': opts.writeinfojson,
'allow_playlist_files': opts.allow_playlist_files,
+ 'clean_infojson': opts.clean_infojson,
'getcomments': opts.getcomments,
'writethumbnail': opts.writethumbnail,
'write_all_thumbnails': opts.write_all_thumbnails,
'download_archive': download_archive_fn,
'break_on_existing': opts.break_on_existing,
'break_on_reject': opts.break_on_reject,
+ 'skip_playlist_after_errors': opts.skip_playlist_after_errors,
'cookiefile': opts.cookiefile,
+ 'cookiesfrombrowser': opts.cookiesfrombrowser,
'nocheckcertificate': opts.no_check_certificate,
'prefer_insecure': opts.prefer_insecure,
'proxy': opts.proxy,
'include_ads': opts.include_ads,
'default_search': opts.default_search,
'dynamic_mpd': opts.dynamic_mpd,
+ 'extractor_args': opts.extractor_args,
'youtube_include_dash_manifest': opts.youtube_include_dash_manifest,
'youtube_include_hls_manifest': opts.youtube_include_hls_manifest,
'encoding': opts.encoding,
'geo_bypass': opts.geo_bypass,
'geo_bypass_country': opts.geo_bypass_country,
'geo_bypass_ip_block': opts.geo_bypass_ip_block,
+ 'warnings': warnings,
+ 'compat_opts': compat_opts,
# just for deprecation check
- 'autonumber': opts.autonumber if opts.autonumber is True else None,
- 'usetitle': opts.usetitle if opts.usetitle is True else None,
+ 'autonumber': opts.autonumber or None,
+ 'usetitle': opts.usetitle or None,
+ 'useid': opts.useid or None,
}
with YoutubeDL(ydl_opts) as ydl:
# Update version
if opts.update_self:
# If updater returns True, exit. Required for windows
- if update_self(ydl.to_screen, opts.verbose, ydl._opener):
+ if run_update(ydl):
if actual_use:
sys.exit('ERROR: The program must exit for the update to complete')
sys.exit()