Add option --print

Deprecates: `--get-description`, `--get-duration`, `--get-filename`, `--get-format`, `--get-id`, `--get-thumbnail`, `--get-title`, `--get-url`
Closes #295
This commit is contained in:
pukkandan 2021-05-14 13:14:38 +05:30
parent e632bce2e4
commit 53c18592d3
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
4 changed files with 66 additions and 38 deletions

View File

@ -534,14 +534,9 @@ ## Verbosity and Simulation Options:
formats are found (default) formats are found (default)
--skip-download Do not download the video but write all --skip-download Do not download the video but write all
related files (Alias: --no-download) related files (Alias: --no-download)
-g, --get-url Simulate, quiet but print URL -O, --print TEMPLATE Simulate, quiet but print the given fields.
-e, --get-title Simulate, quiet but print title Either a field name or similar formatting
--get-id Simulate, quiet but print id as the output template can be used
--get-thumbnail Simulate, quiet but print thumbnail URL
--get-description Simulate, quiet but print video description
--get-duration Simulate, quiet but print video length
--get-filename Simulate, quiet but print output filename
--get-format Simulate, quiet but print output format
-j, --dump-json Simulate, quiet but print JSON information. -j, --dump-json Simulate, quiet but print JSON information.
See "OUTPUT TEMPLATE" for a description of See "OUTPUT TEMPLATE" for a description of
available keys available keys
@ -912,7 +907,7 @@ # OUTPUT TEMPLATE
- `channel_id` (string): Id of the channel - `channel_id` (string): Id of the channel
- `location` (string): Physical location where the video was filmed - `location` (string): Physical location where the video was filmed
- `duration` (numeric): Length of the video in seconds - `duration` (numeric): Length of the video in seconds
- `duration_string` (string): Length of the video (HH-mm-ss) - `duration_string` (string): Length of the video (HH:mm:ss)
- `view_count` (numeric): How many users have watched the video on the platform - `view_count` (numeric): How many users have watched the video on the platform
- `like_count` (numeric): Number of positive ratings of the video - `like_count` (numeric): Number of positive ratings of the video
- `dislike_count` (numeric): Number of negative ratings of the video - `dislike_count` (numeric): Number of negative ratings of the video
@ -990,6 +985,11 @@ # OUTPUT TEMPLATE
- `section_start` (numeric): Start time of the chapter in seconds - `section_start` (numeric): Start time of the chapter in seconds
- `section_end` (numeric): End time of the chapter in seconds - `section_end` (numeric): End time of the chapter in seconds
Available only when used in `--print`:
- `urls` (string): The URLs of all requested formats, one in each line
- `filename` (string): Name of the video file. Note that the actual filename may be different due to post-processing. Use `--exec echo` to get the name after all postprocessing is complete
Each aforementioned sequence when referenced in an output template will be replaced by the actual value corresponding to the sequence name. Note that some of the sequences are not guaranteed to be present since they depend on the metadata obtained by a particular extractor. Such sequences will be replaced with placeholder value provided with `--output-na-placeholder` (`NA` by default). Each aforementioned sequence when referenced in an output template will be replaced by the actual value corresponding to the sequence name. Note that some of the sequences are not guaranteed to be present since they depend on the metadata obtained by a particular extractor. Such sequences will be replaced with placeholder value provided with `--output-na-placeholder` (`NA` by default).
For example for `-o %(title)s-%(id)s.%(ext)s` and an mp4 video with title `yt-dlp test video` and id `BaW_jenozKcj`, this will result in a `yt-dlp test video-BaW_jenozKcj.mp4` file created in the current directory. For example for `-o %(title)s-%(id)s.%(ext)s` and an mp4 video with title `yt-dlp test video` and id `BaW_jenozKcj`, this will result in a `yt-dlp test video-BaW_jenozKcj.mp4` file created in the current directory.
@ -1313,6 +1313,14 @@ # DEPRECATED OPTIONS
#### Not recommended #### Not recommended
While these options still work, their use is not recommended since there are other alternatives to achieve the same While these options still work, their use is not recommended since there are other alternatives to achieve the same
--get-description --print description
--get-duration --print duration_string
--get-filename --print filename
--get-format --print format
--get-id --print id
--get-thumbnail --print thumbnail
-e, --get-title --print title
-g, --get-url --print urls
--all-formats -f all --all-formats -f all
--all-subs --sub-langs all --write-subs --all-subs --sub-langs all --write-subs
--autonumber-size NUMBER Use string formatting. Eg: %(autonumber)03d --autonumber-size NUMBER Use string formatting. Eg: %(autonumber)03d

