From e29663c644a65846125f5792be52dc27feb68297 Mon Sep 17 00:00:00 2001 From: pukkandan Date: Wed, 3 Feb 2021 02:45:00 +0530 Subject: [PATCH] #45 Allow date/time formatting in output template Closes #43 --- README.md | 11 +++++-- youtube_dlc/YoutubeDL.py | 62 +++++++++++++++++++++++++++------------- youtube_dlc/utils.py | 13 +++++++++ 3 files changed, 63 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 1811ed6db..45b0031c6 100644 --- a/README.md +++ b/README.md @@ -751,7 +751,9 @@ The `-o` option is used to indicate a template for the output file names while ` **tl;dr:** [navigate me to examples](#output-template-examples). -The basic usage is not to set any template arguments when downloading a single file, like in `youtube-dlc -o funny_video.flv "https://some/video"`. However, it may contain special sequences that will be replaced when downloading each video. The special sequences may be formatted according to [python string formatting operations](https://docs.python.org/2/library/stdtypes.html#string-formatting). For example, `%(NAME)s` or `%(NAME)05d`. To clarify, that is a percent symbol followed by a name in parentheses, followed by formatting operations. Allowed names along with sequence type are: +The basic usage of `-o` is not to set any template arguments when downloading a single file, like in `youtube-dlc -o funny_video.flv "https://some/video"`. However, it may contain special sequences that will be replaced when downloading each video. The special sequences may be formatted according to [python string formatting operations](https://docs.python.org/2/library/stdtypes.html#string-formatting). For example, `%(NAME)s` or `%(NAME)05d`. To clarify, that is a percent symbol followed by a name in parentheses, followed by formatting operations. Additionally, date/time fields can be formatted according to [strftime formatting](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) by specifying it inside the parantheses seperated from the field name using a `>`. For example, `%(duration>%H-%M-%S)s`. + +The available fields are: - `id` (string): Video identifier - `title` (string): Video title @@ -870,14 +872,17 @@ youtube-dlc_test_video_.mp4 # A simple file name # Download YouTube playlist videos in separate directory indexed by video order in a playlist $ youtube-dlc -o '%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re +# Download YouTube playlist videos in seperate directories according to their uploaded year +$ youtube-dlc -o '%(upload_date>%Y)s/%(title)s.%(ext)s' https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re + # Download all playlists of YouTube channel/user keeping each playlist in separate directory: $ youtube-dlc -o '%(uploader)s/%(playlist)s/%(playlist_index)s - %(title)s.%(ext)s' https://www.youtube.com/user/TheLinuxFoundation/playlists # Download Udemy course keeping each chapter in separate directory under MyVideos directory in your home -$ youtube-dlc -u user -p password -o '~/MyVideos/%(playlist)s/%(chapter_number)s - %(chapter)s/%(title)s.%(ext)s' https://www.udemy.com/java-tutorial/ +$ youtube-dlc -u user -p password -P '~/MyVideos' -o '%(playlist)s/%(chapter_number)s - %(chapter)s/%(title)s.%(ext)s' https://www.udemy.com/java-tutorial/ # Download entire series season keeping each series and each season in separate directory under C:/MyVideos -$ youtube-dlc -o "C:/MyVideos/%(series)s/%(season_number)s - %(season)s/%(episode_number)s - %(episode)s.%(ext)s" https://videomore.ru/kino_v_detalayah/5_sezon/367617 +$ youtube-dlc -P "C:/MyVideos" -o "%(series)s/%(season_number)s - %(season)s/%(episode_number)s - %(episode)s.%(ext)s" https://videomore.ru/kino_v_detalayah/5_sezon/367617 # Stream the video being downloaded to stdout $ youtube-dlc -o - BaW_jenozKc diff --git a/youtube_dlc/YoutubeDL.py b/youtube_dlc/YoutubeDL.py index 0b198f50d..da5001f07 100644 --- a/youtube_dlc/YoutubeDL.py +++ b/youtube_dlc/YoutubeDL.py @@ -61,6 +61,7 @@ from .utils import ( ExistingVideoReached, expand_path, ExtractorError, + float_or_none, format_bytes, format_field, formatSeconds, @@ -91,6 +92,7 @@ from .utils import ( sanitized_Request, std_headers, str_or_none, + strftime_or_none, subtitles_filename, to_high_limit_path, UnavailableVideoError, @@ -735,6 +737,11 @@ class YoutubeDL(object): try: template_dict = dict(info_dict) + template_dict['duration_string'] = ( # %(duration>%H-%M-%S)s is wrong if duration > 24hrs + formatSeconds(info_dict['duration'], '-') + if info_dict.get('duration', None) is not None + else None) + template_dict['epoch'] = int(time.time()) autonumber_size = self.params.get('autonumber_size') if autonumber_size is None: @@ -755,7 +762,8 @@ class YoutubeDL(object): template_dict = dict((k, v if isinstance(v, compat_numeric_types) else sanitize(k, v)) for k, v in template_dict.items() if v is not None and not isinstance(v, (list, tuple, dict))) - template_dict = collections.defaultdict(lambda: self.params.get('outtmpl_na_placeholder', 'NA'), template_dict) + na = self.params.get('outtmpl_na_placeholder', 'NA') + template_dict = collections.defaultdict(lambda: na, template_dict) outtmpl = self.params.get('outtmpl', DEFAULT_OUTTMPL) @@ -773,27 +781,45 @@ class YoutubeDL(object): r'%%(\1)0%dd' % field_size_compat_map[mobj.group('field')], outtmpl) + # As of [1] format syntax is: + # %[mapping_key][conversion_flags][minimum_width][.precision][length_modifier]type + # 1. https://docs.python.org/2/library/stdtypes.html#string-formatting + FORMAT_RE = r'''(?x) + (?[diouxXeEfFgGcrs%]) # conversion type + ''' + + numeric_fields = list(self._NUMERIC_FIELDS) + + # Format date + FORMAT_DATE_RE = FORMAT_RE.format(r'(?P(?P\w+)>(?P.+?))') + for mobj in re.finditer(FORMAT_DATE_RE, outtmpl): + conv_type, field, frmt, key = mobj.group('type', 'field', 'format', 'key') + if key in template_dict: + continue + value = strftime_or_none(template_dict.get(field), frmt, na) + if conv_type in 'crs': # string + value = sanitize(field, value) + else: # number + numeric_fields.append(key) + value = float_or_none(value, default=None) + if value is not None: + template_dict[key] = value + # Missing numeric fields used together with integer presentation types # in format specification will break the argument substitution since # string NA placeholder is returned for missing fields. We will patch # output template for missing fields to meet string presentation type. - for numeric_field in self._NUMERIC_FIELDS: + for numeric_field in numeric_fields: if numeric_field not in template_dict: - # As of [1] format syntax is: - # %[mapping_key][conversion_flags][minimum_width][.precision][length_modifier]type - # 1. https://docs.python.org/2/library/stdtypes.html#string-formatting - FORMAT_RE = r'''(?x) - (?