mirror of https://github.com/yt-dlp/yt-dlp.git
Ability to set a specific field in the file's metadata
Eg: `--parse-metadata "description:(?s)(?P<meta_comment>.+)"` sets the "comment" field using `description`
This commit is contained in:
parent
54df8fc5b2
commit
84601bb72b
|
@ -14,10 +14,10 @@ from yt_dlp.postprocessor import MetadataFromFieldPP, MetadataFromTitlePP
|
||||||
class TestMetadataFromField(unittest.TestCase):
|
class TestMetadataFromField(unittest.TestCase):
|
||||||
def test_format_to_regex(self):
|
def test_format_to_regex(self):
|
||||||
pp = MetadataFromFieldPP(None, ['title:%(title)s - %(artist)s'])
|
pp = MetadataFromFieldPP(None, ['title:%(title)s - %(artist)s'])
|
||||||
self.assertEqual(pp._data[0]['regex'], r'(?P<title>[^\r\n]+)\ \-\ (?P<artist>[^\r\n]+)')
|
self.assertEqual(pp._data[0]['regex'], r'(?P<title>.+)\ \-\ (?P<artist>.+)')
|
||||||
|
|
||||||
|
|
||||||
class TestMetadataFromTitle(unittest.TestCase):
|
class TestMetadataFromTitle(unittest.TestCase):
|
||||||
def test_format_to_regex(self):
|
def test_format_to_regex(self):
|
||||||
pp = MetadataFromTitlePP(None, '%(title)s - %(artist)s')
|
pp = MetadataFromTitlePP(None, '%(title)s - %(artist)s')
|
||||||
self.assertEqual(pp._titleregex, r'(?P<title>[^\r\n]+)\ \-\ (?P<artist>[^\r\n]+)')
|
self.assertEqual(pp._titleregex, r'(?P<title>.+)\ \-\ (?P<artist>.+)')
|
||||||
|
|
|
@ -10,7 +10,7 @@ import json
|
||||||
|
|
||||||
from .common import AudioConversionError, PostProcessor
|
from .common import AudioConversionError, PostProcessor
|
||||||
|
|
||||||
from ..compat import compat_str
|
from ..compat import compat_str, compat_numeric_types
|
||||||
from ..utils import (
|
from ..utils import (
|
||||||
encodeArgument,
|
encodeArgument,
|
||||||
encodeFilename,
|
encodeFilename,
|
||||||
|
@ -530,6 +530,8 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
|
||||||
metadata = {}
|
metadata = {}
|
||||||
|
|
||||||
def add(meta_list, info_list=None):
|
def add(meta_list, info_list=None):
|
||||||
|
if not meta_list:
|
||||||
|
return
|
||||||
if not info_list:
|
if not info_list:
|
||||||
info_list = meta_list
|
info_list = meta_list
|
||||||
if not isinstance(meta_list, (list, tuple)):
|
if not isinstance(meta_list, (list, tuple)):
|
||||||
|
@ -537,7 +539,7 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
|
||||||
if not isinstance(info_list, (list, tuple)):
|
if not isinstance(info_list, (list, tuple)):
|
||||||
info_list = (info_list,)
|
info_list = (info_list,)
|
||||||
for info_f in info_list:
|
for info_f in info_list:
|
||||||
if info.get(info_f) is not None:
|
if isinstance(info.get(info_f), (compat_str, compat_numeric_types)):
|
||||||
for meta_f in meta_list:
|
for meta_f in meta_list:
|
||||||
metadata[meta_f] = info[info_f]
|
metadata[meta_f] = info[info_f]
|
||||||
break
|
break
|
||||||
|
@ -563,6 +565,10 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
|
||||||
add('episode_id', ('episode', 'episode_id'))
|
add('episode_id', ('episode', 'episode_id'))
|
||||||
add('episode_sort', 'episode_number')
|
add('episode_sort', 'episode_number')
|
||||||
|
|
||||||
|
prefix = 'meta_'
|
||||||
|
for key in filter(lambda k: k.startswith(prefix), info.keys()):
|
||||||
|
add(key[len(prefix):], key)
|
||||||
|
|
||||||
if not metadata:
|
if not metadata:
|
||||||
self.to_screen('There isn\'t any metadata to add')
|
self.to_screen('There isn\'t any metadata to add')
|
||||||
return [], info
|
return [], info
|
||||||
|
@ -577,7 +583,7 @@ class FFmpegMetadataPP(FFmpegPostProcessor):
|
||||||
else:
|
else:
|
||||||
options.extend(['-c', 'copy'])
|
options.extend(['-c', 'copy'])
|
||||||
|
|
||||||
for (name, value) in metadata.items():
|
for name, value in metadata.items():
|
||||||
options.extend(['-metadata', '%s=%s' % (name, value)])
|
options.extend(['-metadata', '%s=%s' % (name, value)])
|
||||||
|
|
||||||
chapters = info.get('chapters', [])
|
chapters = info.get('chapters', [])
|
||||||
|
|
|
@ -45,7 +45,7 @@ class MetadataFromFieldPP(PostProcessor):
|
||||||
# replace %(..)s with regex group and escape other string parts
|
# replace %(..)s with regex group and escape other string parts
|
||||||
for match in re.finditer(r'%\((\w+)\)s', fmt):
|
for match in re.finditer(r'%\((\w+)\)s', fmt):
|
||||||
regex += re.escape(fmt[lastpos:match.start()])
|
regex += re.escape(fmt[lastpos:match.start()])
|
||||||
regex += r'(?P<%s>[^\r\n]+)' % match.group(1)
|
regex += r'(?P<%s>.+)' % match.group(1)
|
||||||
lastpos = match.end()
|
lastpos = match.end()
|
||||||
if lastpos < len(fmt):
|
if lastpos < len(fmt):
|
||||||
regex += re.escape(fmt[lastpos:])
|
regex += re.escape(fmt[lastpos:])
|
||||||
|
|
Loading…
Reference in New Issue