View File

@ -177,13 +177,14 @@ class YoutubeDL(object):
verbose: Print additional info to stdout. verbose: Print additional info to stdout.
quiet: Do not print messages to stdout. quiet: Do not print messages to stdout.
no_warnings: Do not print out anything for warnings. no_warnings: Do not print out anything for warnings.
forceurl: Force printing final URL. forceprint: A list of templates to force print
forcetitle: Force printing title. forceurl: Force printing final URL. (Deprecated)
forceid: Force printing ID. forcetitle: Force printing title. (Deprecated)
forcethumbnail: Force printing thumbnail URL. forceid: Force printing ID. (Deprecated)
forcedescription: Force printing description. forcethumbnail: Force printing thumbnail URL. (Deprecated)
forcefilename: Force printing final filename. forcedescription: Force printing description. (Deprecated)
forceduration: Force printing duration. forcefilename: Force printing final filename. (Deprecated)
forceduration: Force printing duration. (Deprecated)
forcejson: Force printing info_dict as JSON. forcejson: Force printing info_dict as JSON.
dump_single_json: Force printing the info_dict of the whole playlist dump_single_json: Force printing the info_dict of the whole playlist
(or video) as a single JSON line. (or video) as a single JSON line.
@ -820,7 +821,7 @@ def prepare_outtmpl(self, outtmpl, info_dict, sanitize=None):
# duration_string # duration_string
template_dict['duration_string'] = ( # %(duration>%H-%M-%S)s is wrong if duration > 24hrs template_dict['duration_string'] = ( # %(duration>%H-%M-%S)s is wrong if duration > 24hrs
formatSeconds(info_dict['duration'], '-') formatSeconds(info_dict['duration'], '-' if sanitize else ':')
if info_dict.get('duration', None) is not None if info_dict.get('duration', None) is not None
else None) else None)
@ -2206,32 +2207,43 @@ def process_subtitles(self, video_id, normal_subtitles, automatic_captions):
return subs return subs
def __forced_printings(self, info_dict, filename, incomplete): def __forced_printings(self, info_dict, filename, incomplete):
def print_mandatory(field): def print_mandatory(field, actual_field=None):
if actual_field is None:
actual_field = field
if (self.params.get('force%s' % field, False) if (self.params.get('force%s' % field, False)
and (not incomplete or info_dict.get(field) is not None)): and (not incomplete or info_dict.get(actual_field) is not None)):
self.to_stdout(info_dict[field]) self.to_stdout(info_dict[actual_field])
def print_optional(field): def print_optional(field):
if (self.params.get('force%s' % field, False) if (self.params.get('force%s' % field, False)
and info_dict.get(field) is not None): and info_dict.get(field) is not None):
self.to_stdout(info_dict[field]) self.to_stdout(info_dict[field])
info_dict = info_dict.copy()
if filename is not None:
info_dict['filename'] = filename
if info_dict.get('requested_formats') is not None:
# For RTMP URLs, also include the playpath
info_dict['urls'] = '\n'.join(f['url'] + f.get('play_path', '') for f in info_dict['requested_formats'])
elif 'url' in info_dict:
info_dict['urls'] = info_dict['url'] + info_dict.get('play_path', '')
for tmpl in self.params.get('forceprint', []):
if re.match(r'\w+$', tmpl):
tmpl = '%({})s'.format(tmpl)
tmpl, info_copy = self.prepare_outtmpl(tmpl, info_dict)
self.to_stdout(tmpl % info_copy)
print_mandatory('title') print_mandatory('title')
print_mandatory('id') print_mandatory('id')
if self.params.get('forceurl', False) and not incomplete: print_mandatory('url', 'urls')
if info_dict.get('requested_formats') is not None:
for f in info_dict['requested_formats']:
self.to_stdout(f['url'] + f.get('play_path', ''))
else:
# For RTMP URLs, also include the playpath
self.to_stdout(info_dict['url'] + info_dict.get('play_path', ''))
print_optional('thumbnail') print_optional('thumbnail')
print_optional('description') print_optional('description')
if self.params.get('forcefilename', False) and filename is not None: print_optional('filename')
self.to_stdout(filename)
if self.params.get('forceduration', False) and info_dict.get('duration') is not None: if self.params.get('forceduration', False) and info_dict.get('duration') is not None:
self.to_stdout(formatSeconds(info_dict['duration'])) self.to_stdout(formatSeconds(info_dict['duration']))
print_mandatory('format') print_mandatory('format')
if self.params.get('forcejson', False): if self.params.get('forcejson', False):
self.post_extract(info_dict) self.post_extract(info_dict)
self.to_stdout(json.dumps(info_dict, default=repr)) self.to_stdout(json.dumps(info_dict, default=repr))

