]> jfr.im git - yt-dlp.git/commitdiff
[FfmpegMetadata] Allow setting metadata of individual streams
authorpukkandan <redacted>
Sat, 1 Jan 2022 22:01:49 +0000 (03:31 +0530)
committerpukkandan <redacted>
Sat, 1 Jan 2022 22:03:15 +0000 (03:33 +0530)
Closes #877

README.md
yt_dlp/postprocessor/ffmpeg.py

index e9785764b79feb8731911537008f82d6922a7e7f..0ebd0594a86ced026c53458f3f2ab908251f7250 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1547,7 +1547,7 @@ # MODIFYING METADATA
 
 This option also has a few special uses:
 * You can download an additional URL based on the metadata of the currently downloaded video. To do this, set the field `additional_urls` to the URL that you want to download. Eg: `--parse-metadata "description:(?P<additional_urls>https?://www\.vimeo\.com/\d+)` will download the first vimeo video found in the description
-* You can use this to change the metadata that is embedded in the media file. To do this, set the value of the corresponding field with a `meta_` prefix. For example, any value you set to `meta_description` field will be added to the `description` field in the file. For example, you can use this to set a different "description" and "synopsis". Any value set to the `meta_` field will overwrite all default values.
+* You can use this to change the metadata that is embedded in the media file. To do this, set the value of the corresponding field with a `meta_` prefix. For example, any value you set to `meta_description` field will be added to the `description` field in the file. For example, you can use this to set a different "description" and "synopsis". To modify the metadata of individual streams, use the `meta<n>_` prefix (Eg: `meta1_language`). Any value set to the `meta_` field will overwrite all default values.
 
 For reference, these are the fields yt-dlp adds by default to the file metadata:
 
index 96b48ded5899d01ca08e8bb68859fdedcc218dd0..97f04d11663564e22f165c576a0c676c19cd6cf2 100644 (file)
@@ -1,5 +1,6 @@
 from __future__ import unicode_literals
 
+import collections
 import io
 import itertools
 import os
@@ -728,15 +729,15 @@ def ffmpeg_escape(text):
         yield ('-map_metadata', '1')
 
     def _get_metadata_opts(self, info):
-        metadata = {}
-        meta_prefix = 'meta_'
+        meta_prefix = 'meta'
+        metadata = collections.defaultdict(dict)
 
         def add(meta_list, info_list=None):
             value = next((
-                str(info[key]) for key in [meta_prefix] + list(variadic(info_list or meta_list))
+                str(info[key]) for key in [f'{meta_prefix}_'] + list(variadic(info_list or meta_list))
                 if info.get(key) is not None), None)
             if value not in ('', None):
-                metadata.update({meta_f: value for meta_f in variadic(meta_list)})
+                metadata['common'].update({meta_f: value for meta_f in variadic(meta_list)})
 
         # See [1-4] for some info on media metadata/metadata supported
         # by ffmpeg.
@@ -760,22 +761,26 @@ def add(meta_list, info_list=None):
         add('episode_sort', 'episode_number')
         if 'embed-metadata' in self.get_param('compat_opts', []):
             add('comment', 'description')
-            metadata.pop('synopsis', None)
+            metadata['common'].pop('synopsis', None)
 
+        meta_regex = rf'{re.escape(meta_prefix)}(?P<i>\d+)?_(?P<key>.+)'
         for key, value in info.items():
-            if value is not None and key != meta_prefix and key.startswith(meta_prefix):
-                metadata[key[len(meta_prefix):]] = value
+            mobj = re.fullmatch(meta_regex, key)
+            if value is not None and mobj:
+                metadata[mobj.group('i') or 'common'][mobj.group('key')] = value
 
-        for name, value in metadata.items():
+        for name, value in metadata['common'].items():
             yield ('-metadata', f'{name}={value}')
 
         stream_idx = 0
         for fmt in info.get('requested_formats') or []:
             stream_count = 2 if 'none' not in (fmt.get('vcodec'), fmt.get('acodec')) else 1
-            if fmt.get('language'):
-                lang = ISO639Utils.short2long(fmt['language']) or fmt['language']
-                for i in range(stream_count):
-                    yield ('-metadata:s:%d' % (stream_idx + i), 'language=%s' % lang)
+            lang = ISO639Utils.short2long(fmt['language']) or fmt.get('language')
+            for i in range(stream_idx, stream_idx + stream_count):
+                if lang:
+                    metadata[str(i)].setdefault('language', lang)
+                for name, value in metadata[str(i)].items():
+                    yield (f'-metadata:s:{i}', f'{name}={value}')
             stream_idx += stream_count
 
     def _get_infojson_opts(self, info, infofn):