[FfmpegMetadata] Allow setting metadata of individual streams

Closes #877
This commit is contained in:
pukkandan 2022-01-02 03:31:49 +05:30
parent 1797b073ed
commit 8896899216
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698
2 changed files with 18 additions and 13 deletions

View file

@ -1,5 +1,6 @@
from __future__ import unicode_literals
import collections
import io
import itertools
import os
@ -728,15 +729,15 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
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 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
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):