View File

@ -321,7 +321,7 @@ def set_default_compat(compat_name, opt_name, default=True, remove_compat=False)
if re.match(MetadataFromFieldPP.regex, f) is None: if re.match(MetadataFromFieldPP.regex, f) is None:
parser.error('invalid format string "%s" specified for --parse-metadata' % f) 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.print 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 any_printing = opts.print_json
download_archive_fn = expand_path(opts.download_archive) if opts.download_archive is not None else opts.download_archive download_archive_fn = expand_path(opts.download_archive) if opts.download_archive is not None else opts.download_archive
@ -508,6 +508,7 @@ def report_args_compat(arg, name):
'forceduration': opts.getduration, 'forceduration': opts.getduration,
'forcefilename': opts.getfilename, 'forcefilename': opts.getfilename,
'forceformat': opts.getformat, 'forceformat': opts.getformat,
'forceprint': opts.print,
'forcejson': opts.dumpjson or opts.print_json, 'forcejson': opts.dumpjson or opts.print_json,
'dump_single_json': opts.dump_single_json, 'dump_single_json': opts.dump_single_json,
'force_write_download_archive': opts.force_write_download_archive, 'force_write_download_archive': opts.force_write_download_archive,

View File

@ -788,38 +788,45 @@ def _dict_from_options_callback(
'--skip-download', '--no-download', '--skip-download', '--no-download',
action='store_true', dest='skip_download', default=False, action='store_true', dest='skip_download', default=False,
help='Do not download the video but write all related files (Alias: --no-download)') help='Do not download the video but write all related files (Alias: --no-download)')
verbosity.add_option(
'-O', '--print', metavar='TEMPLATE',
action='callback', dest='print', type='str', default=[],
callback=_list_from_options_callback, callback_kwargs={'delim': None},
help=(
'Simulate, quiet but print the given fields. Either a field name '
'or similar formatting as the output template can be used'))
verbosity.add_option( verbosity.add_option(
'-g', '--get-url', '-g', '--get-url',
action='store_true', dest='geturl', default=False, action='store_true', dest='geturl', default=False,
help='Simulate, quiet but print URL') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'-e', '--get-title', '-e', '--get-title',
action='store_true', dest='gettitle', default=False, action='store_true', dest='gettitle', default=False,
help='Simulate, quiet but print title') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'--get-id', '--get-id',
action='store_true', dest='getid', default=False, action='store_true', dest='getid', default=False,
help='Simulate, quiet but print id') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'--get-thumbnail', '--get-thumbnail',
action='store_true', dest='getthumbnail', default=False, action='store_true', dest='getthumbnail', default=False,
help='Simulate, quiet but print thumbnail URL') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'--get-description', '--get-description',
action='store_true', dest='getdescription', default=False, action='store_true', dest='getdescription', default=False,
help='Simulate, quiet but print video description') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'--get-duration', '--get-duration',
action='store_true', dest='getduration', default=False, action='store_true', dest='getduration', default=False,
help='Simulate, quiet but print video length') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'--get-filename', '--get-filename',
action='store_true', dest='getfilename', default=False, action='store_true', dest='getfilename', default=False,
help='Simulate, quiet but print output filename') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'--get-format', '--get-format',
action='store_true', dest='getformat', default=False, action='store_true', dest='getformat', default=False,
help='Simulate, quiet but print output format') help=optparse.SUPPRESS_HELP)
verbosity.add_option( verbosity.add_option(
'-j', '--dump-json', '-j', '--dump-json',
action='store_true', dest='dumpjson', default=False, action='store_true', dest='dumpjson', default=False,